Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)
inline fun <V> CharSequence.associateWith(
    valueSelector: (Char) -> V
): Map<Char, V>

Returns a Map where keys are characters from the given char sequence and values are produced by the valueSelector function applied to each character.

If any two characters are equal, 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>) {
val string = "bonne journée"
// associate each character with its code
val result = string.associateWith { char -> 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}

© 2010–2019 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.