Search code examples
kotlinhashmap

how to transform HashMap<Int,List<IntArray>> to HashMap<Int,List<Int>> in kotlin


For example,I have a HashMap<Int,List> which contains 1 to {[2,1],[3,1],[4,1]} , then I want to use a high-order function to transform this map to HashMap<Int,List> format like 1 to {2,3,4}, so how to write a kotlin code to transform the map. Thank you so much.


Solution

  • You can use mapValues on your map to change the values:

    yourMap.mapValues { entry ->
        entry.value.map { it[0] }
    }
    

    Each list's content was an array and is now the first value of that array.