Skip to workspace
Kotlin Lab

Working with data / Chapter 02

Freeze the dispatch queue

Distinguish a read-only reference from a snapshot of a mutable collection.

The decision

A dispatcher edits tomorrow’s route after the confirmation screen opens. Capture the stop names as they stand now so subsequent edits to the queue do not alter the confirmation.

Read the Kotlin

Distinguish another name from another container

Assigning a MutableList to a List variable gives read-only access to the same container. Indexing it later reads whatever the owner currently has at that index. A call to toList() captures the element sequence at the time of the call. Replacing or clearing source entries then affects only the live view.

val live = mutableListOf("Dock")
val view: List<String> = live
val saved = live.toList()
live[0] = "Yard"    // view[0] is Yard, saved[0] is Dock

Look inside one element of a shallow snapshot

Imagine two lists with arrows pointing to the same delivery card. Removing an arrow from the source leaves the snapshot’s arrow intact. Editing the title on the card changes what both arrows lead to. If a later reader needs the old title, capture the title itself or copy the mutable card state when taking the snapshot.

Choose the smallest capability the work needs

A parameter of type List lets you iterate, index and count. For a short-lived count, reading the caller’s list is sufficient. If the algorithm must insert its own temporary detour, take a mutable copy at entry. The caller can pass a read-only list and retains its original order.

val working = stops.toMutableList()
working.add(0, "Inspection")

The full example

Java 25
import java.util.List;

class Lesson {
    static List<String> snapshot(List<String> stops) {
        return List.copyOf(stops);
    }
}
Kotlin/JVM
fun snapshot(stops: List<String>): List<String> =
    stops.toList()

Inputs and outcomes

InputOutcomeWhy it matters
Capture [Port, Mill], then clear the queue[Port, Mill]The confirmation retains both stops.
Capture [], then add Pier[]Even an empty confirmation stays empty.
Capture [Pier, Pier][Pier, Pier]Order and repeated stops both matter.

Walk through the code

  1. Passing a MutableList to a List parameter narrows the operations that this function can call. It does not sever the connection to the caller’s list.
  2. toList captures the elements now. When the dispatcher clears the source, the captured list still contains the same sequence of strings. Java’s List.copyOf also captures the contents and returns an unmodifiable list.
  3. The contract uses non-null String stops. List.copyOf rejects null elements. Kotlin List<String?>.toList() can retain nulls, so widening the element type changes the Java comparison.

The Java habit

Returning stops as List<String> compiles and hides add from the Kotlin caller. The queue owner can still mutate the same list behind that read-only reference.

When to choose differently

A live dispatch monitor should see queue edits. A one-time snapshot would go stale there; expose a deliberate update mechanism instead.

References

Open quizTry code