group By
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.
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
val words = listOf("a", "abc", "ab", "def", "abcd")
val byLength = words.groupBy { it.length }
assertPrints(byLength.keys, "[1, 3, 2, 4]")
assertPrints(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
assertTrue(mutableByLength == byLength)
//sampleEnd
}
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.
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
val nameToTeam = listOf("Alice" to "Marketing", "Bob" to "Sales", "Carol" to "Marketing")
val namesByTeam = nameToTeam.groupBy({ it.second }, { it.first })
assertPrints(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
assertTrue(mutableNamesByTeam == namesByTeam)
//sampleEnd
}
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.
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
val words = listOf("a", "abc", "ab", "def", "abcd")
val byLength = words.groupBy { it.length }
assertPrints(byLength.keys, "[1, 3, 2, 4]")
assertPrints(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
assertTrue(mutableByLength == byLength)
//sampleEnd
}
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.
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
val nameToTeam = listOf("Alice" to "Marketing", "Bob" to "Sales", "Carol" to "Marketing")
val namesByTeam = nameToTeam.groupBy({ it.second }, { it.first })
assertPrints(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
assertTrue(mutableNamesByTeam == namesByTeam)
//sampleEnd
}
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.
Since Kotlin
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
val words = listOf("a", "abc", "ab", "def", "abcd")
val byLength = words.groupBy { it.length }
assertPrints(byLength.keys, "[1, 3, 2, 4]")
assertPrints(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
assertTrue(mutableByLength == byLength)
//sampleEnd
}
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.
Since Kotlin
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
val nameToTeam = listOf("Alice" to "Marketing", "Bob" to "Sales", "Carol" to "Marketing")
val namesByTeam = nameToTeam.groupBy({ it.second }, { it.first })
assertPrints(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
assertTrue(mutableNamesByTeam == namesByTeam)
//sampleEnd
}