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

用法:

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

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

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

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

例子:

import java.util.Locale
import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val string = "bonne journée"
// associate each character with its code
val result = string.associate { char -> char to char.code }
// notice each letter occurs only once
println(result) // {b=98, o=111, n=110, e=101,  =32, j=106, u=117, r=114, é=233}
//sampleEnd
}

输出:

{b=98, o=111, n=110, e=101,  =32, j=106, u=117, r=114, é=233}