0Pricing
Kotlin Academy · Lesson

Grouping and Partitioning

Organize data.

Organizing Data

Sometimes you need to split a collection into categories. Kotlin offers groupBy to bucket elements by a key and partition to split into two lists by a condition.

The partition Function

partition splits a collection into a Pair of two lists: elements that match the predicate, and elements that do not.

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5, 6)
    val (even, odd) = numbers.partition { it % 2 == 0 }
    println("Even: $even")
    println("Odd: $odd")
}

All lessons in this course

  1. Filtering and Mapping
  2. Grouping and Partitioning
  3. Aggregation
  4. Association and Flattening
← Back to Kotlin Academy