map, filter, forEach
Transform collections with lambdas.
Collection Operations
Kotlin collections come with powerful built-in operations. Three you will use constantly are map, filter, and forEach.
They let you transform, select, and iterate over data in one clear line, instead of writing manual loops.
map Transforms Each Item
map creates a new list by applying a transformation to every item.
Inside the lambda, it refers to the current item. The original list is unchanged.
fun main() {
val nums = listOf(1, 2, 3)
val doubled = nums.map { it * 2 }
println(doubled)
}All lessons in this course
- Lists and Mutable Lists
- Maps and Sets
- map, filter, forEach
- Lambdas and Higher-Order Functions