Kotlin Set groupByTo()
Syntax & Examples
Set.groupByTo() extension function
The groupByTo() extension function in Kotlin groups elements of the original set by the key returned by the given keySelector function applied to each element and puts each group key associated with a list of corresponding elements or values into the destination map.
Syntax of Set.groupByTo()
There are 2 variations for the syntax of Set.groupByTo() extension function. They are:
fun <T, K, M : MutableMap<in K, MutableList<T>>> Set<T>.groupByTo(destination: M, keySelector: (T) -> K): M
Parameters
Parameter | Optional/Required | Description |
---|---|---|
destination | required | The map to which the grouped elements or values will be added. |
keySelector | required | A function that takes an element and returns the key for grouping. |
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.
Returns value of type M
.
fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Set<T>.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M
Parameters
Parameter | Optional/Required | Description |
---|---|---|
destination | required | The map to which the grouped elements or values will be added. |
keySelector | required | A function that takes an element and returns the key for grouping. |
valueTransform | optional | A function that takes an element and returns the value to be grouped. |
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.
Returns value of type M
.
✐ Examples
1 Grouping integers by even and odd to a destination map
Using groupByTo() to group elements in a set of integers by even and odd, and putting them into a destination map.
For example,
- Create a set of integers.
- Create an empty destination map.
- Use groupByTo() with a keySelector function that groups elements by even and odd, and put the results into the destination map.
- Print the resulting map.
Kotlin Program
fun main() {
val numbers = setOf(1, 2, 3, 4, 5, 6)
val destinationMap = mutableMapOf<String, MutableList<Int>>()
numbers.groupByTo(destinationMap) { if (it % 2 == 0) "Even" else "Odd" }
println(destinationMap)
}
Output
{Odd=[1, 3, 5], Even=[2, 4, 6]}
2 Grouping strings by their length to a destination map
Using groupByTo() to group elements in a set of strings by their length, and putting them into a destination map.
For example,
- Create a set of strings.
- Create an empty destination map.
- Use groupByTo() with a keySelector function that groups elements by their length, and put the results into the destination map.
- Print the resulting map.
Kotlin Program
fun main() {
val strings = setOf("one", "two", "three", "four", "five")
val destinationMap = mutableMapOf<Int, MutableList<String>>()
strings.groupByTo(destinationMap) { it.length }
println(destinationMap)
}
Output
{3=[one, two], 5=[three], 4=[four, five]}
3 Grouping integers by even and odd with custom value transformation to a destination map
Using groupByTo() to group elements in a set of integers by even and odd, transforming the values to their squares, and putting them into a destination map.
For example,
- Create a set of integers.
- Create an empty destination map.
- Use groupByTo() with a keySelector function that groups elements by even and odd, and a valueTransform function that squares the elements, putting the results into the destination map.
- Print the resulting map.
Kotlin Program
fun main() {
val numbers = setOf(1, 2, 3, 4, 5, 6)
val destinationMap = mutableMapOf<String, MutableList<Int>>()
numbers.groupByTo(destinationMap, keySelector = { if (it % 2 == 0) "Even" else "Odd" }, valueTransform = { it * it })
println(destinationMap)
}
Output
{Odd=[1, 9, 25], Even=[4, 16, 36]}
Summary
In this Kotlin tutorial, we learned about groupByTo() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.