0Pricing
Android Academy · Lesson

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

  1. Lists and Mutable Lists
  2. Maps and Sets
  3. map, filter, forEach
  4. Lambdas and Higher-Order Functions
← Back to Android Academy