CodeGym /Courses /Kotlin SELF /Object collections: MutableList<Expense>, operations, and...

Object collections: MutableList<Expense>, operations, and migration from Pair

Kotlin SELF
Level 30, Lesson 3
Available

1. Expense instead of Pair: readability and meaning

When we were just learning collections, it felt very natural to store an expense as Pair<String, Int>: "Coffee" to 300. This is an honest beginner’s path: fast, compact, and your brain doesn’t overheat. But Pair has a “side effect”: a week later you open the code and see it.second, and a small internal panic starts — “is second the amount? the category? the quantity? the stress level?”.

The Expense class solves this in a simple way: it provides names. We no longer work with “first” and “second”; we work with title and amount. And the best part is that all collection tools (loops, filter, map, sortedByDescending, fold) keep working — the lambdas just become clearer.

Let’s compare “how it was” and “how it became” in one table:

Approach What an element looks like How the code reads Typical feelings
Pair<String, Int>
"Coffee" to 300
it.first, it.second
“hope I didn’t mix it up…”
Expense
Expense("Coffee", 300)
it.title, it.amount
“oh, at least this is human-readable”

A mini-starter for our app (a console expense tracker) will look like this:


class Expense(val title: String, val amount: Int)

2. Creating a collection: mutableListOf<Expense>()

Let’s move to practice. We want to store many expenses, so we need a collection. In this course we use MutableList, because the list must be able to grow and shrink: today you bought coffee — add it; tomorrow you found a mistake — delete it.

An important point that often surprises beginners: you can store the list in a val and still change its contents. val forbids reassigning the reference, but it does not forbid calling methods of a mutable object (for example, add). This is a separate useful habit: keep the reference stable.

Example:

fun main() {
    val expenses = mutableListOf<Expense>()

    expenses.add(Expense("Coffee", 300))
    expenses.add(Expense("Taxi", 1200))

    println(expenses.size) // 2
}

Notice: expenses is a val, but .add(...) works just fine, because you change the list’s contents, not the variable.

3. Printing the expense list

When a list stores numbers, we often print them directly. When a list stores objects, printing becomes formatting. The user (and you in two days) needs lines like 1) Coffee — 300.

First, let’s make a simple print function. Important: we’re not diving into “pretty printers” yet; we need a simple, clear loop.

class Expense(val title: String, val amount: Int)

fun printExpenses(expenses: List<Expense>) {
    if (expenses.isEmpty()) {
        println("No expenses yet.")
        return
    }

    for (i in expenses.indices) {
        val e = expenses[i]
        println("${i + 1}) ${e.title} — ${e.amount}")
    }
}

Here we use indices so we don’t mess up the list bounds. If we did 0..expenses.size, we would accidentally hit index size (and that is already “outside the fence”).

Let’s test it:

fun main() {
    val expenses = mutableListOf(
        Expense("Coffee", 300),
        Expense("Taxi", 1200),
    )

    printExpenses(expenses)
    // 1) Coffee — 300
    // 2) Taxi — 1200
}

4. Adding an expense

Lists become truly useful when users add things to them. Let’s create the add command: it will ask for a title and an amount, create an Expense, and add it to expenses.

To avoid turning main() into “spaghetti with ketchup”, let’s move amount input into a small function. We already know trim() and toIntOrNull(), so we follow the classic pattern: “read → sanitize → parse”.

fun readIntOrNull(prompt: String): Int? {
    print(prompt)
    return readln().trim().toIntOrNull()
}

Now the add function:

class Expense(val title: String, val amount: Int)

fun addExpense(expenses: MutableList<Expense>) {
    print("Title: ")
    val title = readln().trim()

    val amount = readIntOrNull("Amount: ")
    if (title.isBlank() || amount == null || amount <= 0) {
        println("Error: you need a non-empty title and an amount > 0.")
        return
    }

    expenses.add(Expense(title, amount))
    println("Added: $title — $amount")
}

Pay attention to the style: we do simple checks and an early return. This keeps the code linear and readable.

5. Removing an expense: removeAt() and indices

Removal is a favorite place where beginners meet “index out of bounds”. And usually the meeting is sudden, without warning, and at high speed.

