groupByTo

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
inline fun <T, K, M : MutableMap<in K, MutableList<T>>> Array<out T>.groupByTo(
    destination: M, 
    keySelector: (T) -> K
): M
inline fun <K, M : MutableMap<in K, MutableList<Byte>>> ByteArray.groupByTo(
    destination: M, 
    keySelector: (Byte) -> K
): M
inline fun <K, M : MutableMap<in K, MutableList<Short>>> ShortArray.groupByTo(
    destination: M, 
    keySelector: (Short) -> K
): M
inline fun <K, M : MutableMap<in K, MutableList<Int>>> IntArray.groupByTo(
    destination: M, 
    keySelector: (Int) -> K
): M
inline fun <K, M : MutableMap<in K, MutableList<Long>>> LongArray.groupByTo(
    destination: M, 
    keySelector: (Long) -> K
): M
inline fun <K, M : MutableMap<in K, MutableList<Float>>> FloatArray.groupByTo(
    destination: M, 
    keySelector: (Float) -> K
): M
inline fun <K, M : MutableMap<in K, MutableList<Double>>> DoubleArray.groupByTo(
    destination: M, 
    keySelector: (Double) -> K
): M
inline fun <K, M : MutableMap<in K, MutableList<Boolean>>> BooleanArray.groupByTo(
    destination: M, 
    keySelector: (Boolean) -> K
): M
inline fun <K, M : MutableMap<in K, MutableList<Char>>> CharArray.groupByTo(
    destination: M, 
    keySelector: (Char) -> K
): M
@ExperimentalUnsignedTypes inline fun <K, M : MutableMap<in K, MutableList<UInt>>> UIntArray.groupByTo(
    destination: M, 
    keySelector: (UInt) -> K
): M
@ExperimentalUnsignedTypes inline fun <K, M : MutableMap<in K, MutableList<ULong>>> ULongArray.groupByTo(
    destination: M, 
    keySelector: (ULong) -> K
): M
@ExperimentalUnsignedTypes inline fun <K, M : MutableMap<in K, MutableList<UByte>>> UByteArray.groupByTo(
    destination: M, 
    keySelector: (UByte) -> K
): M
@ExperimentalUnsignedTypes inline fun <K, M : MutableMap<in K, MutableList<UShort>>> UShortArray.groupByTo(
    destination: M, 
    keySelector: (UShort) -> K
): M

Groups elements of the original array by the key returned by the given keySelector function applied to each element and puts to the destination map each group key associated with a list of corresponding elements.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val words = listOf("a", "abc", "ab", "def", "abcd")
val byLength = words.groupBy { it.length }

println(byLength.keys) // [1, 3, 2, 4]
println(byLength.values) // [[a], [abc, def], [ab], [abcd]]

val mutableByLength: MutableMap<Int, MutableList<String>> = words.groupByTo(mutableMapOf()) { it.length }
// same content as in byLength map, but the map is mutable
println("mutableByLength == byLength is ${mutableByLength == byLength}") // true
//sampleEnd
}

