0Pricing
Android Academy · Lesson

map, filter, forEach

Transform collections with lambdas.

map, filter, forEach is a free Android Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)
}

map Can Change Types

The result of map does not have to be the same type as the input.

Here we turn a list of names into a list of their lengths.

fun main() {
    val names = listOf("Ada", "Linus")
    val lengths = names.map { it.length }
    println(lengths)
}

filter Keeps Matches

filter returns a new list containing only the items for which the lambda returns true.

Here we keep just the even numbers.

fun main() {
    val nums = listOf(1, 2, 3, 4, 5, 6)
    val evens = nums.filter { it % 2 == 0 }
    println(evens)
}

filter on Strings

The condition can be anything that returns a Boolean. Here we keep only the long words.

Items that fail the test are simply left out of the result.

fun main() {
    val words = listOf("hi", "hello", "hey", "howdy")
    val long = words.filter { it.length > 3 }
    println(long)
}

forEach Runs an Action

forEach runs a block of code for every item but does not build a new list.

Use it for side effects like printing or logging.

fun main() {
    val fruits = listOf("apple", "banana")
    fruits.forEach { println("Fruit: $it") }
}

map vs forEach

map returns a transformed list, so use it when you need a result.

forEach returns nothing useful, so use it only for actions. Choosing the right one keeps code clear.

Chaining Operations

These operations return collections, so you can chain them together.

Read left to right: first filter, then map the survivors.

fun main() {
    val nums = listOf(1, 2, 3, 4, 5, 6)
    val result = nums.filter { it % 2 == 0 }.map { it * 10 }
    println(result)
}

Naming the Parameter

Instead of it, you can name the lambda parameter for clarity, especially when chaining.

Use an arrow: { name -> ... }.

fun main() {
    val prices = listOf(10, 25, 5)
    val withTax = prices.map { price -> price + price / 10 }
    println(withTax)
}

forEachIndexed

When you need the position too, use forEachIndexed. It passes the index and the item.

This avoids manually tracking a counter.

fun main() {
    val tasks = listOf("eat", "code", "rest")
    tasks.forEachIndexed { i, task ->
        println("$i: $task")
    }
}

Combining for Real Work

Together these operations express data pipelines simply. Here we find the total length of the long names.

This reads almost like plain English.

fun main() {
    val names = listOf("Ada", "Grace", "Bo")
    val total = names.filter { it.length > 2 }.map { it.length }.sum()
    println(total)
}

Quick Check

Test your understanding of collection operations.

Recap

map transforms each item into a new list, filter keeps items matching a condition, and forEach runs an action per item.

They return collections, so chain them to build readable data pipelines.

Frequently asked questions

Is the “map, filter, forEach” lesson free?

Yes — the full text of “map, filter, forEach” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “map, filter, forEach”?

Transform collections with lambdas. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “map, filter, forEach” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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