Add all entries of a Map to given Map in Kotlin
This article explores different ways to put all entries of a Map to another Map in Kotlin.
1. Using putIfAbsent() function
The putIfAbsent() function is used to associate the specified key with a value if the key is not present in the map. The idea is to iterate over each entry of the second map and invoke the putIfAbsent() function on the first map for each mapping of the second map.
|
1 2 3 4 5 6 7 |
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')) second.forEach { (k, v) -> first.putIfAbsent(k, v) } println(first) } |
Output:
{1=A, 2=B, 3=C, 4=D}
This effectively copies all mappings of the second map to the first map and does not replace the existing mappings in the first map. For instance,
|
1 2 3 4 5 6 7 |
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')) second.forEach { (k, v) -> first.putIfAbsent(k, v) } println(first) } |
Output:
{1=A, 2=B, 3=D}
2. Using merge() function
A better alternative is to use the merge() function to associate the existing key with the old value or the new value. It takes the remapping function to recompute the value if already present.
The following code demonstrates its usage with a remapping function, where the new value takes precedence over the old value if the key is already present:
|
1 2 3 4 5 6 7 8 9 |
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')) for ((k, v) in second) { first.merge(k, v) { oldval, newval -> newval } } println(first) } |
Output:
{1=A, 2=C, 3=D}
We can shorten the code using forEach() to iterate over each mapping in the map.
|
1 2 3 4 5 6 7 |
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')) second.forEach { (k, v) -> first.merge(k, v) { oldval, newval -> newval } } println(first) } |
Output:
{1=A, 2=C, 3=D}
3. Using containsKey() function
We can also iterate over each entry in the second map and add the mapping if and only if the key is not already present in the first map.
|
1 2 3 4 5 6 7 8 9 10 11 |
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')) second.forEach { (key: Int, value: Char) -> if (!first.containsKey(key)) { first[key] = value } } println(first) } |
Output:
{1=A, 2=B, 3=D}
4. Using + operator
To create a new map that is the combination of both maps, we can use the + operator or the plus() function. It replaces the existing mappings in the first map from the mappings of the second map. For instance,
|
1 2 3 4 5 6 7 |
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 merged = first + second println(merged) } |
Output:
{1=A, 2=C, 3=D}
5. Using groupBy() function
The idea is to create a sequence of both maps entries and group them by key using the groupBy() function. Finally, map their values to comma-separated strings, resulting in a multimap-like structure.
|
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 merged = (first.asSequence() + second.asSequence()) .groupBy({ it.key }, { it.value }) .mapValues { (_, values) -> values.joinToString(", ", "[", "]") } println(merged) } |
Output:
{1=[A], 2=[B, C], 3=[D]}
Alternatively, we can use the associateWith() function as follows:
|
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 merged = (first.keys + second.keys) .associateWith { setOf(first[it], second[it]).filterNotNull() .joinToString(", ", "[", "]") } println(merged) } |
Output:
{1=[A], 2=[B, C], 3=[D]}
That’s all about putting all Map’s entries to another 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 :)