associate所在位置是kotlin.sequences.associate,其相关用法介绍如下。

用法:

inline fun <T, K, V> Sequence<T>.associate(
    transform: (T) -> Pair<K, V>
): Map<K, V>

返回一个Map,其中包含由transform 函数提供的键值对,该函数应用于给定序列的元素。

如果两对中的任何一对具有相同的 key ,则最后一个将添加到Map中。

返回的映射保留原始序列的条目迭代顺序。

操作是 terminal

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val names = listOf("Grace Hopper", "Jacob Bernoulli", "Johann Bernoulli")

val byLastName = names.associate { it.split(" ").let { (firstName, lastName) -> lastName to firstName } }

// Jacob Bernoulli does not occur in the map because only the last pair with the same key gets added
println(byLastName) // {Hopper=Grace, Bernoulli=Johann}
//sampleEnd
}

输出:

{Hopper=Grace, Bernoulli=Johann}