100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Collection Operations in Kotlin (map, filter, fold)

How to transform, filter, and reduce Kotlin collections using functional-style operations like map, filter, and fold.

CollectionsIntermediate10 min readJul 8, 2026
Analogies

Introduction

Kotlin's standard library provides a large set of extension functions on collections that are inspired by functional programming languages. Operations like map, filter, fold, reduce, sortedBy, and groupBy let you transform and process data declaratively, chaining multiple steps together instead of writing manual loops. These operations are eager by default, meaning each call produces a new intermediate collection.

🏏

Cricket analogy: Chaining map, filter, fold, sortedBy, groupBy on a ball-by-ball dataset is like a commentator declaratively deriving 'top six-hitters this over' from raw deliveries, but each step (like grouping by bowler) produces a fresh intermediate scorecard.

Syntax

kotlin
val doubled = list.map { it * 2 }
val evens = list.filter { it % 2 == 0 }
val sum = list.fold(0) { acc, x -> acc + x }
val product = list.reduce { acc, x -> acc * x }
val sorted = list.sortedBy { it }
val grouped = list.groupBy { it % 2 == 0 }

Explanation

map { } applies a transformation to each element and returns a new list of the results. filter { } keeps only the elements for which the given predicate lambda returns true. fold(initial) { acc, x -> ... } combines all elements into a single accumulated value, starting from an explicit seed value; this makes it safe on empty collections since the seed is always returned when there are no elements. reduce { acc, x -> ... } behaves like fold but uses the first element of the collection as the initial accumulator, so it throws an exception if called on an empty collection. These operations can be chained together, and because each eager call materializes a new collection, chaining many operations on very large collections can create several intermediate lists.

🏏

Cricket analogy: map{} converting each delivery to 'runs scored' is like a scorer transforming every ball into a run tally; filter{} keeps only boundary balls; fold(0){acc,x->...} sums total runs safely even on a rain-abandoned (empty) innings, while reduce would crash with no first ball to seed from.

Example

kotlin
fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)

    val result = numbers
        .filter { it % 2 != 0 }
        .map { it * it }
        .fold(0) { acc, x -> acc + x }

    println(result)
    println(numbers.reduce { acc, x -> acc + x })
}
kotlin
35
15
  • map { } transforms each element into a new value, producing a list of the same size.
  • filter { } keeps only elements matching a predicate, producing a list of equal or smaller size.
  • fold(initial) { } reduces a collection to one value starting from a given seed, safe on empty collections.
  • reduce { } is like fold but uses the first element as the seed, and throws on empty collections.
  • These operations are eager and chainable, but each step creates a new intermediate collection.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#CollectionOperationsInKotlinMapFilterFold#Collection#Operations#Map#Filter#StudyNotes#SkillVeris