← Back to Kotlin Course | Chapter 9: Lambdas & Higher-Order Functions | Lesson 3 of 6

map, filter, reduce

map, filter, and reduce are three power tools for transforming a whole list at once: changing each item, keeping only some, or squishing it all into one result.
Syntax
kotlin
val mapped = list.map { it transformation }
val filtered = list.filter { condition }
val total = list.reduce { acc, item -> acc + item }

Transforming with map

.map { } applies a transformation to every element of a collection and returns a brand-new list with the results, leaving the original collection untouched.

Example: Transforming with map

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4)`
    val numbers = listOf(1, 2, 3, 4)
    // Declare a read-only variable `squares`, set to `numbers.map { it * it }`
    val squares = numbers.map { it * it }
    // Print `squares` to the console, with a trailing newline
    println(squares)
}

Selecting with filter

.filter { } returns a new list containing only the elements for which the given predicate returns true, discarding the rest.

Example: Selecting with filter

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4, 5, 6)`
    val numbers = listOf(1, 2, 3, 4, 5, 6)
    // Declare a read-only variable `evens`, set to `numbers.filter { it % 2 == 0 }`
    val evens = numbers.filter { it % 2 == 0 }
    // Print `evens` to the console, with a trailing newline
    println(evens)
}

Combining into One Value with reduce

.reduce { acc, item -> ... } repeatedly combines elements into a single accumulated result, starting with the first element as the initial accumulator value.

Example: Combining into One Value with reduce

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4)`
    val numbers = listOf(1, 2, 3, 4)
    // Declare a read-only variable `sum`, set to `numbers.reduce { acc, n -> acc + n }`
    val sum = numbers.reduce { acc, n -> acc + n }
    // Print "Sum via reduce: $sum" to the console, with a trailing newline
    println("Sum via reduce: $sum")
}

Combining with fold and an Initial Value

.fold(initial) { acc, item -> ... } behaves like reduce but starts from an explicit initial value, which also makes it safe to use on an empty collection.

Example: Combining with fold and an Initial Value

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `listOf<Int>()`
    val numbers = listOf<Int>()
    // Declare a read-only variable `sum`, set to `numbers.fold(0) { acc, n -> acc + n }`
    val sum = numbers.fold(0) { acc, n -> acc + n }
    // Print "Sum via fold on empty list: $sum" to the console, with a trailing newline
    println("Sum via fold on empty list: $sum")

    // Declare a read-only variable `words`, set to `listOf("Kotlin", "is", "fun")`
    val words = listOf("Kotlin", "is", "fun")
    // Declare a read-only variable `sentence`, set to `words.fold("Result:") { acc, word -> "$acc $word" }`
    val sentence = words.fold("Result:") { acc, word -> "$acc $word" }
    // Print `sentence` to the console, with a trailing newline
    println(sentence)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Confusing .reduce { } with .fold(initial) { }; reduce uses the first element as the starting accumulator and throws on an empty collection, while fold takes an explicit initial value.
  2. Chaining .filter { }.map { } in the wrong order and getting different (though sometimes equally valid) results than intended.
  3. Forgetting these functions return a new collection or value and never mutate the original collection.
Chapter Summary
  • .map { transform } returns a new list with each element transformed according to the given function.
  • .filter { predicate } returns a new list containing only the elements that satisfy the given condition.
  • .reduce { acc, item -> ... } combines all elements into a single result, using the first element as the initial accumulator.
  • .fold(initial) { acc, item -> ... } is like reduce but takes an explicit starting value, so it also works on empty collections.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.