Convert a List of Pairs to a Map in Kotlin
This article explores different ways to convert a List<Pair<K,V>>
to a Map<K,V>
in Kotlin.
1. Using associate()
function
The associate()
function returns a map containing key-value pairs provided by the transform function applied to elements of the given map. It can be used as follows to convert a List<Pair<K,V>>
to a Map<K,V>
in Kotlin:
1 2 3 4 5 6 7 8 9 |
class Point(var x: Int, var y: Int) fun main() { val points = listOf(Point(0, 1), Point(1, 2), Point(1, 1), Point(2, 3)) val map = points.associate { Pair(it.x, it.y) } println(map) // {0=1, 1=1, 2=3} } |
Alternatively, we can use the following shorthand form:
1 2 3 4 5 6 7 8 9 |
class Point(var x: Int, var y: Int) fun main() { val points = listOf(Point(0, 1), Point(1, 2), Point(1, 1), Point(2, 3)) val map = points.associate { it.x to it.y } println(map) // {0=1, 1=1, 2=3} } |
2. Using associateBy()
function
The associateBy()
function returns a map containing the values provided by valueTransform
and indexed by keySelector
functions applied to elements of the given map. It can be used as follows to convert a List<Pair<K,V>>
to a Map<K,V>
in Kotlin:
1 2 3 4 5 6 7 8 9 |
class Point(var x: Int, var y: Int) fun main() { val points = listOf(Point(0, 1), Point(1, 2), Point(1, 1), Point(2, 3)) val map = points.associateBy({ it.x }, { it.y }) println(map) // {0=1, 1=1, 2=3} } |
The above code is short for the following:
1 2 3 4 5 6 7 8 9 10 |
class Point(var x: Int, var y: Int) fun main() { val points = listOf(Point(0, 1), Point(1, 2), Point(1, 1), Point(2, 3)) val map = points.associateBy(keySelector = { point -> point.x }, valueTransform = { point -> point.y }) println(map) // {0=1, 1=1, 2=3} } |
3. Using map()
function
Another option is to use the map()
library function to extract keys and values from the List of Pairs and uses the toMap()
function to build a map:
1 2 3 4 5 6 7 8 9 |
class Point(var x: Int, var y: Int) fun main() { val points = listOf(Point(0, 1), Point(1, 2), Point(1, 1), Point(2, 3)) val map = points.map { it.x to it.y }.toMap() println(map) // {0=1, 1=1, 2=3} } |
4. Using Loop
Finally, we can write custom logic for converting a list of pairs into a map. This can be implemented as follows in Kotlin. Note that the code throws IllegalStateException
on encountering a duplicate key. The code can be easily modified to handle duplicate keys.
1 2 3 4 5 6 7 8 9 10 11 12 |
class Point(var x: Int, var y: Int) fun main() { val points = listOf(Point(0, 1), Point(1, 2), Point(2, 3), Point(3, 4)) val map = mutableMapOf<Int, Int>() for (point in points) { check(map.put(point.x, point.y) == null) { "Duplicate key" } } println(map) // {0=1, 1=2, 2=3, 3=4} } |
That’s all about converting a List<Pair<K,V>>
to a Map<K,V>
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 :)