Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)
inline fun <V, M : MutableMap<in Char, in V>> CharSequence.associateWithTo(
    destination: M, 
    valueSelector: (Char) -> V
): M

Populates and returns the destination mutable map with key-value pairs for each character of the given char sequence, where key is the character itself and value is provided by the valueSelector function applied to that key.

If any two characters are equal, the last one overwrites the former value in the map.

import kotlin.test.*

fun main(args: Array<String>) {
val string = "bonne journée"
// associate each character with its code
val result = mutableMapOf<Char, Int>()
string.associateWithTo(result) { 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–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.