Kotlin Academy · Lesson

Association and Flattening

associate and flatMap.

Lesson 4 of 413 steps

Association and Flattening is a free Kotlin Academy lesson on CoddyKit. This is lesson 4 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.

Associating and Flattening

Two more powerful collection tools: associate family functions build maps from a list, and flatMap / flatten turn nested collections into a single flat one.

The associateWith Function

associateWith uses each element as a key and computes its value with a lambda, producing a Map.

fun main() {
    val words = listOf("a", "bb", "ccc")
    val lengths = words.associateWith { it.length }
    println(lengths)
}

The associateBy Function

associateBy is the opposite: it computes the key from each element, keeping the element as the value.

data class User(val id: Int, val name: String)

fun main() {
    val users = listOf(User(1, "Ann"), User(2, "Bob"))
    val byId = users.associateBy { it.id }
    println(byId[2]?.name)
}

The associate Function

The general associate lets you build both key and value at once by returning a Pair for each element.

fun main() {
    val names = listOf("Ann", "Bob", "Cy")
    val map = names.associate { it to it.length }
    println(map)
}

Duplicate Keys

If two elements produce the same key in an associate function, the last one wins. Keep keys unique when you need every element.

fun main() {
    val words = listOf("apple", "ant", "bee")
    val byFirst = words.associateBy { it.first() }
    println(byFirst)
}

The flatten Function

flatten takes a collection of collections and merges them into one flat list.

fun main() {
    val nested = listOf(listOf(1, 2), listOf(3, 4), listOf(5))
    val flat = nested.flatten()
    println(flat)
}

The flatMap Function

flatMap maps each element to a collection and then flattens all those collections into one list. It is map plus flatten in one step.

fun main() {
    val words = listOf("hi", "ok")
    val chars = words.flatMap { it.toList() }
    println(chars)
}

flatMap on Nested Data

flatMap is ideal for expanding nested structures into a single sequence of items.

data class Team(val members: List<String>)

fun main() {
    val teams = listOf(Team(listOf("Ann", "Bob")), Team(listOf("Cy")))
    val everyone = teams.flatMap { it.members }
    println(everyone)
}

flatMap vs map

Use map when each element produces exactly one result. Use flatMap when each element produces a collection and you want them all combined into one flat list.

fun main() {
    val numbers = listOf(1, 2, 3)
    val mapped = numbers.map { listOf(it, it * 10) }
    val flat = numbers.flatMap { listOf(it, it * 10) }
    println("map: $mapped")
    println("flatMap: $flat")
}

Combining Associate and FlatMap

These tools often appear together in data pipelines: flatten nested data, then associate it into a lookup map for fast access.

data class Order(val items: List<String>)

fun main() {
    val orders = listOf(Order(listOf("pen", "ink")), Order(listOf("pen")))
    val counts = orders.flatMap { it.items }.groupingBy { it }.eachCount()
    println(counts)
}

When to Use Each

Choose associateBy/associateWith to build fast lookup maps from lists, and flatMap/flatten to collapse nested collections. They make working with structured data concise and clear.

Quick Check

What does flatMap do?

Recap

You learned to build maps with associate, associateBy, and associateWith (last duplicate key wins), and to collapse nesting with flatten and flatMap. These complete your collections toolkit for transforming, organizing, aggregating, and restructuring data.

Free to start

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 “Association and Flattening” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “Association and Flattening”, 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 “Association and Flattening”?

associate and 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 4 of 4.

How long does the “Association and Flattening” 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. Filtering and Mapping
  2. Grouping and Partitioning
  3. Aggregation
  4. Association and Flattening
← Back to Kotlin Academy