liveBook · Manning

Immutable collection

As explained in Advanced Algorithms and Data Structures

Immutability is the property that an existing object or data structure cannot be changed after creation, so updates produce a new structure or version.

When multiple threads read shared data, a change during a read can make the result unreliable. An immutable collection cannot be changed after creation, so readers can use it without a lock to guard against concurrent changes. section 7.7.6

Updates create a new structure and leave the original unchanged. For example, appending to list L1 creates a new list L2 with one additional element. A cache update can likewise return a new cache instance rather than changing the existing one. section 13.2.3 section 7.7.6

Immutability offers practical trade-offs:

  • It can make code easier to reason about because other methods cannot change an object’s state. section 7.7.6
  • It reduces shared state in parallel computations, but can make maintaining running state harder and updating large objects more expensive. section 7.7.6 section 13.2.3
  • Concurrent writes can still race when two threads update the same shared cache; optimistic locking with versioning is one possible response. section 7.7.6

How Advanced Algorithms and Data Structures covers it

How deep it goes
Defines updates as new structures and explains concurrency benefits and trade-offs, without detailing an immutable collection implementation.
Examples
Uses appending to a list and updating a cache to illustrate creating new structures.
What you can do after
Understand how immutability affects concurrent reads, shared state, and the cost of updates.
How it connects
Connects immutability to functional programming, pure functions, concurrency, and MapReduce.

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 Advanced Algorithms and Data Structures. The section links point to the parts of the book it draws on.