Kotlin Tutorials

Kotlin Set groupBy()
Syntax & Examples

Set.groupBy() extension function

The groupBy() extension function in Kotlin groups elements of the original set 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 or values.


Syntax of Set.groupBy()

There are 2 variations for the syntax of Set.groupBy() extension function. They are:

1.
fun <T, K> Set<T>.groupBy(keySelector: (T) -> K): Map<K, List<T>>

Parameters

ParameterOptional/RequiredDescription
keySelectorrequiredA 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 returns a map where each group key is associated with a list of corresponding elements.

Returns value of type Map<K, List<T>>.

2.
fun <T, K, V> Set<T>.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map<K, List<V>>

Parameters

ParameterOptional/RequiredDescription
keySelectorrequiredA function that takes an element and returns the key for grouping.
valueTransformoptionalA 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 returns a map where each group key is associated with a list of corresponding values.

Returns value of type Map<K, List<V>>.



✐ Examples

1 Grouping integers by even and odd

Using groupBy() to group elements in a set of integers by even and odd.

For example,

  1. Create a set of integers.
  2. Use groupBy() with a keySelector function that groups elements by even and odd.
  3. Print the resulting map.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3, 4, 5, 6)
    val groupedByEvenOdd = numbers.groupBy { if (it % 2 == 0) "Even" else "Odd" }
    println(groupedByEvenOdd)
}

Output

{Odd=[1, 3, 5], Even=[2, 4, 6]}

2 Grouping strings by their length

Using groupBy() to group elements in a set of strings by their length.

For example,

  1. Create a set of strings.
  2. Use groupBy() with a keySelector function that groups elements by their length.
  3. Print the resulting map.

Kotlin Program

fun main() {
    val strings = setOf("one", "two", "three", "four", "five")
    val groupedByLength = strings.groupBy { it.length }
    println(groupedByLength)
}

Output

{3=[one, two], 5=[three], 4=[four, five]}

3 Grouping integers by even and odd with custom value transformation

Using groupBy() to group elements in a set of integers by even and odd, and transforming the values to their squares.

For example,

  1. Create a set of integers.
  2. Use groupBy() with a keySelector function that groups elements by even and odd, and a valueTransform function that squares the elements.
  3. Print the resulting map.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3, 4, 5, 6)
    val groupedByEvenOddWithSquares = numbers.groupBy(
        keySelector = { if (it % 2 == 0) "Even" else "Odd" },
        valueTransform = { it * it }
    )
    println(groupedByEvenOddWithSquares)
}

Output

{Odd=[1, 9, 25], Even=[4, 16, 36]}

Summary

In this Kotlin tutorial, we learned about groupBy() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.