Filtering and Mapping
Transform collections.
Transforming Collections
Kotlin collections come with a rich set of functional operations. Two of the most common are filter (select elements) and map (transform elements). They return new collections and never modify the original.
The filter Function
filter keeps only the elements that satisfy a predicate, returning a new list.
fun main() {
val numbers = listOf(1, 2, 3, 4, 5, 6)
val evens = numbers.filter { it % 2 == 0 }
println(evens)
}All lessons in this course
- Filtering and Mapping
- Grouping and Partitioning
- Aggregation
- Association and Flattening