inline fun <K, V, M : MutableMap<in K, in V>> Sequence<K>.associateWithTo(
    destination: M,
    valueSelector: (K) -> V
): M


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

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

The operation is terminal.

import kotlin.test.*

fun main(args: Array<String>) {
data class Person(val firstName: String, val lastName: String) {
    override fun toString(): String = "$firstName $lastName"

val scientists = listOf(Person("Grace", "Hopper"), Person("Jacob", "Bernoulli"), Person("Jacob", "Bernoulli"))
val withLengthOfNames = mutableMapOf<Person, Int>()
println("withLengthOfNames.isEmpty() is ${withLengthOfNames.isEmpty()}") // true

scientists.associateWithTo(withLengthOfNames) { it.firstName.length + it.lastName.length }

println("withLengthOfNames.isNotEmpty() is ${withLengthOfNames.isNotEmpty()}") // true
// Jacob Bernoulli only occurs once in the map because only the last pair with the same key gets added
println(withLengthOfNames) // {Grace Hopper=11, Jacob Bernoulli=14}