We will delete by the number the user sees in the list (starting from 1), but removeAt() works with indices (starting from 0). So we convert: number -> index = number - 1, and we must check bounds via indices.

fun removeExpense(expenses: MutableList<Expense>) {
    if (expenses.isEmpty()) {
        println("Nothing to remove: the list is empty.")
        return
    }

    val number = readIntOrNull("Expense number to remove: ")
    if (number == null) {
        println("Error: enter a number.")
        return
    }

    val index = number - 1
    if (index !in expenses.indices) {
        println("Error: there is no expense with number $number.")
        return
    }

    val removed = expenses.removeAt(index)
    println("Removed: ${removed.title} — ${removed.amount}")
}

Here is the key idea: a list is a “living organism”. After removal, elements shift, and that’s normal. So if the user remembers that “taxi was number 2”, and then removes item 1, taxi becomes number 1. That’s not a bug — it’s math.

6. Operations on objects in collections

Now for the pleasant part. The operations you already used on strings and numbers apply to objects in exactly the same way. The only difference is that in a lambda you usually take a field: it.amount, it.title.

filter + map: large expenses and their titles

Operations map() and filter() are basic collection transformations.

Example: find expenses >= 1000 and get their titles.

fun largeExpenseTitles(expenses: List<Expense>): List<String> {
    return expenses
        .filter { it.amount >= 1000 }
        .map { it.title }
}

Test:

fun main() {
    val expenses = listOf(
        Expense("Coffee", 300),
        Expense("Taxi", 1200),
        Expense("Lunch", 900),
    )

    println(largeExpenseTitles(expenses)) // [Taxi]
}

sortedByDescending: sort by amount

Sorting reads especially well on objects, because you explicitly say “sort by amount”:

fun topExpenses(expenses: List<Expense>, n: Int): List<Expense> {
    return expenses
        .sortedByDescending { it.amount }
        .take(n)
}

Test:

fun main() {
    val expenses = listOf(
        Expense("Coffee", 300),
        Expense("Taxi", 1200),
        Expense("Laptop stand", 2500),
    )

    val top2 = topExpenses(expenses, 2)
    for (e in top2) {
        println("${e.title} — ${e.amount}")
    }
    // Laptop stand — 2500
    // Taxi — 1200
}

fold: compute the total

fold is a neat reduction of a collection into a single value. In our case — the total.

fun totalAmount(expenses: List<Expense>): Int {
    return expenses.fold(0) { acc, e -> acc + e.amount }
}

Test:

fun main() {
    val expenses = listOf(
        Expense("Coffee", 300),
        Expense("Taxi", 1200),
    )

    println(totalAmount(expenses)) // 1500
}

To cement it “in your head”, here’s a mini pipeline diagram (when you build a report):

flowchart TD
    A[expenses: List⟨Expense⟩] --> B[filter amount >= 1000]
    B --> C[sortedByDescending amount]
    C --> D["take(3)"]
    D --> E[map to 'title — amount']
    E --> F[joinToString + print]

7. Mini CLI: add/list/remove/total/top/exit

Now let’s assemble everything into a minimal but complete “expense tracker”. We’re not building the perfect architecture; we’re building a readable skeleton that shows how a list of objects lives and gets updated.

Command loop

main() with a command loop:

fun main() {
    val expenses = mutableListOf<Expense>()

    while (true) {
        print("> ")
        when (readln().trim().lowercase()) {
            "add" -> addExpense(expenses)
            "list" -> printExpenses(expenses)
            "remove" -> removeExpense(expenses)
            "total" -> println("Total: ${totalAmount(expenses)}")
            "exit" -> return
            else -> println("Commands: add, list, remove, total, exit")
        }
    }
}

Command top: a quick report

And a small bonus — the top command, to feel the pipeline joy:

fun printTop(expenses: List<Expense>) {
    val top = expenses.sortedByDescending { it.amount }.take(3)

    if (top.isEmpty()) {
        println("No expenses yet.")
        return
    }

    println("Top-3 expenses:")
    for (e in top) {
        println("${e.title} — ${e.amount}")
    }
}

If you want to wire it into main():

"top" -> printTop(expenses)

Migration from Pair to Expense without a “big bang”

