Advanced Collections (groupBy, fold, windowed)

Kotlin's standard library offers powerful collection operators — groupBy , fold , windowed and more — that replace hand-written loops with clear, declarative one-liners.

Learn Advanced Collections (groupBy, fold, windowed) in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice…

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.

You'll group and index data, accumulate with fold / reduce , slice with chunked / windowed , and aggregate with sumOf , maxByOrNull , and zip .

What You'll Learn in This Lesson

1️⃣ Grouping and Indexing

groupBy {' '} buckets elements into a Map<K, List<V>> — every member is kept. associateBy {' '} builds a Map<K, V> with one value per key, so on a key collision the last element wins.

2️⃣ Accumulating and Splitting

fold(seed) {' '} accumulates from an initial value and can change type; reduce is the seedless version starting from the first element. partition {' '} splits a list into a pair of (matches, non-matches).

Remember: reduce throws on an empty list (no first element), while fold safely returns the seed.

3️⃣ Slicing, Flattening, Aggregating

chunked(n) makes non-overlapping batches; windowed(n) makes overlapping sliding windows; flatMap expands each element to a list and flattens. Then sumOf , maxByOrNull , and zip aggregate.

Your turn. Fill in the ___ blanks, then run and compare.

Group words by length and total their letters with groupBy and sumOf .

📋 Quick Reference — Operators

Practice quiz

How do groupBy and associateBy differ on duplicate keys?

  • They behave identically
  • associateBy keeps all; groupBy keeps the first
  • groupBy keeps all members in a list; associateBy keeps only the last
  • Both throw on duplicates

Answer: groupBy keeps all members in a list; associateBy keeps only the last. groupBy gives Map<K, List<V>>; associateBy gives Map<K, V> where the last wins.

What does chunked(2) on [1,2,3,4,5] produce?

chunked makes consecutive non-overlapping pieces; the last may be smaller.

What does windowed(2) on [1,2,3,4,5] produce?

windowed makes overlapping sliding windows that move one step at a time.

What does flatMap do compared to map?

  • It only filters
  • It maps each element to a list and flattens one level
  • It sorts the result
  • It is identical to map

Answer: It maps each element to a list and flattens one level. flatMap expands each element to a list then flattens into one flat list.

What does partition { it > 2 } return?

  • A Pair of (matches, non-matches)
  • A single filtered list
  • A Map of groups
  • A boolean

Answer: A Pair of (matches, non-matches). partition splits into a pair: elements that match, and those that don't.

What does reduce do on an empty list, and fold?

  • Both return null
  • Both throw
  • reduce throws; fold returns its seed
  • reduce returns the seed; fold throws

Answer: reduce throws; fold returns its seed. fold is empty-safe via its seed; reduce throws with no first element.

What does maxByOrNull { it.length } return?

  • The largest length value
  • The element with the largest length, or null if empty
  • A sorted list
  • The first element

Answer: The element with the largest length, or null if empty. maxByOrNull returns the element, not the derived value, and null on empty.

What does sumOf { it.length } do for a list of strings?

  • Counts the strings
  • Returns the longest string
  • Concatenates the strings
  • Applies the selector to each and sums those numbers

Answer: Applies the selector to each and sums those numbers. sumOf sums a number derived from each element without an intermediate map.

What does zip do with two lists?

  • Concatenates them
  • Pairs them element by element, stopping at the shorter
  • Sorts both
  • Removes duplicates

Answer: Pairs them element by element, stopping at the shorter. zip pairs elements positionally and stops at the shorter list.

In groupBy, in what order do the keys appear?

  • Sorted ascending
  • Random order
  • First-seen (insertion) order
  • Reverse order

Answer: First-seen (insertion) order. Keys appear in the order they were first encountered.