Android Academy · Lesson

Kotlin Collections

Work with List, Set, and Map. Use functional operators like filter, map, sortedBy, forEach, find, any, all, and chain operations for concise data transformations.

Lesson 1 of 611 steps

Kotlin Collections is a free Android Academy lesson on CoddyKit. This is lesson 1 of 6. 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 Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 6 lessons in total.

Collections Overview

Kotlin's standard library provides three main collection types:

  • List — ordered sequence of elements (can have duplicates)
  • Set — unique elements, no specific order
  • Map — key-value pairs, keys are unique

Each type has an immutable version (read-only) and a mutable version (can be modified).

Lists: listOf vs mutableListOf

Create lists using factory functions:

fun main() {
    // Immutable — cannot add/remove items
    val fruits = listOf("Apple", "Banana", "Cherry")
    println(fruits[0])       // Apple
    println(fruits.size)     // 3

    // Mutable — can modify
    val scores = mutableListOf(10, 20, 30)
    scores.add(40)
    scores.removeAt(0)       // remove index 0
    println(scores)          // [20, 30, 40]
}

Map: Key-Value Pairs

Use mapOf and mutableMapOf to create maps. Access values by key:

fun main() {
    val capitals = mapOf(
        "Turkey" to "Ankara",
        "Germany" to "Berlin",
        "Japan" to "Tokyo"
    )

    println(capitals["Turkey"])      // Ankara
    println(capitals["France"])      // null (not found)
    println(capitals.getOrDefault("France", "Unknown")) // Unknown

    // Iterate
    for ((country, city) in capitals) {
        println("$country -> $city")
    }
}

filter — Select Items

filter returns a new list containing only elements that match a condition:

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8)

    val evens = numbers.filter { it % 2 == 0 }
    println(evens)   // [2, 4, 6, 8]

    val names = listOf("Alice", "Bob", "Anna", "Charlie")
    val aNames = names.filter { it.startsWith("A") }
    println(aNames)  // [Alice, Anna]
}

map — Transform Items

map transforms every element and returns a new list:

fun main() {
    val prices = listOf(10.0, 20.0, 30.0)

    // Apply 10% tax
    val withTax = prices.map { it * 1.1 }
    println(withTax)  // [11.0, 22.0, 33.0]

    val names = listOf("alice", "bob", "carol")
    val upper = names.map { it.uppercase() }
    println(upper)    // [ALICE, BOB, CAROL]
}

sortedBy & sortedByDescending

Sort a list by a property:

data class Student(val name: String, val grade: Int)

fun main() {
    val students = listOf(
        Student("Alice", 88),
        Student("Bob", 95),
        Student("Carol", 72)
    )

    val byGrade = students.sortedByDescending { it.grade }
    for (s in byGrade) println("${s.name}: ${s.grade}")
    // Bob: 95
    // Alice: 88
    // Carol: 72
}

forEach & find

Two more useful collection functions:

  • forEach — iterate without building a result
  • find — return the first item matching a condition, or null

Chaining Operations

Chain multiple operations for concise data transformations:

data class Product(val name: String, val price: Double, val inStock: Boolean)

fun main() {
    val products = listOf(
        Product("Laptop", 999.0, true),
        Product("Mouse", 29.0, false),
        Product("Keyboard", 79.0, true),
        Product("Monitor", 349.0, true)
    )

    // In-stock products under $500, sorted by price
    val result = products
        .filter { it.inStock && it.price < 500 }
        .sortedBy { it.price }
        .map { "${it.name}: ${it.price}" }

    result.forEach { println(it) }
}

any, all, count

Aggregate functions for quick answers:

  • any { condition } — true if at least one item matches
  • all { condition } — true if every item matches
  • count { condition } — number of matching items
  • sumOf { it.price } — sum a numeric property

Quick Check

Which function would you use to get only the items in a list that satisfy a condition?

Recap: Kotlin Collections

You can now work with data in Kotlin efficiently:

  • listOf / mutableListOf, mapOf / mutableMapOf
  • filter — select matching items
  • map — transform items
  • sortedBy / sortedByDescending
  • Chain operations for clean, readable data pipelines

Next: display lists on screen with RecyclerView.

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
36
Lessons
152

Frequently Asked Questions

Is the “Kotlin Collections” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “Kotlin Collections”, 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 Android Academy course includes 6 lessons in total.

What will I learn in “Kotlin Collections”?

Work with List, Set, and Map. Use functional operators like filter, map, sortedBy, forEach, find, any, all, and chain operations for concise data transformations. You practise Android 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 Android Academy?

No prior experience is required. Android 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 6.

How long does the “Kotlin Collections” 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 Android Academy lesson?

Yes. Every Android 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. Kotlin Collections
  2. RecyclerView Basics
  3. RecyclerView Click Events
  4. SharedPreferences
  5. DataStore Preferences
  6. Adapters & DiffUtil
← Back to Android Academy