Convert a list to a map in Kotlin
This article explores different ways to convert a list to a map in Kotlin.
1. Using associate() function
The standard way to convert a list to a map is with the associate() function. It returns a map containing key-value pairs provided by the transform function applied to the given list elements.
In the following example, we’ll construct a Map from List of Color objects. The Color class contains the name and hex field. They represent the color name and its corresponding HTML color code, respectively.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
data class Color(var name: String, var hex: String) fun main() { val colors: List<Color> = listOf( Color("SILVER", "#C0C0C0"), Color("GOLD", "#FFD700"), Color("OLIVE", "#808000") ) // Add a mapping from name to hex of `Color` object val map: Map<String, String> = colors.associate { Pair(it.name, it.hex) } // or, use below // val map: Map<String, String> = colors.associate { it.name to it.hex } println(map) // {SILVER=#C0C0C0, GOLD=#FFD700, OLIVE=#808000} } |
2. Using associateBy() function
Another good solution is to use the associateBy() function that takes two lambdas for generating the key and the value.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
data class Color(var name: String, var hex: String) fun main() { val colors: List<Color> = listOf( Color("SILVER", "#C0C0C0"), Color("GOLD", "#FFD700"), Color("OLIVE", "#808000") ) // Add a mapping from name to hex of Color object val map: Map<String, String> = colors.associateBy({it.name}, {it.hex}) println(map) // {SILVER=#C0C0C0, GOLD=#FFD700, OLIVE=#808000} } |
3. Using map() function
Another plausible way is to use the map() function to generate a list of Key-Value Pairs and then call the toMap() function to transform it into a map.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
data class Color(var name: String, var hex: String) fun main() { val colors: List<Color> = listOf( Color("SILVER", "#C0C0C0"), Color("GOLD", "#FFD700"), Color("OLIVE", "#808000") ) // Add a mapping from name to hex of Color object val map: Map<String, String> = colors.map { it.name to it.hex }.toMap() println(map) // {SILVER=#C0C0C0, GOLD=#FFD700, OLIVE=#808000} } |
4. Custom Routine
You can write your custom logic for transforming a list into a map. Here’s what the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
data class Color(var name: String, var hex: String) fun main() { val colors: List<Color> = listOf( Color("SILVER", "#C0C0C0"), Color("GOLD", "#FFD700"), Color("OLIVE", "#808000") ) // Add a mapping from name to hex of Color object val map: MutableMap<String, String> = HashMap() for (color in colors) { map[color.name] = color.hex } println(map) // {SILVER=#C0C0C0, GOLD=#FFD700, OLIVE=#808000} } |
That’s all about converting a list to a map in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)