In practice, migration is rarely “one commit and no pain”. Usually part of the code still lives with the old format, and part with the new one. And that’s fine — the main thing is to have a clear bridge between them.

Let’s imagine we still have an old collection somewhere:

val legacy: MutableList<Pair<String, Int>> = mutableListOf(
    "Coffee" to 300,
    "Taxi" to 1200
)

Let’s make a migration function:

fun migrate(pairs: List<Pair<String, Int>>): MutableList<Expense> {
    return pairs.map { (title, amount) -> Expense(title, amount) }.toMutableList()
}

Here we use pair destructuring (title, amount). After migration, all “domain code” is better written through Expense.

Test:

fun main() {
    val legacy = listOf("Coffee" to 300, "Taxi" to 1200)
    val expenses = migrate(legacy)

    printExpenses(expenses)
    // 1) Coffee — 300
    // 2) Taxi — 1200
}

If you need to temporarily convert back (for example, some old report still expects Pair), you can do that too, but it’s better to treat it as a temporary hack:

fun toPairs(expenses: List<Expense>): List<Pair<String, Int>> {
    return expenses.map { it.title to it.amount }
}

A nuance about references: what’s inside MutableList<Expense>

Now comes a topic where people often “float”, because you can’t see it with your eyes. When a list stores Int, it’s simple: those are values. When a list stores objects, it stores references to objects. That means a list element is not a “copy of an expense” but a “pointer to the same object”.

To feel it in your hands, let’s take an intentionally mutable class:

class Tag(var name: String)

fun main() {
    val tags = mutableListOf(Tag("food"))

    val t = tags[0]
    t.name = "groceries"

    println(tags[0].name) // groceries
}

We changed t.name, and it’s immediately visible through tags[0], because t and tags[0] point to the same object.

This is not “scary” or “bad”. It’s just the object model. But it implies a practical rule: if you make object properties var, then changing an object in one place can “surface” in another place, because it’s still the same object.

8. Typical mistakes

Mistake #1: keep storing domain entities in Pair because “it’s shorter”.
In the short run, Pair really does seem faster. But as soon as a pair has meaning (“expense title” and “expense amount”), first/second starts to get in the way of reading. Moving to Expense(title, amount) usually makes the code longer by a couple of characters, but it reduces the number of “wait, what is this?” moments by an order of magnitude.

Mistake #2: create parallel lists instead of one list of objects.
A common anti-pattern: val titles = mutableListOf<String>() and val amounts = mutableListOf<Int>(), and then you add/remove elements “in sync”. This works exactly until the first bug, after which the lists desynchronize, and you suddenly get “Taxi — 300” and “Coffee — 1200”. A single MutableList<Expense> removes this entire class of problems.

Mistake #3: forget the index shift (number starts at 1, index starts at 0).
The user enters “1”, and you habitually do removeAt(1) and delete the second element. That’s the kind of bug that makes people suspect the computer doesn’t respect them. The fix is simple: do index = number - 1 and check index in expenses.indices.

Mistake #4: don’t check bounds before removeAt() and index access.
removeAt() is not required to “guess” that the user entered 999. It will honestly throw an exception. In a learning project, this looks like “the program crashed and rode off into the sunset”. So checking via indices is not bureaucracy — it’s basic kindness to your future self.

Mistake #5: turn filter/map/sortedByDescending chains into unreadable “noodles”.
Collection operations are powerful, and that creates the temptation to write one half-screen line. If you can’t quickly read it yourself a week later, it’s better to split it into intermediate vals with meaningful names. This is not “extra code” — it’s insurance against your own eyes.

Mistake #6: think that val expenses = mutableListOf(...) forbids changes to the list.
This is a common confusion: val forbids reassigning the variable, but it does not forbid changing the object it points to. For MutableList that means add/remove are allowed, and that’s normal Kotlin behavior.

1
Task
Kotlin SELF, level 30, lesson 3
Locked
Expense list
Expense list
1
Task
Kotlin SELF, level 30, lesson 3
Locked
Numbered receipt
Numbered receipt
1
Task
Kotlin SELF, level 30, lesson 3
Locked
Quick add
Quick add
1
Task
Kotlin SELF, level 30, lesson 3
Locked
Delete by number
Delete by number
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION