合并 map kotlin 中的值
我需要将地图和成对的“名称” - “电话号码”合并到最终地图中,将重复键的值粘在一起,用逗号分隔。重复值只应添加一次。就语言方法而言,我需要最成语和最正确的方法。mapA
mapB
例如:
val mapA = mapOf("Emergency" to "112", "Fire department" to "101", "Police" to "102")
val mapB = mapOf("Emergency" to "911", "Police" to "102")
最终结果应如下所示:
{"Emergency" to "112, 911", "Fire department" to "101", "Police" to "102"}
这是我的函数:
fun mergePhoneBooks(mapA: Map<String, String>, mapB: Map<String, String>): Map<String, String> {
val unionList: MutableMap <String, String> = mapA.toMutableMap()
unionList.forEach { (key, value) -> TODO() } // here's I can't come on with a beatiful solution
return unionList
}