associateBy

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
inline fun <T, K> Array<out T>.associateBy(
    keySelector: (T) -> K
): Map<K, T>
inline fun <K> ByteArray.associateBy(
    keySelector: (Byte) -> K
): Map<K, Byte>
inline fun <K> ShortArray.associateBy(
    keySelector: (Short) -> K
): Map<K, Short>
inline fun <K> IntArray.associateBy(
    keySelector: (Int) -> K
): Map<K, Int>
inline fun <K> LongArray.associateBy(
    keySelector: (Long) -> K
): Map<K, Long>
inline fun <K> FloatArray.associateBy(
    keySelector: (Float) -> K
): Map<K, Float>
inline fun <K> DoubleArray.associateBy(
    keySelector: (Double) -> K
): Map<K, Double>
inline fun <K> BooleanArray.associateBy(
    keySelector: (Boolean) -> K
): Map<K, Boolean>
inline fun <K> CharArray.associateBy(
    keySelector: (Char) -> K
): Map<K, Char>

Returns a Map containing the elements from the given array indexed by the key returned from keySelector function applied to each element.

If any two elements would have the same key returned by keySelector 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 charCodes = intArrayOf(72, 69, 76, 76, 79)

val byChar = charCodes.associateBy { it.toChar() }

// L=76 only occurs once because only the last pair with the same key gets added
println(byChar) // {H=72, E=69, L=76, O=79}
//sampleEnd
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
inline fun <T, K, V> Array<out T>.associateBy(
    keySelector: (T) -> K, 
    valueTransform: (T) -> V
): Map<K, V>
inline fun <K, V> ByteArray.associateBy(
    keySelector: (Byte) -> K, 
    valueTransform: (Byte) -> V
): Map<K, V>
inline fun <K, V> ShortArray.associateBy(
    keySelector: (Short) -> K, 
    valueTransform: (Short) -> V
): Map<K, V>
inline fun <K, V> IntArray.associateBy(
    keySelector: (Int) -> K, 
    valueTransform: (Int) -> V
): Map<K, V>
inline fun <K, V> LongArray.associateBy(
    keySelector: (Long) -> K, 
    valueTransform: (Long) -> V
): Map<K, V>
inline fun <K, V> FloatArray.associateBy(
    keySelector: (Float) -> K, 
    valueTransform: (Float) -> V
): Map<K, V>
inline fun <K, V> DoubleArray.associateBy(
    keySelector: (Double) -> K, 
    valueTransform: (Double) -> V
): Map<K, V>
inline fun <K, V> BooleanArray.associateBy(
    keySelector: (Boolean) -> K, 
    valueTransform: (Boolean) -> V
): Map<K, V>
inline fun <K, V> CharArray.associateBy(
    keySelector: (Char) -> K, 
    valueTransform: (Char) -> V
): Map<K, V>

Returns a Map containing the values provided by valueTransform and indexed by keySelector functions applied to elements of the given array.

If any two elements would have the same key returned by keySelector 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 charCodes = intArrayOf(65, 65, 66, 67, 68, 69)

val byUpperCase = charCodes.associateBy({ it.toChar() }, { (it + 32).toChar() })

// A=a only occurs once because only the last pair with the same key gets added
println(byUpperCase) // {A=a, B=b, C=c, D=d, E=e}
//sampleEnd
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
inline fun <T, K> Iterable<T>.associateBy(
    keySelector: (T) -> K
): Map<K, T>

Returns a Map containing the elements from the given collection indexed by the key returned from keySelector function applied to each element.

If any two elements would have the same key returned by keySelector 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
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("Johann", "Bernoulli"))

val byLastName = scientists.associateBy { it.lastName }

// Jacob Bernoulli does not occur in the map because only the last pair with the same key gets added
println(byLastName) // {Hopper=Grace Hopper, Bernoulli=Johann Bernoulli}
//sampleEnd
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
inline fun <T, K, V> Iterable<T>.associateBy(
    keySelector: (T) -> K, 
    valueTransform: (T) -> V
): Map<K, V>

Returns a Map containing the values provided by valueTransform and indexed by keySelector functions applied to elements of the given collection.

If any two elements would have the same key returned by keySelector 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
data class Person(val firstName: String, val lastName: String)

val scientists = listOf(Person("Grace", "Hopper"), Person("Jacob", "Bernoulli"), Person("Johann", "Bernoulli"))

val byLastName = scientists.associateBy({ it.lastName }, { it.firstName })

// 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-by.html