Kotlin List groupBy()
Syntax & Examples


Syntax of List.groupBy()

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

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

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.

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

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.



✐ Examples

1 Example

In this example,

  • We create a list named list containing strings representing various fruits.
  • We use the groupBy function to group the fruits based on the first letter of each fruit, converted to uppercase.
  • The result is a map where each group key represents the first letter of the fruit names, associated with a list of fruits starting with that letter.
  • We print the resulting map to standard output.

Kotlin Program

fun main(args: Array<String>) {
    val list = listOf("apple", "banana", "cherry", "orange", "grape");
    val result = list.groupBy { it.first().toUpperCase() }
    println(result);
}

Output

{A=[apple], B=[banana], C=[cherry], O=[orange], G=[grape]}

2 Example

In this example,

  • We define a data class Person with properties name and age.
  • We create a list named list containing instances of Person with different names and ages.
  • We use the groupBy function to group the persons by their names, and for each name group, we extract their ages into a list.
  • The result is a map where each group key represents a person's name, associated with a list of ages of persons with that name.
  • We print the resulting map to standard output.

Kotlin Program

fun main(args: Array<String>) {
    data class Person(val name: String, val age: Int)
    val list = listOf(
        Person("Alice", 20),
        Person("Bob", 25),
        Person("Alice", 30)
    )
    val result = list.groupBy({ it.name }, { it.age })
    println(result);
}

Output

{Alice=[20, 30], Bob=[25]}

3 Example

In this example,

  • We create a list named list containing strings representing various fruits.
  • We use the groupBy function to group the fruits based on their lengths.
  • The result is a map where each group key represents the length of the fruit names, associated with a list of fruits having that length.
  • We print the resulting map to standard output.

Kotlin Program

fun main(args: Array<String>) {
    val list = listOf("apple", "banana", "cherry", "orange", "grape")
    val result = list.groupBy { it.length }
    println(result);
}

Output

{5=[apple, grape], 6=[banana, cherry, orange]}

Summary

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