Collection Operations (map, filter, fold)

Kotlin is a modern, concise language whose standard library gives collections a rich functional API — map , filter , fold , and more — so you can transform and aggregate data without writing loops.

Learn Collection Operations (map, filter, fold) in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise…

Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll transform, filter, and reduce collections with clean, chainable operations.

What You'll Learn in This Lesson

1️⃣ map, filter, and Chaining

map applies a lambda to every element and returns a new list of results. filter keeps only the elements for which a lambda returns true . Because each returns a new collection, you can chain them into a readable pipeline.

Helpers like first {' '} , any {' '} , and count {' '} answer common questions in a single, declarative line.

2️⃣ fold, reduce, and groupBy

To collapse a collection into a single value, use fold (with an explicit start) or reduce (which starts from the first element). groupBy partitions elements into a Map keyed by whatever the lambda returns.

The accumulator in fold can be any type — note how the same call built first a number, then a string. That flexibility makes fold the most general aggregation tool.

Your turn. Replace the TODO , then run and compare.

Build one pipeline that filters, maps, and folds to produce a single total.

📋 Quick Reference — Collection Ops

Practice quiz

What does map do to a collection?

  • Removes elements that fail a test
  • Mutates the original list
  • Transforms each element and returns a new list
  • Sums the elements

Answer: Transforms each element and returns a new list. map applies a lambda to every element and returns a new list.

What does filter return?

  • A new list of elements where the lambda returns true
  • A single boolean
  • The original list reversed
  • The count of matches

Answer: A new list of elements where the lambda returns true. filter keeps only elements for which the predicate is true.

How does fold differ from reduce?

  • fold mutates the list; reduce does not
  • They are identical
  • reduce takes a seed value
  • fold takes an explicit initial value; reduce starts from the first element

Answer: fold takes an explicit initial value; reduce starts from the first element. fold has an initial accumulator and can change type; reduce starts from element one.

What does reduce do on an empty collection?

  • It returns 0
  • It throws an exception
  • It returns null
  • It returns an empty list

Answer: It throws an exception. reduce throws on empty because there is no first element; fold returns its seed.

What does listOf(1,2,3,4).fold(0) { acc, n -> acc + n } evaluate to?

  • 10
  • 24
  • 0
  • 1234

Answer: 10. It sums starting from 0: 0+1+2+3+4 = 10.

What does groupBy produce?

  • A single combined value
  • A sorted list
  • A Map keyed by the lambda's result, each value a list of members
  • A boolean

Answer: A Map keyed by the lambda's result, each value a list of members. groupBy partitions elements into a Map<K, List<V>>.

Do map, filter, and fold modify the original collection?

  • Yes, they mutate in place
  • No, they return a new collection or value
  • Only fold mutates
  • Only on mutable lists

Answer: No, they return a new collection or value. These operations are non-destructive; the original is untouched.

What is the difference between map and forEach?

  • forEach returns a new list; map does not
  • They are the same
  • map runs side effects only
  • map returns a new collection; forEach returns nothing useful (Unit)

Answer: map returns a new collection; forEach returns nothing useful (Unit). Use map to produce data; forEach for side effects like printing.

What does nums.first { it > 3 } return for listOf(1,2,3,4,5,6)?

  • true
  • 4

Answer: 4. first returns the first element matching the predicate, which is 4.

How can you make a long map/filter chain lazy to avoid intermediate lists?

  • Use forEach
  • Use a mutable list
  • Call asSequence() first
  • Wrap it in fold

Answer: Call asSequence() first. asSequence() processes each element through the chain lazily.