Return The destination map.

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
inline fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Array<out T>.groupByTo(
    destination: M, 
    keySelector: (T) -> K, 
    valueTransform: (T) -> V
): M
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> ByteArray.groupByTo(
    destination: M, 
    keySelector: (Byte) -> K, 
    valueTransform: (Byte) -> V
): M
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> ShortArray.groupByTo(
    destination: M, 
    keySelector: (Short) -> K, 
    valueTransform: (Short) -> V
): M
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> IntArray.groupByTo(
    destination: M, 
    keySelector: (Int) -> K, 
    valueTransform: (Int) -> V
): M
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> LongArray.groupByTo(
    destination: M, 
    keySelector: (Long) -> K, 
    valueTransform: (Long) -> V
): M
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> FloatArray.groupByTo(
    destination: M, 
    keySelector: (Float) -> K, 
    valueTransform: (Float) -> V
): M
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> DoubleArray.groupByTo(
    destination: M, 
    keySelector: (Double) -> K, 
    valueTransform: (Double) -> V
): M
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> BooleanArray.groupByTo(
    destination: M, 
    keySelector: (Boolean) -> K, 
    valueTransform: (Boolean) -> V
): M
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> CharArray.groupByTo(
    destination: M, 
    keySelector: (Char) -> K, 
    valueTransform: (Char) -> V
): M
@ExperimentalUnsignedTypes inline fun <K, V, M : MutableMap<in K, MutableList<V>>> UIntArray.groupByTo(
    destination: M, 
    keySelector: (UInt) -> K, 
    valueTransform: (UInt) -> V
): M
@ExperimentalUnsignedTypes inline fun <K, V, M : MutableMap<in K, MutableList<V>>> ULongArray.groupByTo(
    destination: M, 
    keySelector: (ULong) -> K, 
    valueTransform: (ULong) -> V
): M
@ExperimentalUnsignedTypes inline fun <K, V, M : MutableMap<in K, MutableList<V>>> UByteArray.groupByTo(
    destination: M, 
    keySelector: (UByte) -> K, 
    valueTransform: (UByte) -> V
): M
@ExperimentalUnsignedTypes inline fun <K, V, M : MutableMap<in K, MutableList<V>>> UShortArray.groupByTo(
    destination: M, 
    keySelector: (UShort) -> K, 
    valueTransform: (UShort) -> V
): M

Groups values returned by the valueTransform function applied to each element of the original array by the key returned by the given keySelector function applied to the element and puts to the destination map each group key associated with a list of corresponding values.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val nameToTeam = listOf("Alice" to "Marketing", "Bob" to "Sales", "Carol" to "Marketing")
val namesByTeam = nameToTeam.groupBy({ it.second }, { it.first })
println(namesByTeam) // {Marketing=[Alice, Carol], Sales=[Bob]}

val mutableNamesByTeam = nameToTeam.groupByTo(HashMap(), { it.second }, { it.first })
// same content as in namesByTeam map, but the map is mutable
println("mutableNamesByTeam == namesByTeam is ${mutableNamesByTeam == namesByTeam}") // true
//sampleEnd
}

Return The destination map.

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
inline fun <T, K, M : MutableMap<in K, MutableList<T>>> Iterable<T>.groupByTo(
    destination: M, 
    keySelector: (T) -> K
): M

Groups elements of the original collection by the key returned by the given keySelector function applied to each element and puts to the destination map each group key associated with a list of corresponding elements.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val words = listOf("a", "abc", "ab", "def", "abcd")
val byLength = words.groupBy { it.length }

println(byLength.keys) // [1, 3, 2, 4]
println(byLength.values) // [[a], [abc, def], [ab], [abcd]]

val mutableByLength: MutableMap<Int, MutableList<String>> = words.groupByTo(mutableMapOf()) { it.length }
// same content as in byLength map, but the map is mutable
println("mutableByLength == byLength is ${mutableByLength == byLength}") // true
//sampleEnd
}

Return The destination map.

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
inline fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Iterable<T>.groupByTo(
    destination: M, 
    keySelector: (T) -> K, 
    valueTransform: (T) -> V
): M

Groups values returned by the valueTransform function applied to each element of the original collection by the key returned by the given keySelector function applied to the element and puts to the destination map each group key associated with a list of corresponding values.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val nameToTeam = listOf("Alice" to "Marketing", "Bob" to "Sales", "Carol" to "Marketing")
val namesByTeam = nameToTeam.groupBy({ it.second }, { it.first })
println(namesByTeam) // {Marketing=[Alice, Carol], Sales=[Bob]}

val mutableNamesByTeam = nameToTeam.groupByTo(HashMap(), { it.second }, { it.first })
// same content as in namesByTeam map, but the map is mutable
println("mutableNamesByTeam == namesByTeam is ${mutableNamesByTeam == namesByTeam}") // true
//sampleEnd
}

Return The destination map.

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