CodeGym /Courses /Kotlin SELF /Generics on collections + a minimal data structure for th...

Generics on collections + a minimal data structure for the practical project

Kotlin SELF
Level 19, Lesson 4
Available

1. Why generics are needed and how to read collection types

If you’re just starting out, generics can feel like “those weird angle brackets that make life harder.” But in practice, they’re your insurance against silly mistakes. Kotlin really can infer types, but type inference is not telepathy: the compiler relies on what you give it. If you store data in collections, it matters that the compiler knows exactly what’s inside; otherwise it can’t protect you from accidentally mixing unrelated values.

Imagine a kitchen jar labeled “Sugar.” Generics are that label. You can pour anything into the jar, but if it says “Sugar,” at least you won’t try to scoop “a couple of bolts” into your tea. And vice versa: if you don’t label the jar, one day you’ll find a USB stick in your porridge, because “well, it somehow got in there.”

In Kotlin the label looks like this: List<String>, MutableList<Int>, Map<String, Int>. And it’s not just decoration — it’s a contract that the compiler checks for you. The general idea of generics in Kotlin is described as “generic type parameters” (type parameters).

There’s a moment here when your brain may try to “mute the audio,” so let’s agree on this: we read generics not as “symbols,” but as an English phrase. You see List<T> — say: “a list of elements of type T.” You see Map<K, V> — say: “a map from a key of type K to a value of type V.” After a couple dozen repetitions this becomes automatic, roughly like reading the word println.

Kotlin collections have interfaces and a hierarchy, but today what matters more is the type contract: the container stores elements of a given kind. Roughly:

  • List<T> stores elements in order and provides access by index.
  • Set<T> stores unique elements.
  • Map<K, V> stores “key → value” pairs.

A small “how to read it” table

Type notation How to read it (plain English) Example values
List<String>
a list of strings
["milk", "bread"]
MutableList<Int>
a mutable list of integers
[10, 20, 30]
Set<String>
a set of unique strings
{"kotlin", "jvm"}
Map<String, Int>
a map “string → integer”
{"tea" -> 90}

And yes, this is all not about what “really lies inside”, but about what you are allowed to put in and take out.

Type inference: why a non-empty collection is easy, but an empty one is not

One of the most common beginner situations: “I created an empty list, and Kotlin complains. It’s smart, right?” Kotlin is smart, but not a fortune-teller. If you write mutableListOf() with no elements, the compiler doesn’t see a single value and can’t know which T you meant. And that’s fair: a list “of what exactly” — strings, numbers, triple records, cats.

With non-empty collections it’s simpler: Kotlin sees the elements and infers the type from them. This is standard type inference behavior: you can omit type arguments if they can be inferred unambiguously from context.

Example: the type is inferred automatically because there are elements

fun main() {
    val names = listOf("Ann", "Bob")    // List<String>
    val nums = mutableListOf(1, 2, 3)   // MutableList<Int>

    println(names) // [Ann, Bob]
    println(nums)  // [1, 2, 3]
}

Example: an empty collection — you need to hint the type

fun main() {
    val tags = mutableSetOf<String>()
    tags.add("kotlin")

    println(tags) // [kotlin]
}

Why is everything fine here? Because you wrote <String>, and the compiler calmed down.

Two ways to hint the type

Sometimes it’s more convenient to write the type on the right:

fun main() {
    val ids = mutableListOf<Int>()
    ids.add(10)

    println(ids) // [10]
}

Sometimes — on the left, especially if the type is long:

fun main() {
    val ids: MutableList<Int> = mutableListOf()
    ids.add(10)

    println(ids) // [10]
}

Both options work the same; choose the one that makes the code more readable.

2. The most important truth about Map: map[key] returns V?

With List, things are usually intuitive: the index either exists, or you get an error, and you quickly understand what you did. But Map behaves differently, because a key may be absent — and that is a perfectly normal situation. That’s why when you read by key, Kotlin gives you not V, but V?. Otherwise you’d constantly get crashes in places where you could use polite logic instead.

Example: reading yields Int?

fun main() {
    val prices: Map<String, Int> = mapOf("tea" to 90)

    val teaPrice: Int? = prices["tea"]
    val milkPrice: Int? = prices["milk"]

    println(teaPrice)  // 90
    println(milkPrice) // null
}

