inline fun <T, K, V> Sequence<T>.associate( transform: (T) -> Pair<K, V> ): Map<K, V>
Returns a Map containing key-value pairs provided by transform function applied to elements of the given 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 sequence.
The operation is terminal.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val names = listOf("Grace Hopper", "Jacob Bernoulli", "Johann Bernoulli")
val byLastName = names.associate { it.split(" ").let { (firstName, lastName) -> lastName to firstName } }
// Jacob Bernoulli does not occur in the map because only the last pair with the same key gets added
println(byLastName) // {Hopper=Grace, Bernoulli=Johann}
//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.html