associateWith

Common
JVM
JS
Native
1.4
inline fun <K, V> Array<out K>.associateWith(
    valueSelector: (K) -> V
): Map<K, V>

(source)
inline fun <V> ByteArray.associateWith(
    valueSelector: (Byte) -> V
): Map<Byte, V>

(source)
inline fun <V> ShortArray.associateWith(
    valueSelector: (Short) -> V
): Map<Short, V>

(source)
inline fun <V> IntArray.associateWith(
    valueSelector: (Int) -> V
): Map<Int, V>

(source)
inline fun <V> LongArray.associateWith(
    valueSelector: (Long) -> V
): Map<Long, V>

(source)
inline fun <V> FloatArray.associateWith(
    valueSelector: (Float) -> V
): Map<Float, V>

(source)
inline fun <V> DoubleArray.associateWith(
    valueSelector: (Double) -> V
): Map<Double, V>

(source)
inline fun <V> BooleanArray.associateWith(
    valueSelector: (Boolean) -> V
): Map<Boolean, V>

(source)
inline fun <V> CharArray.associateWith(
    valueSelector: (Char) -> V
): Map<Char, V>

(source)
@ExperimentalUnsignedTypes inline fun <V> UIntArray.associateWith(
    valueSelector: (UInt) -> V
): Map<UInt, V>

(source)
@ExperimentalUnsignedTypes inline fun <V> ULongArray.associateWith(
    valueSelector: (ULong) -> V
): Map<ULong, V>

(source)
@ExperimentalUnsignedTypes inline fun <V> UByteArray.associateWith(
    valueSelector: (UByte) -> V
): Map<UByte, V>

(source)
@ExperimentalUnsignedTypes inline fun <V> UShortArray.associateWith(
    valueSelector: (UShort) -> V
): Map<UShort, V>

(source)

Returns a Map where keys are elements from the given array and values are produced by the valueSelector function applied to each element.

If any two elements are equal, the last one gets added to the map.

The returned map preserves the entry iteration order of the original array.

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
}
Common
JVM
JS
Native
1.3
inline fun <K, V> Iterable<K>.associateWith(
    valueSelector: (K) -> V
): Map<K, V>

(source)

Returns a Map where keys are elements from the given collection and values are produced by the valueSelector function applied to each element.

If any two elements are equal, the last one gets added to the map.

The returned map preserves the entry iteration order of the original collection.

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
}