Elvis ?: as a fallback plan

fun main() {
    val prices = mapOf("tea" to 90)

    val milkPrice = prices["milk"] ?: 0
    println(milkPrice) // 0
}

And this is not a “hack.” It’s a normal, honest contract: “there may be a value, or there may be null.”

When containsKey is more appropriate than ?:

Sometimes you want to distinguish “no such key” from “the value for the key equals 0.” Then it’s more logical to check whether the key exists first:

fun main() {
    val stock = mapOf("apple" to 0)

    val key = "banana"
    if (stock.containsKey(key)) {
        println("Key exists, value = ${stock[key]}") // will not run
    } else {
        println("No such key") // No such key
    }
}

3. Nested generics without pain: how not to kill readability

At this point many people start writing types that make you want to call a priest and “cleanse the project of demons”: MutableMap<String, MutableList<Triple<String, String, Int>>>. It’s legal, but readability suffers. What matters now is learning two techniques: first, don’t be shy about intermediate val variables; second, make types visible in the code through good variable names.

That means our goal is not “to write the type,” but “to write the type so that people aren’t afraid of it.”

Bad readability, but it compiles

fun main() {
    val data: MutableMap<String, MutableList<Triple<String, String, Int>>> = mutableMapOf()
    println(data) // {}
}

This looks like an organic chemistry formula. Maybe it’s correct, but you don’t want to “drink” it.

Better: split into meaningful chunks

fun main() {
    val byCategory: MutableMap<String, MutableList<Triple<String, String, Int>>> = mutableMapOf()
    val foodRecords: MutableList<Triple<String, String, Int>> = mutableListOf()

    foodRecords.add(Triple("food", "coffee", 300))
    byCategory["food"] = foodRecords

    println(byCategory) // {food=[(food, coffee, 300)]}
}

We haven’t learned “nice collection operations” yet, so we use the basics: create, add, assign by key.

4. A minimal data structure for the practical project

Now we’ll build a data “skeleton” for the project so it’s already useful, but doesn’t require classes or complex architecture. Imagine we’re writing a console mini expense tracker. Each record has a category, a description, and an amount. This maps perfectly to Triple<String, String, Int>: three fields, a simple contract. Yes, Triple is not the most readable format for large projects, but for a start it’s a fine temporary tool—if you’re disciplined about keeping the field order consistent.

In parallel, we need a container that stores many records. For that, MutableList<Record> works, where Record is our Triple<...>.

Step 1: agree on the record shape

fun main() {
    val record = Triple("food", "coffee", 300)

    println(record.first)  // food
    println(record.second) // coffee
    println(record.third)  // 300
}

Yes, first/second/third aren’t a readability dream, but they’ll do for now.

Step 2: store records in a list

fun main() {
    val records: MutableList<Triple<String, String, Int>> = mutableListOf()

    records.add(Triple("food", "coffee", 300))
    records.add(Triple("transport", "taxi", 1200))

    println(records.size) // 2
}

MutableList in Kotlin is the standard mutable list that supports add/remove and other mutation operations.

Step 3: add an identifier without classes

A very common task is being able to reference a record by id. If we had classes, we’d add an id field. But we’re not doing OOP yet, so we use Map<Int, Record>.

It’s important to remember the Map contract: keys are unique, and access by key may return null.

fun main() {
    val byId: MutableMap<Int, Triple<String, String, Int>> = mutableMapOf()

    byId[1] = Triple("food", "coffee", 300)
    byId[2] = Triple("transport", "taxi", 1200)

    val found = byId[2]
    println(found) // (transport, taxi, 1200)
}

Step 4: minimal in-memory project state

Now let’s combine everything: a list of records (to keep insertion order) and a map by id (to find quickly). Yes, that’s two storages, and yes—it’s manual synchronization discipline. But we’re building a minimal version without architectural complexity.

fun main() {
    var nextId = 1
    val records: MutableList<Pair<Int, Triple<String, String, Int>>> = mutableListOf()
    val byId: MutableMap<Int, Triple<String, String, Int>> = mutableMapOf()

    val id = nextId++
    val record = Triple("food", "coffee", 300)

    records.add(id to record)
    byId[id] = record

    println(records) // [(1, (food, coffee, 300))]
}

