associate With To
inline fun <K, V, M : MutableMap<in K, in V>> Array<out K>.associateWithTo(destination: M, valueSelector: (K) -> V): M
Content copied to clipboard
inline fun <V, M : MutableMap<in Byte, in V>> ByteArray.associateWithTo(destination: M, valueSelector: (Byte) -> V): M
Content copied to clipboard
inline fun <V, M : MutableMap<in Short, in V>> ShortArray.associateWithTo(destination: M, valueSelector: (Short) -> V): M
Content copied to clipboard
inline fun <V, M : MutableMap<in Int, in V>> IntArray.associateWithTo(destination: M, valueSelector: (Int) -> V): M
Content copied to clipboard
inline fun <V, M : MutableMap<in Long, in V>> LongArray.associateWithTo(destination: M, valueSelector: (Long) -> V): M
Content copied to clipboard
inline fun <V, M : MutableMap<in Float, in V>> FloatArray.associateWithTo(destination: M, valueSelector: (Float) -> V): M
Content copied to clipboard
inline fun <V, M : MutableMap<in Double, in V>> DoubleArray.associateWithTo(destination: M, valueSelector: (Double) -> V): M
Content copied to clipboard
inline fun <V, M : MutableMap<in Boolean, in V>> BooleanArray.associateWithTo(destination: M, valueSelector: (Boolean) -> V): M
Content copied to clipboard
inline fun <V, M : MutableMap<in Char, in V>> CharArray.associateWithTo(destination: M, valueSelector: (Char) -> V): M
Content copied to clipboard
inline fun <V, M : MutableMap<in UInt, in V>> UIntArray.associateWithTo(destination: M, valueSelector: (UInt) -> V): M
Content copied to clipboard
inline fun <V, M : MutableMap<in ULong, in V>> ULongArray.associateWithTo(destination: M, valueSelector: (ULong) -> V): M
Content copied to clipboard
inline fun <V, M : MutableMap<in UByte, in V>> UByteArray.associateWithTo(destination: M, valueSelector: (UByte) -> V): M
Content copied to clipboard
inline fun <V, M : MutableMap<in UShort, in V>> UShortArray.associateWithTo(destination: M, valueSelector: (UShort) -> V): M
Content copied to clipboard
Populates and returns the destination mutable map with key-value pairs for each element of the given array, 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.
Since Kotlin
1.4
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
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>()
assertTrue(withLengthOfNames.isEmpty())
scientists.associateWithTo(withLengthOfNames) { it.firstName.length + it.lastName.length }
assertTrue(withLengthOfNames.isNotEmpty())
// Jacob Bernoulli only occurs once in the map because only the last pair with the same key gets added
assertPrints(withLengthOfNames, "{Grace Hopper=11, Jacob Bernoulli=14}")
//sampleEnd
}
inline fun <K, V, M : MutableMap<in K, in V>> Iterable<K>.associateWithTo(destination: M, valueSelector: (K) -> V): M
Content copied to clipboard
Populates and returns the destination mutable map with key-value pairs for each element of the given collection, 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.
Since Kotlin
1.3
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
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>()
assertTrue(withLengthOfNames.isEmpty())
scientists.associateWithTo(withLengthOfNames) { it.firstName.length + it.lastName.length }
assertTrue(withLengthOfNames.isNotEmpty())
// Jacob Bernoulli only occurs once in the map because only the last pair with the same key gets added
assertPrints(withLengthOfNames, "{Grace Hopper=11, Jacob Bernoulli=14}")
//sampleEnd
}