Kotlin List groupByTo()
Syntax & Examples
Syntax of List.groupByTo()
There are 2 variations for the syntax of List.groupByTo() extension function. They are:
fun <T, K, M : MutableMap<in K, MutableList<T>>> Iterable<T>.groupByTo( destination: M, keySelector: (T) -> K ): M
This extension function 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.
fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Iterable<T>.groupByTo( destination: M, keySelector: (T) -> K, valueTransform: (T) -> V ): M
This extension function 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.
✐ Examples
1 Example
In this example,
- We create a list of strings named
list
. - We also create an empty mutable map named
resultMap
. - We use the
groupByTo()
function onlist
with a keySelector that extracts the first character of each string. - The result is grouped elements in
list
by the first character, and the groups are stored inresultMap
. - Finally, we print the
resultMap
to standard output using println statement.
Kotlin Program
fun main(args: Array<String>) {
val list = listOf("apple", "banana", "cherry", "blueberry", "apricot")
val resultMap = mutableMapOf<Char, MutableList<String>>()
list.groupByTo(resultMap) { it.first() }
println(resultMap)
}
Output
{a=[apple, apricot], b=[banana, blueberry], c=[cherry]}
2 Example
In this example,
- We define a data class
Person
withname
andage
properties. - We create a list of
Person
objects namedpeople
. - We also create an empty mutable map named
ageMap
. - We use the
groupByTo()
function onpeople
with a keySelector that extracts the age and a valueTransform that gets the name of each person. - The result is groups of people in
people
by age, and their names are stored inageMap
. - Finally, we print the
ageMap
to standard output using println statement.
Kotlin Program
fun main(args: Array<String>) {
data class Person(val name: String, val age: Int)
val people = listOf(
Person("Alice", 30),
Person("Bob", 25),
Person("Charlie", 35),
Person("David", 40)
)
val ageMap = mutableMapOf<Int, MutableList<String>>()
people.groupByTo(ageMap, { it.age }, { it.name })
println(ageMap)
}
Output
{30=[Alice], 25=[Bob], 35=[Charlie], 40=[David]}
3 Example
In this example,
- We create a list of integers named
numbers
. - We also create an empty mutable map named
isEvenMap
. - We use the
groupByTo()
function onnumbers
with a keySelector that checks if each number is even. - The result is groups of numbers in
numbers
by whether they are even or not, and the groups are stored inisEvenMap
. - Finally, we print the
isEvenMap
to standard output using println statement.
Kotlin Program
fun main(args: Array<String>) {
val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
val isEvenMap = mutableMapOf<Boolean, MutableList<Int>>()
numbers.groupByTo(isEvenMap) { it % 2 == 0 }
println(isEvenMap)
}
Output
{false=[1, 3, 5, 7, 9], true=[2, 4, 6, 8]}
Summary
In this Kotlin tutorial, we learned about groupByTo() extension function of List: the syntax and few working examples with output and detailed explanation for each example.