List to Map Conversion in Kotlin
In this tutorial, we will explore various methods for converting a List to a Map in Kotlin, illustrated with practical examples.
1. Using associateBy
Kotlin offers a straightforward way to convert a list to a Map using associateBy
. This method requires a keySelector and a valueTransformer. In the following example, the list element serves as the key, and its length as the value.
1 2 3 4 5 6 |
fun usingAssociateBy() { val list = listOf("java", "python", "kotlin") val mapName2Length: Map<String, Int> = list.associateBy({ it }, { it.length }) println(mapName2Length) } |
Output:
1 2 |
{java=4, python=6, kotlin=6} |
The resulting map is immutable, and if there are duplicate keys, the last value is used. This behavior is different from Java lambdas, where you must explicitly define duplicate handling. If not specified, Java throws an exception, which can lead to issues in production code.
2. Using toMap
Kotlin collections offer a toMap()
method, which is a simple way to convert a collection to a map. However, you must first call the map()
method to transform each list element into a Pair.
1 2 3 4 5 6 7 |
fun main() { val list = listOf("christian", "damien", "cogni") val map: List<Pair<String, Int>> = list.map { it to it.length } val mapName2Length: Map<String, Int> = map.toMap() println(mapName2Length) } |
Output:
1 2 |
{christian=9, damien=6, cogni=5} |
The code above can be condensed into a single line:
1 2 |
val oneLinerMap: Map<String, Int> = listOf("christian", "damien", "cogni").map { it to it.length }.toMap() |
The returned map is also immutable. We will discuss mutable map conversion later.
3. Using associate
The associate
method is another way to convert a List to a Map. You can pass a transformer function that converts list elements into Pairs.
1 2 3 4 5 6 |
fun usingAssociate() { val list = listOf("a", "abc", "abcd") val mapName2Length: Map<String, Int> = list.associate { it to it.length } println(mapName2Length) } |
Output:
1 2 |
{a=1,abc=3, abcd=4} |
These conversion methods preserve the original list order. The returned map is a LinkedHashMap, which maintains the ordering.
4. Using associateTo
The previous examples produced immutable Maps. If you need a mutable Map, associateTo
enables this by accepting a mutable Map and adding the collection items based on your transformer.
1 2 3 4 5 6 7 |
fun usingAssociateTo() { val list = listOf("a", "abc", "abcd") val ourMap: MutableMap<String, Int> = hashMapOf("ab" to 2, "bar" to 3) val mapName2Length: Map<String, Int> = list.associateTo(ourMap, { it to it.length }) println(mapName2Length) } |
Output:
1 2 |
{ab=2, a=1, bar=3, abc=3, abcd=4} |
5. Using groupByTo
In cases where you want to keep all values for a key, you can use groupByTo
, which maps each key to a list of values.
1 2 3 4 5 6 7 |
fun usingGroupByTo() { val list = listOf("a", "abc", "abcd", "gde") val ourMap: MutableMap<Int, MutableList> = HashMap() val mapName2Length: Map<Int, MutableList> = list.groupByTo(ourMap, { it.length }) println(mapName2Length) } |
Output:
1 2 |
{1=[a], 3=[abc, gde], 4=[abcd]} |
You can use groupBy
if you don’t need a mutable Map.
1 2 3 4 5 6 |
fun usingGroupBy() { val list = listOf("a", "abc", "abcd", "gde") val mapName2Length: Map<Int, List> = list.groupBy { it.length } println(mapName2Length) } |
Output:
1 2 |
{1=[a], 3=[abc, gde], 4=[abcd]} |
Summary
In this tutorial, we examined various methods for converting a List to a Map in Kotlin. These techniques should prove helpful in learning Kotlin basics. Feel free to share your suggestions in the comments.