liveBook · Manning

Immutable collection

As explained in Kotlin in Action, 2E

Immutability is the property that an object's state cannot change after the object is created.

An immutable collection never changes after creation. That stability makes shared data easier to reason about: no caller can alter the collection behind another caller’s back. It also avoids unsafe shared modifications that complicate multithreaded code. Kotlin’s functional style favors immutable data and pure functions for these reliability and testing benefits. section 1.2.3 section 1.6

Do not equate Kotlin’s read-only collection types with immutable collections. A `Collection<T>` reference prevents that reference from calling mutation methods, while `MutableCollection<T>` exposes operations such as `add`. Both references can still designate the same mutable object, so code with a read-only view may observe changes made elsewhere. section 8.2.2

Figure 8.5 The Collections interface is read-only. MutableCollection extends it and adds methods to modify a collection’s contents.
CH08_F05_Isakova

The type hierarchy separates read-only access from mutation-capable access: `MutableCollection` extends `Collection` with operations that change contents. section 8.2.2

Accept source data without granting mutation

`copyElements` takes its input as `Collection<T>` and its destination as `MutableCollection<T>`. The signature documents the intended roles: iteration is allowed for the source, whereas adding elements is allowed only for the target. Passing a variable typed as `Collection<Int>` as the target fails at compile time. section 8.2.2

A read-only view is not automatically thread safe. Its underlying collection may be mutable and may be changed concurrently through another reference, causing problems such as `ConcurrentModificationException`. Synchronize access or use data structures designed for concurrent access when necessary. section 8.2.2

For truly immutable Kotlin collections, use `kotlinx.collections.immutable`; the standard library does not provide them. Immutability also matters for data-class instances used as `HashMap` keys: changing key state after insertion can leave the container invalid. Prefer `val` properties, and create a changed copy rather than modifying the original. section 8.2.2 section 4.3.2

Copy instead of mutate

```kotlin data class Customer(val name: String, val postalCode: Int) val bob = Customer("Bob", 973293) val moved = bob.copy(postalCode = 382555) ``` `moved` is a separate instance. Updating it through `copy` cannot affect code that still refers to `bob`. section 4.3.2

How Kotlin in Action, 2E covers it

How deep it goes
Distinguishes immutable collections from read-only views and explains aliasing, thread-safety limits, and immutable data-class updates.
Examples
Uses collection source-and-target typing and a Customer data-class copy with a changed postal code.
What it assumes
Assumes familiarity with Kotlin collection types and introduces the distinction between Collection and MutableCollection.
What you can do after
Evaluate/understand whether Kotlin collection access is truly immutable or merely read-only, and choose copying or synchronization accordingly.
How it connects
Connects immutability with pure functions, functional programming, data classes, HashMap keys, and mutable collections.

Also explained in

Compare how 24 books explain immutable collection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.