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
- Filtering and Mapping
- Grouping and Partitioning
- Aggregation
- Association and Flattening