associateWithTo

inline fun <V, M : MutableMap<in Char, in V>> CharSequence.associateWithTo(destination: M, valueSelector: (Char) -> V): M(source)

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.

Since Kotlin

1.3

Samples

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

fun main() { 
   //sampleStart 
   val string = "bonne journée"
// associate each character with its code
val result = mutableMapOf<Char, Int>()
string.associateWithTo(result) { 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
}