Merge two maps in Kotlin
This article explores different ways to merge two maps in Kotlin.
1. Using + operator
A simple and fairly efficient solution to merge two maps in Kotlin is using the + operator. It can be used as follows in Kotlin:
|
1 2 3 4 5 6 7 8 |
fun main() { val first: Map<Int, Char> = mapOf(Pair(1, 'A'), Pair(2, 'B')) val second: Map<Int, Char> = mapOf(Pair(3, 'C'), Pair(4, 'D')) val result: Map<Int, Char> = first + second println(result) // {1=A, 2=B, 3=C, 4=D} } |
Note that for any common keys present in the maps, the latter overrides the former’s value. We can also use the plus() function to merge the contents of the specified map to the caller.
|
1 2 3 4 5 6 7 |
fun main() { val first: Map<Int, Char> = mapOf(Pair(1, 'A'), Pair(2, 'B')) val second: Map<Int, Char> = mapOf(Pair(2, 'C'), Pair(3, 'D')) val result: Map<Int, Char> = first.plus(second) println(result) // {1=A, 2=C, 3=D} } |
2. Using groupBy() function
Another option is to get the sequence of all mappings contained in both maps, and then group values by the key to get a map where each group key is associated with a list of corresponding values. This is demonstrated below using the groupBy() function:
|
1 2 3 4 5 6 7 8 9 10 |
fun main() { val first: Map<Int, Char> = mapOf(Pair(1, 'A'), Pair(2, 'B')) val second: Map<Int, Char> = mapOf(Pair(2, 'C'), Pair(3, 'D')) val result: Map<Int, String> = (first.asSequence() + second.asSequence()) .groupBy({ it.key }, { it.value }) .mapValues { (_, values) -> values.joinToString(", ", "[", "]") } println(result) // {1=[A], 2=[B, C], 3=[D]} } |
3. Using associateWith() function
Another option is to collect all keys present in both maps and then associate each key with the set of values using the associateWith() function. Here’s complete usage of this function:
|
1 2 3 4 5 6 7 8 9 10 |
fun main() { val first: Map<Int, Char> = mapOf(Pair(1, 'A'), Pair(2, 'B')) val second: Map<Int, Char> = mapOf(Pair(2, 'C'), Pair(3, 'D')) val result: Map<Int, String> = (first.keys + second.keys).associateWith { setOf(first[it], second[it]).filterNotNull().joinToString(", ", "[", "]") } println(result) // {1=[A], 2=[B, C], 3=[D]} } |
The above solution results in a string of comma-separated values enclosed within []. To get a proper list of values, do like:
|
1 2 3 4 5 6 7 8 9 |
fun main() { val first: Map<Int, Char> = mapOf(Pair(1, 'A'), Pair(2, 'B')) val second: Map<Int, Char> = mapOf(Pair(2, 'C'), Pair(3, 'D')) val result: Map<Int, List<Char>> = (first.keys.asSequence() + second.keys) .associateWith { setOf(first[it], second[it]).filterNotNull().toList() } println(result) // {1=[A], 2=[B, C], 3=[D]} } |
4. Using apply() function
Alternatively, you can use scope functions to merge two maps. This can be implemented as follows in Kotlin using the apply() function:
|
1 2 3 4 5 6 7 8 9 10 11 |
fun main() { val first: Map<Int, Char> = mapOf(Pair(1, 'A'), Pair(2, 'B')) val second: Map<Int, Char> = mapOf(Pair(2, 'C'), Pair(3, 'D')) val result = first.toMutableMap() .apply { second.forEach { (k, v) -> merge(k, v) { oldVal, newVal -> oldVal } } } println(result) // {1=A, 2=B, 3=D} } |
5. Using putAll() function
Finally, we can use the putAll() function to copy all entries from a map to another map. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 |
fun main() { val first: MutableMap<Int, Char> = mutableMapOf(Pair(1, 'A'), Pair(2, 'B')) val second: MutableMap<Int, Char> = mutableMapOf(Pair(3, 'C'), Pair(4, 'D')) val map: MutableMap<Int, Char> = mutableMapOf() map.putAll(first) map.putAll(second) println(map) // {1=A, 2=B, 3=C, 4=D} } |
Note that if a key exists in both maps, the value in the second map will overwrite the value in the first map:
|
1 2 3 4 5 6 7 8 9 10 |
fun main() { val first: MutableMap<Int, Char> = mutableMapOf(Pair(1, 'A'), Pair(2, 'B')) val second: MutableMap<Int, Char> = mutableMapOf(Pair(2, 'C'), Pair(3, 'D')) val map: MutableMap<Int, Char> = mutableMapOf() map.putAll(first) map.putAll(second) println(map) // {1=A, 2=D, 3=C} } |
We can avoid making an extra call to the putAll() function, as follows:
|
1 2 |
val map: MutableMap<Int, Char> = first.toMutableMap() map.putAll(second) |
That’s all about merging two maps 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 :)