作者:玄天战猫 | 来源:互联网 | 2022-11-27 19:47
我需要在地图中散列所有值并返回相同类型的地图.
我的解决方案目前看起来非常不稳定:
val hashedPolicies = policyProperties.map { it.key to it.value.hash() }.toMap()
没有toMap()
,它返回一个List
不可接受的.
有没有更好的方法来创建这样的地图的新地图(不必使用.toMap()
)?
1> Moira..:
试试mapValues
:
inline fun Map.mapValues(
transform: (Entry) -> R
): Map
返回一个新映射,其中包含具有此映射关键字的条目以及通过将转换函数应用于此映射中的每个条目而获得的值.
返回的映射保留原始映射的条目迭代顺序.
kotlin-stdlib/kotlin.collections/mapValues
val test = mapOf("foo" to "bar")
println(test)
// {foo=bar}
val result = test.mapValues { it.value.hashCode() }
println(result)
// {foo=97299}