Merge values in map kotlin

前端 未结 11 1520
慢半拍i
慢半拍i 2021-02-18 23:50

I need merge maps mapA andmapB with pairs of \"name\" - \"phone number\" into the final map, sticking together the values for duplicate keys, separated

11条回答
  •  后悔当初
    2021-02-19 00:19

        val mapA = mapOf("Emergency" to "112", "Fire department" to "101", "Police" to "102")
        val mapB = mapOf("Emergency" to "911", "Police" to "102")
    
        val result = (mapA.entries + mapB.entries)
            .groupBy({ it.key }, { it.value })
            .mapValues {(_, value) -> 
                value.joinToString(", ")
            }
    

提交回复
热议问题