0Pricing
Kotlin Academy · Lesson

Iterating and Transforming Collections with Standard Library

Use map, filter, any, all, count on real collection examples.

Iterating and Transforming Collections with Standard Library is a free Kotlin Academy lesson on CoddyKit — lesson 4 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 Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Collections as Pipelines

Kotlin's standard library transforms collections through chained operations. Each function returns a new collection — the original is untouched.

map: One-to-One Transformation

map applies a function to each element and returns a list of results.
val prices = listOf(10, 20, 30)
val withTax = prices.map { it * 1.1 }
println(withTax)  // [11.0, 22.0, 33.0]

flatMap: One-to-Many

flatMap maps each element to a list, then flattens all lists into one.
val sentences = listOf("hello world", "foo bar")
val words = sentences.flatMap { it.split(" ") }
println(words)  // [hello, world, foo, bar]

filter and filterNot

filter keeps elements matching the predicate; filterNot keeps those that don't.
val nums = (1..10).toList()
val evens = nums.filter { it % 2 == 0 }
val odds = nums.filterNot { it % 2 == 0 }
println(evens)  // [2, 4, 6, 8, 10]

groupBy: Bucket by Key

groupBy returns a Map> where elements are grouped by the key function.
val words = listOf("one", "two", "three", "four", "five")
val byLength = words.groupBy { it.length }
println(byLength)
// {3=[one, two], 5=[three], 4=[four, five]}

associate and associateBy

associate builds a Map with a key-value pair lambda. associateBy uses a key extractor.
val items = listOf("apple", "banana", "cherry")
val byFirst = items.associateBy { it.first() }
println(byFirst)  // {a=apple, b=banana, c=cherry}

partition: Split Into Two

partition splits a collection into two lists: matching and non-matching elements.
val (evens, odds) = (1..8).partition { it % 2 == 0 }
println(evens)  // [2, 4, 6, 8]
println(odds)   // [1, 3, 5, 7]

distinct and distinctBy

distinct removes duplicate elements. distinctBy removes duplicates based on a key.
val nums = listOf(1, 2, 2, 3, 1, 4)
println(nums.distinct())  // [1, 2, 3, 4]
val words = listOf("Hi", "hello", "HI")
println(words.distinctBy { it.lowercase() })

take, drop, and chunked

take takes the first N elements; drop skips the first N; chunked splits into sublists of size N.
val list = (1..10).toList()
println(list.take(3))    // [1, 2, 3]
println(list.drop(7))    // [8, 9, 10]
println(list.chunked(3)) // [[1,2,3],[4,5,6],[7,8,9],[10]]

zip: Pair Two Collections

zip combines two lists into a list of pairs. Stops at the shorter list.
val names = listOf("Alice", "Bob", "Charlie")
val scores = listOf(90, 85, 92)
val combined = names.zip(scores)
println(combined)  // [(Alice, 90), (Bob, 85), (Charlie, 92)]

sumOf, minOf, maxOf, averageOf

Aggregation shortcuts using transformation functions.
val items = listOf("apple", "banana", "cherry")
println(items.sumOf { it.length })    // 18
println(items.maxOf { it.length })    // 6
println(items.minOf { it.length })    // 5

Quick Check

Which function groups a collection into a Map> by a key selector?

Recap

map, flatMap, filter, groupBy, associate, partition, distinct, zip — the core collection transformation toolkit. Chain them into expressive pipelines. Next: classes and constructors!

Frequently asked questions

Is the “Iterating and Transforming Collections with Standard Library” lesson free?

Yes — the full text of “Iterating and Transforming Collections with Standard Library” is free to read here on the web, and the Kotlin 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 Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “Iterating and Transforming Collections with Standard Library”?

Use map, filter, any, all, count on real collection examples. You practise Kotlin 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 Kotlin Academy?

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

How long does the “Iterating and Transforming Collections with Standard Library” 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 Kotlin Academy lesson?

Yes. Every Kotlin 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. listOf, setOf, mapOf: Read-Only Collections
  2. mutableListOf, mutableMapOf: Mutable Builders
  3. Accessing Elements: get, first, last, getOrNull
  4. Iterating and Transforming Collections with Standard Library
← Back to Kotlin Academy