Map.mapTo到另一个地图
我想映射Map<DAO, Int>
到Map<String, Boolean>
但是我不能在map函数中返回Map.Entry:
itemsWithQuantity.mapTo(mutableMapOf<String, Boolean>(), { it.key.toString() to it.value != 0 })
(当然我使用的是更复杂的映射函数,但是没关系,问题是一样的)
它说
MutableMap<String, Boolean> is not a subtype of MutableCollection<Pair<String, Boolean>>.
那么我怎样才能返回Map.Entry而不是Pair?
现在我正在这样做:
val detailsIds = mutableMapOf<String, Boolean>() itemsWithQuantity.forEach { item, quantity -> detailsIds.put(it.key.toString(), it.value != 0) }
但是我想用mapTo
使用associateTo
来代替:
xs.associateTo(mutableMapOf<String, Boolean>(), { "${it.key}" to (it.value != 0) })
另外,请注意it.value != 0
的括号。
mapTo
函数与map
类似,不会将结果收集到Map
,而是与Collection
,期望您提供MutableCollection<Pair<String, Boolean>>
。
- 在Kotlin中是否有像#region#endregion的语法?
- 尽管有效的文件路径,ThumbnailUtils.createVideoThumbnail崩溃
- 用于创建json对象的Kotlin DSL(不会创建垃圾)
- 应用程序图标不显示在Android版本大于4.4.2的设备上
- Int :: class.javaPrimitiveType.kotlin引用不等于Int :: class.javaObjectType.kotlin
- 像动画一样缓慢地在画布上画一条线
- ojAlgo – 在优化中将变量表示为边界?
- 为什么我不能在typealias上使用@PublishedApi?
- 是否有可能限制一个列表的类型为Kotlin接口的实现?