inline fun <K, V> Sequence<K>.associateWith( valueSelector: (K) -> V ): Map<K, V>
Returns a Map where keys are elements from the given sequence and values are produced by the valueSelector function applied to each element.
If any two elements are equal, the last one gets added to the map.
The returned map preserves the entry iteration order of the original sequence.
The operation is terminal.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val words = listOf("a", "abc", "ab", "def", "abcd")
val withLength = words.associateWith { it.length }
println(withLength.keys) // [a, abc, ab, def, abcd]
println(withLength.values) // [1, 3, 2, 3, 4]
//sampleEnd
}
© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/associate-with.html