Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

An unmodifiable list of mutable objects such as List<StringBuilder> is still not immutable. Explain shallow versus deep immutability, and how you would make a deep immutable structure.

An unmodifiable collection only stops structural changes, adding, removing or replacing elements, so the elements themselves can still change: in a List<StringBuilder>, anyone can call append on an element. That is shallow immutability. Deep immutability means every object reachable from the structure is immutable too, so we build it from immutable element types such as String, records of immutable fields and java.time types, and copy nested collections at every level with List.copyOf and Map.copyOf. Guava's ImmutableList is also shallow, so the same rule applies.

Why this question is asked

Teams often protect a collection with List.copyOf and believe the data is now safe to cache or share across threads, but the objects inside the list can still change. This shows up in production as cached values that drift, or as data races inside supposedly immutable configuration. Interviewers ask this to see whether we think about the whole object graph rather than the outer container. In this answer, we will recap what an unmodifiable collection protects, reproduce the change through an element, define shallow and deep immutability, and then build a deep immutable structure step by step, including nested collections.

1A Quick Recap: What an Unmodifiable List Protects

A list is a container of references: each slot points to an element object stored elsewhere on the heap. An unmodifiable list such as one from List.copyOf protects the slots. We cannot add a slot, remove one or make a slot point to a different object. But it says nothing about the objects the slots point to. If those objects have methods that change them, the list cannot stop anyone from calling those methods.

2Reproducing the Problem

StringBuilder is a mutable string, so it makes the point clearly.

Java
List<StringBuilder> names = List.copyOf(List.of(new StringBuilder("ana"), new StringBuilder("bo")));
names.add(new StringBuilder("cy")); // throws UnsupportedOperationException: the structure is protected
names.get(0).append("!!"); // allowed: the element itself is mutable
System.out.println(names); // [ana!!, bo]: the contents changed anyway
What each layer protects
LayerUnmodifiable list of StringBuilderUnmodifiable list of String
Add or remove elementsblockedblocked
Replace an element (set)blockedblocked
Change an element's contentsallowed: append, setLengthimpossible: String has no mutators
Safe to share across threadsnoyes
Only the right-hand column is deeply immutable.

Notice that the list did exactly what it promised: no element was added or replaced. Yet anyone who held names and assumed it would print [ana, bo] forever is now wrong. If two threads did this at the same time, they would also race on the StringBuilder, which is not thread-safe.

3Shallow Versus Deep Immutability

Shallow immutability means the object itself cannot change: its fields are final and its collections cannot be restructured. Deep immutability means that nothing reachable from the object can change, following every reference through every field, every collection and every element, all the way down.

Only deep immutability gives the guarantees we usually want: that a value can be cached without changing, shared between threads without locks, and used as a map key with a stable hashCode. A shallowly immutable structure that contains one mutable object anywhere in its graph loses all three, because that one object can change everything that depends on it.

Step throughDeep immutability checks every level
  1. Catalogue recordfinal fields: shallowly immutable
  2. Map<String, List<Item>>Map.copyOf, unmodifiable
  3. List<Item> valuesList.copyOf at this level too
  4. Item recordfields: String, Money, LocalDate
  5. Field valuesString and java.time: immutable by design

5 layers, from the top down. Step through them one at a time.

One mutable object at any level makes the whole structure only shallowly immutable.

4Use Immutable Element Types

The first and most important step is to choose element types that cannot change. The JDK gives us many: String, the boxed primitives such as Integer, BigDecimal, the java.time types such as Instant and LocalDate, UUID, and enums. For our own data we use records whose fields are themselves immutable types.

Java
record Tag(String name, LocalDate added) {} // both fields immutable: Tag is deeply immutable
List<Tag> tags = List.copyOf(List.of(new Tag("vip", LocalDate.of(2026, 1, 5))));
// nobody can add a tag, replace a tag, or change a tag's name or date

Where we have mutable values from elsewhere, we convert them on the way in: a StringBuilder becomes a String with toString(), a java.util.Date becomes an Instant with toInstant(), and a mutable domain object becomes a record snapshot of its fields.

5Copy Nested Collections at Every Level

