Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kotlin merge two maps by overwriting values

Tags:

kotlin

If I have the below two maps:

val x = mapOf("a" to 10, "b" to 5)
val y = mapOf("a" to 4)
val result = //

How can I get the result to be: {a=4, b=5}? I want the value to be overwritten if the key exists.

like image 633
max_mal Avatar asked Oct 08 '20 22:10

max_mal


1 Answers

val result = x + y does exactly what you need, the second parameter (y) will overwrite the values with the same keys from the first parameter (x)

like image 178
IR42 Avatar answered Nov 04 '22 10:11

IR42