associateWith所在位置是kotlin.text.associateWith,其相關用法介紹如下。

用法:

inline fun <V> CharSequence.associateWith(
    valueSelector: (Char) -> V
): Map<Char, V>

返回一個Map,其中鍵是給定字符序列中的字符,值由應用於每個字符的valueSelector 函數生成。

如果任何兩個字符相等,則將最後一個字符添加到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.associateWith { char -> 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}