Collections of collections need care, because List.copyOf and Map.copyOf are shallow too. Map.copyOf(map) protects the map's keys and the references to its values, but if a value is an ArrayList, that list stays mutable and shared with whoever built it. So we copy every level ourselves, usually in the record's compact constructor.

Java
record Catalogue(Map<String, List<Tag>> tagsByProduct) {
Catalogue {
tagsByProduct = tagsByProduct.entrySet().stream()
.collect(Collectors.toUnmodifiableMap(
Map.Entry::getKey,
e -> List.copyOf(e.getValue()))); // copy each inner list as well
}
}
Map<String, List<Tag>> input = new HashMap<>();
input.put("p1", new ArrayList<>(List.of(new Tag("vip", LocalDate.of(2026, 1, 5)))));
Catalogue c = new Catalogue(input);
input.get("p1").clear(); // does not affect c
System.out.println(c.tagsByProduct().get("p1").size()); // 1

Notice that Map.copyOf(tagsByProduct) alone would not have been enough: the inner ArrayList for p1 would still be shared, and the clear() would have emptied it inside the catalogue too. Collectors.toUnmodifiableMap builds an unmodifiable map just like Map.copyOf, and it also rejects null keys and values.

6Libraries Are Shallow Too

Guava's ImmutableList, ImmutableMap and similar types from other libraries have exactly the same limit. They guarantee that the collection's structure never changes, and they make that visible in the type, which is an advantage over plain List. But an ImmutableList<StringBuilder> is just as changeable as our List.copyOf example, and Guava's documentation says to use it with immutable elements. No collection type can make its elements immutable; only the element types can do that.

7Follow-up questions

Can we make a mutable element type safe without changing its class?

Partly, by copying it whenever it enters or leaves our structure, as we do with Date. But that is fragile and costs allocations on every access. It is better to convert the data into an immutable form once, such as a record with the same fields, and keep only that form inside the structure.

Is a record with a List component deeply immutable?

Only if the compact constructor copies the list with List.copyOf and the element type is itself deeply immutable. A record makes its own fields final but does nothing to the objects they refer to, so both conditions are our responsibility.

Does deep copying large structures cost too much?

It costs one copy at construction. After that, an immutable structure never needs copying again, can be shared freely, and can even reuse unchanged parts when we build a new version, because those parts can never change under us. For data that is built rarely and read often, such as configuration or catalogues, that is a very good trade.

8Conclusion

In this answer, we saw that an unmodifiable collection only stops structural changes, so the elements inside it can still change, as a List<StringBuilder> shows. That is shallow immutability. Deep immutability requires every reachable object to be immutable, which we achieve by using immutable element types such as String, records of immutable fields and java.time types, and by copying nested collections at every level, since List.copyOf and Map.copyOf are shallow. Guava's immutable collections follow the same rule: the structure is fixed, but the elements must be immutable themselves.

What to say in the interviewAbout 80 seconds aloud

Open with

An unmodifiable collection only protects its structure: nobody can add, remove or replace elements. It does nothing to the elements themselves. So in an unmodifiable List of StringBuilder, add throws, but get(0).append still works, and every holder of the list sees the change, and two threads doing it race on a non-thread-safe object.

Then, if they want more

That is shallow immutability. Deep immutability means that every object reachable from the structure is immutable, and only that gives the guarantees we actually want: safe sharing without locks, values that stay the same in a cache, and stable hash codes.

To build a deeply immutable structure, I would first use immutable element types: String, BigDecimal, java.time types, enums, and records whose fields are all of such types, converting mutable inputs like StringBuilder or Date on the way in. Then I copy collections at every level, because List.copyOf and Map.copyOf are shallow as well.

For a Map of String to List, I copy the map and each inner list, for example with Collectors.toUnmodifiableMap and List.copyOf on each value, inside the record's compact constructor. The same applies to Guava's ImmutableList and similar library types: they make the structure immutable and visible in the type, but they cannot make the elements immutable.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "An unmodifiable list of mutable objects such as List<StringBuilder> is still not...": https://scrivio-interview-library.netlify.app/java/immutability/486c6baeba/