在kotlin中转换地图的习惯方法?

在斯卡拉,这只是map功能。 例如,如果hashMap是一个字符串的hashMap,那么你可以执行以下操作:

 val result : HashMap[String,String] = hashMap.map(case(k,v) => (k -> v.toUpperCase)) 

但是,在Kotlin中, map将地图变成了一个列表。 在Kotlin做同样的事情是否有一种惯用的方式?

我认为一个人的意见不是惯用的,但我可能会用

 // transform keys only (use same values) hashMap.mapKeys { it.key.toUpperCase() } // transform values only (use same key) - what you're after! hashMap.mapValues { it.value.toUpperCase() } // transform keys + values hashMap.entries.associate { it.key.toUpperCase() to it.value.toUpperCase() } 

你可以使用其他人提出的stdlib mapValues函数 :

 hashMap.mapValues { it.value.toUpperCase() } 

我相信这是最习惯的方式。