groupBy

Common
JVM
JS
Native
1.0
inline fun < T , K > Array < out T > . groupBy (
keySelector : ( T ) -> K
) : Map < K , List < T > >

(source)
inline fun < K > ByteArray . groupBy (
keySelector : ( Byte ) -> K
) : Map < K , List < Byte > >

(source)
inline fun < K > ShortArray . groupBy (
keySelector : ( Short ) -> K
) : Map < K , List < Short > >

(source)
inline fun < K > IntArray . groupBy (
keySelector : ( Int ) -> K
) : Map < K , List < Int > >

(source)
inline fun < K > LongArray . groupBy (
keySelector : ( Long ) -> K
) : Map < K , List < Long > >

(source)
inline fun < K > FloatArray . groupBy (
keySelector : ( Float ) -> K
) : Map < K , List < Float > >

(source)
inline fun < K > DoubleArray . groupBy (
keySelector : ( Double ) -> K
) : Map < K , List < Double > >

(source)
inline fun < K > BooleanArray . groupBy (
keySelector : ( Boolean ) -> K
) : Map < K , List < Boolean > >

(source)
inline fun < K > CharArray . groupBy (
keySelector : ( Char ) -> K
) : Map < K , List < Char > >

(source)
@ExperimentalUnsignedTypes inline fun < K > UIntArray . groupBy (
keySelector : ( UInt ) -> K
) : Map < K , List < UInt > >

(source)
@ExperimentalUnsignedTypes inline fun < K > ULongArray . groupBy (
keySelector : ( ULong ) -> K
) : Map < K , List < ULong > >

(source)
@ExperimentalUnsignedTypes inline fun < K > UByteArray . groupBy (
keySelector : ( UByte ) -> K
) : Map < K , List < UByte > >

(source)
@ExperimentalUnsignedTypes inline fun < K > UShortArray . groupBy (
keySelector : ( UShort ) -> K
) : Map < K , List < UShort > >

(source)

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

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

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
}
Common
JVM
JS
Native
1.0
inline fun < T , K , V > Array < out T > . groupBy (
keySelector : ( T ) -> K ,
valueTransform : ( T ) -> V
) : Map < K , List < V > >

(source)
inline fun < K , V > ByteArray . groupBy (
keySelector : ( Byte ) -> K ,
valueTransform : ( Byte ) -> V
) : Map < K , List < V > >

(source)
inline fun < K , V > ShortArray . groupBy (
keySelector : ( Short ) -> K ,
valueTransform : ( Short ) -> V
) : Map < K , List < V > >

(source)
inline fun < K , V > IntArray . groupBy (
keySelector : ( Int ) -> K ,
valueTransform : ( Int ) -> V
) : Map < K , List < V > >

(source)
inline fun < K , V > LongArray . groupBy (
keySelector : ( Long ) -> K ,
valueTransform : ( Long ) -> V
) : Map < K , List < V > >

(source)
inline fun < K , V > FloatArray . groupBy (
keySelector : ( Float ) -> K ,
valueTransform : ( Float ) -> V
) : Map < K , List < V > >

(source)
inline fun < K , V > DoubleArray . groupBy (
keySelector : ( Double ) -> K ,
valueTransform : ( Double ) -> V
) : Map < K , List < V > >

(source)
inline fun < K , V > BooleanArray . groupBy (
keySelector : ( Boolean ) -> K ,
valueTransform : ( Boolean ) -> V
) : Map < K , List < V > >

(source)
inline fun < K , V > CharArray . groupBy (
keySelector : ( Char ) -> K ,
valueTransform : ( Char ) -> V
) : Map < K , List < V > >

(source)
@ExperimentalUnsignedTypes inline fun < K , V > UIntArray . groupBy (
keySelector : ( UInt ) -> K ,
valueTransform : ( UInt ) -> V
) : Map < K , List < V > >

(source)
@ExperimentalUnsignedTypes inline fun < K , V > ULongArray . groupBy (
keySelector : ( ULong ) -> K ,
valueTransform : ( ULong ) -> V
) : Map < K , List < V > >

(source)
@ExperimentalUnsignedTypes inline fun < K , V > UByteArray . groupBy (
keySelector : ( UByte ) -> K ,
valueTransform : ( UByte ) -> V
) : Map < K , List < V > >

(source)
@ExperimentalUnsignedTypes inline fun < K , V > UShortArray . groupBy (
keySelector : ( UShort ) -> K ,
valueTransform : ( UShort ) -> V
) : Map < K , List < V > >

(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 returns a map where each group key is associated with a list of corresponding values.

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

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
}
Common
JVM
JS
Native
1.0
inline fun < T , K > Iterable < T > . groupBy (
keySelector : ( T ) -> K
) : Map < K , List < T > >

(source)

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

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

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
}
Common
JVM
JS
Native
1.0
inline fun < T , K , V > Iterable < T > . groupBy (
keySelector : ( T ) -> K ,
valueTransform : ( T ) -> V
) : Map < K , List < V > >

(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 returns a map where each group key is associated with a list of corresponding values.

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

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
}