map and flatMap: Transforming Every Element
Transform collections with map and flatten nested results with flatMap.
map and flatMap: Transforming Every Element is a free Kotlin Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
Transformation Basics
map applies a function to each element, returning a new collection of the same size. flatMap does the same but flattens lists-of-lists into a single list.
Basic map
Transform each element with a lambda; the result has the same size as the input.
fun main() {
val nums = listOf(1, 2, 3, 4)
val squares = nums.map { it * it }
println(squares) // [1, 4, 9, 16]
}map Changes the Type
The output element type can differ from the input — List<Int> can become List<String>.
fun main() {
val nums = listOf(1, 2, 3)
val labels = nums.map { "Item $it" }
println(labels)
}map on a Data Class
Project a property out of each element — turn data classes into simpler types.
data class User(val name: String, val age: Int)
fun main() {
val users = listOf(User("Ada", 35), User("Ben", 22))
val names = users.map { it.name }
println(names) // [Ada, Ben]
}Chaining map Calls
Multiple map calls compose smoothly — each transforms the output of the previous.
fun main() {
val nums = listOf(1, 2, 3)
val result = nums.map { it + 1 }.map { it * 10 }
println(result) // [20, 30, 40]
}Basic flatMap
flatMap calls a function that returns a list for each element, then flattens all results into a single list.
fun main() {
val words = listOf("hello", "world")
val chars = words.flatMap { it.toList() }
println(chars) // [h, e, l, l, o, w, o, r, l, d]
}When to Use flatMap
Use flatMap when the lambda naturally produces a list and you want one flat result instead of a list-of-lists.
data class Order(val items: List<String>)
fun main() {
val orders = listOf(
Order(listOf("pen", "book")),
Order(listOf("lamp")),
Order(listOf("mug", "ring"))
)
val allItems = orders.flatMap { it.items }
println(allItems) // [pen, book, lamp, mug, ring]
}map of List vs flatMap
Compare the result types — map keeps nesting, flatMap removes it.
fun main() {
val nums = listOf(1, 2, 3)
val mapped = nums.map { listOf(it, it * 10) }
val flat = nums.flatMap { listOf(it, it * 10) }
println(mapped) // [[1, 10], [2, 20], [3, 30]]
println(flat) // [1, 10, 2, 20, 3, 30]
}flatMap with Conditional Lists
Return empty lists for elements you want to drop — flatMap filters and transforms in one step.
fun main() {
val nums = listOf(1, 2, 3, 4, 5)
val doubled = nums.flatMap { if (it % 2 == 0) listOf(it, it * 2) else emptyList() }
println(doubled) // [2, 4, 4, 8]
}map with Index
mapIndexed provides both index and value to the lambda.
fun main() {
val fruits = listOf("apple", "banana", "cherry")
val labeled = fruits.mapIndexed { i, name -> "$i: $name" }
println(labeled)
}Sequence vs List
For very long chains, convert to a Sequence first to avoid building intermediate lists.
fun main() {
val big = (1..100).asSequence()
.map { it + 1 }
.filter { it % 5 == 0 }
.map { it * 2 }
.toList()
println(big.take(5)) // first 5 results
}Quick Check
What's the key difference between map and flatMap?
Recap
map transforms each element (same size, possibly new type). flatMap applies a function returning a list, then concatenates all results. Combine with mapIndexed for indices, and switch to Sequence for long lazy pipelines.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “map and flatMap: Transforming Every Element” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “map and flatMap: Transforming Every Element”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
What will I learn in “map and flatMap: Transforming Every Element”?
Transform collections with map and flatten nested results with flatMap. 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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “map and flatMap: Transforming Every Element” 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
- map and flatMap: Transforming Every Element
- filter, filterNot, and partition
- fold, reduce, and runningFold
- Chaining Pipelines and Avoiding Intermediate Lists with Sequence