例如,我有一个字符串列表,如:
val list = listOf("a", "b", "c", "d")
我希望将其转换为地图,其中字符串是键。
我知道我应该使用.toMap()
功能,但我不知道怎么做,而且我还没有看到它的任何例子。
答案 0 :(得分:234)
您有两种选择:
第一个也是性能最好的是使用comments
函数,它接受两个lambdas来生成键和值,并内联地图的创建:
# DON'T DO THIS IN PRODUCTION
Comment.all.sort_by { |comment| comment.edit.keys.first }
第二个效果较差的是使用标准associateBy
函数创建val map = friends.associateBy({it.facebookId}, {it.points})
列表,map
可以使用该列表生成最终地图:
Pair
答案 1 :(得分:28)
List
到Map
,associate
功能使用Kotlin 1.3,List
有一个名为associate
的函数。 associate
有以下声明:
fun <T, K, V> Iterable<T>.associate(transform: (T) -> Pair<K, V>): Map<K, V>
返回
Map
包含应用于给定集合元素的transform
函数提供的键值对。
用法:的
class Person(val name: String, val id: Int)
fun main() {
val friends = listOf(Person("Sue Helen", 1), Person("JR", 2), Person("Pamela", 3))
val map = friends.associate({ Pair(it.id, it.name) })
//val map = friends.associate({ it.id to it.name }) // also works
println(map) // prints: {1=Sue Helen, 2=JR, 3=Pamela}
}
List
到Map
,associateBy
功能使用Kotlin,List
有一个名为associateBy
的函数。 associateBy
有以下声明:
fun <T, K, V> Iterable<T>.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map<K, V>
返回
Map
,其中包含valueTransform
提供的值,并由应用于给定集合元素的keySelector
函数编制索引。
用法:的
class Person(val name: String, val id: Int)
fun main() {
val friends = listOf(Person("Sue Helen", 1), Person("JR", 2), Person("Pamela", 3))
val map = friends.associateBy(keySelector = { person -> person.id }, valueTransform = { person -> person.name })
//val map = friends.associateBy({ it.id }, { it.name }) // also works
println(map) // prints: {1=Sue Helen, 2=JR, 3=Pamela}
}
答案 2 :(得分:6)
您可以使用associate
执行此任务:
val list = listOf("a", "b", "c", "d")
val m: Map<String, Int> = list.associate { it to it.length }
在此示例中,来自list
的字符串成为键,其对应的长度(作为示例)成为地图内的值。
答案 3 :(得分:5)
1-关联(同时设置键和值):构建可以设置键和值元素的地图:
IterableSequenceElements.associate { newKey to newValue } //Output => Map {newKey : newValue ,...}
如果两对中的任何一个具有相同的密钥,则最后一个将添加到地图中。
返回的映射保留原始数组的输入迭代顺序。
2- associateBy(通过计算仅设置键):建立一个可以设置新键的映射,将为值设置类似元素
IterableSequenceElements.associateBy { newKey } //Result: => Map {newKey : 'Values will be set from analogous IterableSequenceElements' ,...}
3- associateWith(只需通过计算设置值):建立一个可以设置新值的映射,将为键设置类似的元素
IterableSequenceElements.associateWith { newValue } //Result => Map { 'Keys will be set from analogous IterableSequenceElements' : newValue , ...}
答案 4 :(得分:2)
如果您不想丢失的列表中有重复项,则可以使用groupBy
进行操作。
否则,就像其他所有人一样,请使用associate/By/With
(我相信,如果重复,则只会返回该键的最后一个值)。
按年龄分组人员列表的示例:
class Person(val name: String, val age: Int)
fun main() {
val people = listOf(Person("Sue Helen", 31), Person("JR", 25), Person("Pamela", 31))
val duplicatesKept = people.groupBy { it.age }
val duplicatesLost = people.associateBy({ it.age }, { it })
println(duplicatesKept)
println(duplicatesLost)
}
结果:
{31=[Person@41629346, Person@4eec7777], 25=[Person@3b07d329]}
{31=Person@4eec7777, 25=Person@3b07d329}
答案 5 :(得分:0)
RC版本已经改变了。
我正在使用val map = list.groupByTo(destinationMap, {it.facebookId}, { it -> it.point })
答案 6 :(得分:-2)
例如,您有一个字符串列表,例如:
val list = listOf("a", "b", "c", "d")
,您需要将其转换为以字符串为键的地图。
有两种方法可以做到这一点:
第一个也是最有效的方法是使用associateBy函数,该函数需要两个lambda来生成键和值,并内联地图的创建:
val map = friends.associateBy({it.facebookId}, {it.points})
第二个性能较差的方法是使用标准map函数创建一个Pair列表,toMap可以使用它生成最终地图:
val map = friends.map { it.facebookId to it.points }.toMap()