我正在尝试找出如何有效地将一个Map拆分为两个Map。原始Map看起来如下所示,但要大得多:
val sourceMap = <Map<String, List<Item>> = mapOf(
"key1", listOf(Item(owned: Boolean = true), Item(owned: Boolean = false)),
"key2", listOf(Item(owned: Boolean = false), Item(owned: Boolean = false)),
"key3", listOf(Item(owned: Boolean = true), Item(owned: Boolean = true))
)
我想把这个Map分成两个Map,在一个Map中把键和所有“owned = true”项关联起来,在另一个Map中把键和“owned = false”项关联起来。
val ownedMap = Map<String, List<Item>> = mapOf(
"key1", listOf(Item(owned: Boolean = true)),
"key3", listOf(Item(owned: Boolean = true), Item(owned: Boolean = true))
)
val unOwnedMap = Map<String, List<Item>> = mapOf(
"key1", listOf(Item(owned: Boolean = false)),
"key2", listOf(Item(owned: Boolean = false), Item(owned: Boolean = false)),
)
编辑:理想情况下,答案将允许所有排序在原始sourceMap
的一次传递中发生。
1条答案
按热度按时间sg3maiej1#
以下代码可能会有所帮助:
你也可以不使用扩展函数来编写代码,这样看起来就像这样: