associate
inline suspend fun <T, K, V> Flow<T>.associate(crossinline transform: suspend (T) -> Pair<K, V>): Map<K, V>(source)
Collects this Flow into a Map with the key-value pairs provided by the transform function applied to each element.
If the same key appears in more than one pair, the last one gets added to the map.
The entry iteration order of the resulting Map is the order of the elements in the original Flow.
The operation is terminal.
val names = flowOf("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}Content copied to clipboard