0Pricing
Kotlin Academy · Lesson

Association and Flattening

associate and flatMap.

Association and Flattening 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.

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.

Frequently asked questions

Is the “Association and Flattening” lesson free?

Yes — the full text of “Association and Flattening” 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 “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; 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 “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