groupByTo

Common
JVM
JS
Native
1.0
inline fun <T, K, M : MutableMap<in K, MutableList<T>>> Array<out T>.groupByTo(
    destination: M,
    keySelector: (T) -> K
): M

(source)
inline fun <K, M : MutableMap<in K, MutableList<Byte>>> ByteArray.groupByTo(
    destination: M,
    keySelector: (Byte) -> K
): M

(source)
inline fun <K, M : MutableMap<in K, MutableList<Short>>> ShortArray.groupByTo(
    destination: M,
    keySelector: (Short) -> K
): M

(source)
inline fun <K, M : MutableMap<in K, MutableList<Int>>> IntArray.groupByTo(
    destination: M,
    keySelector: (Int) -> K
): M

(source)
inline fun <K, M : MutableMap<in K, MutableList<Long>>> LongArray.groupByTo(
    destination: M,
    keySelector: (Long) -> K
): M

(source)
inline fun <K, M : MutableMap<in K, MutableList<Float>>> FloatArray.groupByTo(
    destination: M,
    keySelector: (Float) -> K
): M

(source)
inline fun <K, M : MutableMap<in K, MutableList<Double>>> DoubleArray.groupByTo(
    destination: M,
    keySelector: (Double) -> K
): M

(source)
inline fun <K, M : MutableMap<in K, MutableList<Boolean>>> BooleanArray.groupByTo(
    destination: M,
    keySelector: (Boolean) -> K
): M

(source)
inline fun <K, M : MutableMap<in K, MutableList<Char>>> CharArray.groupByTo(
    destination: M,
    keySelector: (Char) -> K
): M

(source)
@ExperimentalUnsignedTypes inline fun <K, M : MutableMap<in K, MutableList<UInt>>> UIntArray.groupByTo(
    destination: M,
    keySelector: (UInt) -> K
): M

(source)
@ExperimentalUnsignedTypes inline fun <K, M : MutableMap<in K, MutableList<ULong>>> ULongArray.groupByTo(
    destination: M,
    keySelector: (ULong) -> K
): M

(source)
@ExperimentalUnsignedTypes inline fun <K, M : MutableMap<in K, MutableList<UByte>>> UByteArray.groupByTo(
    destination: M,
    keySelector: (UByte) -> K
): M

(source)
@ExperimentalUnsignedTypes inline fun <K, M : MutableMap<in K, MutableList<UShort>>> UShortArray.groupByTo(
    destination: M,
    keySelector: (UShort) -> K
): M

(source)

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.

Common
JVM
JS
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

(source)
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> ByteArray.groupByTo(
    destination: M,
    keySelector: (Byte) -> K,
    valueTransform: (Byte) -> V
): M

(source)
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> ShortArray.groupByTo(
    destination: M,
    keySelector: (Short) -> K,
    valueTransform: (Short) -> V
): M

(source)
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> IntArray.groupByTo(
    destination: M,
    keySelector: (Int) -> K,
    valueTransform: (Int) -> V
): M

(source)
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> LongArray.groupByTo(
    destination: M,
    keySelector: (Long) -> K,
    valueTransform: (Long) -> V
): M

(source)
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> FloatArray.groupByTo(
    destination: M,
    keySelector: (Float) -> K,
    valueTransform: (Float) -> V
): M

(source)
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> DoubleArray.groupByTo(
    destination: M,
    keySelector: (Double) -> K,
    valueTransform: (Double) -> V
): M

(source)
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> BooleanArray.groupByTo(
    destination: M,
    keySelector: (Boolean) -> K,
    valueTransform: (Boolean) -> V
): M

(source)
inline fun <K, V, M : MutableMap<in K, MutableList<V>>> CharArray.groupByTo(
    destination: M,
    keySelector: (Char) -> K,
    valueTransform: (Char) -> V
): M

(source)
@ExperimentalUnsignedTypes inline fun <K, V, M : MutableMap<in K, MutableList<V>>> UIntArray.groupByTo(
    destination: M,
    keySelector: (UInt) -> K,
    valueTransform: (UInt) -> V
): M

(source)
@ExperimentalUnsignedTypes inline fun <K, V, M : MutableMap<in K, MutableList<V>>> ULongArray.groupByTo(
    destination: M,
    keySelector: (ULong) -> K,
    valueTransform: (ULong) -> V
): M

(source)
@ExperimentalUnsignedTypes inline fun <K, V, M : MutableMap<in K, MutableList<V>>> UByteArray.groupByTo(
    destination: M,
    keySelector: (UByte) -> K,
    valueTransform: (UByte) -> V
): M

(source)
@ExperimentalUnsignedTypes inline fun <K, V, M : MutableMap<in K, MutableList<V>>> UShortArray.groupByTo(
    destination: M,
    keySelector: (UShort) -> K,
    valueTransform: (UShort) -> V
): M

(source)

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.

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

(source)

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.

Common
JVM
JS
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

(source)

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.