associateWith
inline fun <K, V> Array<out K>.associateWith( valueSelector: (K) -> V ): Map<K, V>
inline fun <V> ByteArray.associateWith( valueSelector: (Byte) -> V ): Map<Byte, V>
inline fun <V> ShortArray.associateWith( valueSelector: (Short) -> V ): Map<Short, V>
inline fun <V> IntArray.associateWith( valueSelector: (Int) -> V ): Map<Int, V>
inline fun <V> LongArray.associateWith( valueSelector: (Long) -> V ): Map<Long, V>
inline fun <V> FloatArray.associateWith( valueSelector: (Float) -> V ): Map<Float, V>
inline fun <V> DoubleArray.associateWith( valueSelector: (Double) -> V ): Map<Double, V>
inline fun <V> BooleanArray.associateWith( valueSelector: (Boolean) -> V ): Map<Boolean, V>
inline fun <V> CharArray.associateWith( valueSelector: (Char) -> V ): Map<Char, V>
@ExperimentalUnsignedTypes inline fun <V> UIntArray.associateWith( valueSelector: (UInt) -> V ): Map<UInt, V>
@ExperimentalUnsignedTypes inline fun <V> ULongArray.associateWith( valueSelector: (ULong) -> V ): Map<ULong, V>
@ExperimentalUnsignedTypes inline fun <V> UByteArray.associateWith( valueSelector: (UByte) -> V ): Map<UByte, V>
@ExperimentalUnsignedTypes inline fun <V> UShortArray.associateWith( valueSelector: (UShort) -> V ): Map<UShort, V>
Возвращает Map, где ключи — элементы из заданного массива, а значения — результаты применения функции valueSelector к каждому элементу.
Если какие-либо два элемента равны, в карту добавляется последний.
Возвращаемая карта сохраняет порядок итерации записей из исходного массива.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val words = listOf("a", "abc", "ab", "def", "abcd")
val withLength = words.associateWith { it.length }
println(withLength.keys) // [a, abc, ab, def, abcd]
println(withLength.values) // [1, 3, 2, 3, 4]
//sampleEnd
}inline fun <K, V> Iterable<K>.associateWith( valueSelector: (K) -> V ): Map<K, V>
Возвращает Map, где ключи — элементы из заданного множества, а значения — результаты применения функции valueSelector к каждому элементу.
Если какие-либо два элемента равны, в карту добавляется последний.
Возвращаемая карта сохраняет порядок итерации записей из исходного множества.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val words = listOf("a", "abc", "ab", "def", "abcd")
val withLength = words.associateWith { it.length }
println(withLength.keys) // [a, abc, ab, def, abcd]
println(withLength.values) // [1, 3, 2, 3, 4]
//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-with.html