Here we use to (an infix function) to create a Pair, so the record looks like “id to record.” This reads almost like plain text: “id corresponds to record.”

Generics as API design: List<T> vs MutableList<T>

At this point a subtle but very practical principle appears: if a function should not modify a collection, it’s better to accept a read-only type (List, Map, Set) instead of the mutable version. This makes code safer: you immediately communicate “I’m only reading here.” In Kotlin, separating read-only and mutable collections is a standard idea.

Let’s show it with a small example: we have a function that prints records. It doesn’t need to add/remove; it only needs to iterate and output.

fun printRecords(records: List<Pair<Int, Triple<String, String, Int>>>) {
    for ((id, record) in records) {
        val category = record.first
        val title = record.second
        val amount = record.third

        println("#$id [$category] $title = $amount")
    }
}

fun main() {
    val records = mutableListOf<Pair<Int, Triple<String, String, Int>>>()
    records.add(1 to Triple("food", "coffee", 300))

    printRecords(records)
    // #1 [food] coffee = 300
}

Notice: we store data in a MutableList because we’ll add records, but we pass it to the function as a List because the function is “read-only.”

Key normalization: a habit that saves your Map

A map (Map) is a great tool until you start using “dirty strings” from user input as keys. The user types " Food ", then "food", then "FOOD", and your program suddenly decides those are three different categories. The program isn’t at fault: you gave it three different strings.

So let’s lock in a simple habit: before using a string as a key, normalize it. Here, trim() and lowercase() are enough.

fun normalizeKey(raw: String): String {
    return raw.trim().lowercase()
}

fun main() {
    val categories: MutableSet<String> = mutableSetOf()

    categories.add(normalizeKey(" Food "))
    categories.add(normalizeKey("food"))
    categories.add(normalizeKey("FOOD"))

    println(categories)      // [food]
    println(categories.size) // 1
}

This is a good example of how Set (uniqueness) and string normalization together provide predictable behavior.

5. Typical mistakes

Mistake #1: “Kotlin is smart, it’ll figure it out” — and creating empty collections without a type.
An empty collection contains no elements, so the compiler has nothing to infer T from. As a result, you get a type inference error. The fix is simple: specify the type explicitly either via <T> on the factory (mutableListOf<String>()), or via the type on the left (val xs: MutableList<String> = mutableListOf()).

Mistake #2: confusing Map<K, V> with the result of map[key].
Even if the value type in a Map is not nullable (V), reading by key returns V?, because the key might be absent. If you forget this and try to use the result as “definitely not null,” you’ll quickly hit a compilation error (best case) or start abusing !! (worst case). A normal strategy is ?: or an explicit key-existence check.

Mistake #3: creating monstrous nested generic types without trying to make them readable.
A type like MutableMap<String, MutableList<Triple<String, String, Int>>> may be correct, but it’s hard to read—especially for a beginner (and for you a week later). Intermediate variables with human-friendly names and small steps help: first the list, then the map, then the connection between them.

Mistake #4: passing MutableList and MutableMap to every function “just in case.”
This breaks responsibility boundaries: any function becomes allowed to “accidentally delete half the data.” If a function should only read, accept read-only collection types: List/Map/Set. Kotlin intentionally separates collection interfaces into read-only and mutable so you can write more predictable code.

Mistake #5: using raw user input as a key without normalization.
If you don’t do trim().lowercase(), then "food", " Food ", and "FOOD" become different keys. The program looks “broken,” but in reality it’s just interpreting the string too literally. Make normalization a habit, and Map will start behaving like a tool rather than a riddle generator.

1
Task
Kotlin SELF, level 19, lesson 4
Locked
Cart at startup
Cart at startup
1
Task
Kotlin SELF, level 19, lesson 4
Locked
Tags without chaos
Tags without chaos
1
Task
Kotlin SELF, level 19, lesson 4
Locked
Price guide
Price guide
1
Task
Kotlin SELF, level 19, lesson 4
Locked
Expenses by folders
Expenses by folders
1
Survey/quiz
Collections `List/Set/Map`, level 19, lesson 4
Unavailable
Collections `List/Set/Map`
Collections `List/Set/Map`
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION