associateTo
Требования к платформе и версии: JVM (1.0), JS (1.0), Native (1.0)
inline fun <T, K, V, M : MutableMap<in K, in V>> Array<out T>.associateTo( destination: M, transform: (T) -> Pair<K, V> ): M
inline fun <K, V, M : MutableMap<in K, in V>> ByteArray.associateTo( destination: M, transform: (Byte) -> Pair<K, V> ): M
inline fun <K, V, M : MutableMap<in K, in V>> ShortArray.associateTo( destination: M, transform: (Short) -> Pair<K, V> ): M
inline fun <K, V, M : MutableMap<in K, in V>> IntArray.associateTo( destination: M, transform: (Int) -> Pair<K, V> ): M
inline fun <K, V, M : MutableMap<in K, in V>> LongArray.associateTo( destination: M, transform: (Long) -> Pair<K, V> ): M
inline fun <K, V, M : MutableMap<in K, in V>> FloatArray.associateTo( destination: M, transform: (Float) -> Pair<K, V> ): M
inline fun <K, V, M : MutableMap<in K, in V>> DoubleArray.associateTo( destination: M, transform: (Double) -> Pair<K, V> ): M
inline fun <K, V, M : MutableMap<in K, in V>> BooleanArray.associateTo( destination: M, transform: (Boolean) -> Pair<K, V> ): M
inline fun <K, V, M : MutableMap<in K, in V>> CharArray.associateTo( destination: M, transform: (Char) -> Pair<K, V> ): M
Заполняет и возвращает изменяемую карту destination парами ключ-значение, предоставленными функцией transform, примененной к каждому элементу заданного массива.
Если две пары имеют одинаковый ключ, в карту добавляется последняя пара.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val charCodes = intArrayOf(72, 69, 76, 76, 79)
val byChar = mutableMapOf<Int, Char>()
charCodes.associateTo(byChar) { it to it.toChar() }
// 76=L only occurs once because only the last pair with the same key gets added
println(byChar) // {72=H, 69=E, 76=L, 79=O}
//sampleEnd
}
Требования к платформе и версии: JVM (1.0), JS (1.0), Native (1.0)
inline fun <T, K, V, M : MutableMap<in K, in V>> Iterable<T>.associateTo( destination: M, transform: (T) -> Pair<K, V> ): M
Заполняет и возвращает изменяемую карту destination парами ключ-значение, предоставленными функцией transform, примененной к каждому элементу заданного набора.
Если две пары имеют одинаковый ключ, в карту добавляется последняя пара.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
data class Person(val firstName: String, val lastName: String)
val scientists = listOf(Person("Grace", "Hopper"), Person("Jacob", "Bernoulli"), Person("Johann", "Bernoulli"))
val byLastName = mutableMapOf<String, String>()
println("byLastName.isEmpty() is ${byLastName.isEmpty()}") // true
scientists.associateTo(byLastName) { it.lastName to it.firstName }
println("byLastName.isNotEmpty() is ${byLastName.isNotEmpty()}") // true
// 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.collections/associate-to.html