associate

Common
JVM
JS
Native
1.0
inline fun <K, V> CharSequence.associate(
    transform: (Char) -> Pair<K, V>
): Map<K, V>

Returns a Map containing key-value pairs provided by transform function applied to characters of the given char sequence.

If any of two pairs would have the same key the last one gets added to the map.

The returned map preserves the entry iteration order of the original char sequence.

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.toInt() }
// 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
}