skip to content

How do List.of / Set.of / Map.of differ from Collections.unmodifiableList / unmodifiableSet / unmodifiableMap?

level: middleimportance: must knowfreq 74%

answer

  1. Wrapper = view (shares backing); factory = standalone value
  2. unmodifiable* leaks if backing is mutated
  3. copyOf = unmodifiable snapshot
  4. Factories reject null; wrappers don't
  5. Set/Map.of order unspecified

basics

~20 s

List.of, Set.of and Map.of build a self-contained unmodifiable collection with no backing collection to change. The Collections.unmodifiable* methods only wrap an existing collection in a read-only view, so if the original changes, the view changes too.

solid answer

~40 s

Both block mutation through the reference you hold, but the guarantee differs. Collections.unmodifiableList returns a thin read-only *wrapper* around an existing collection — it forwards reads to the backing list and throws on writes. Crucially, it does not copy: if anyone still holds the original and mutates it, those changes are visible through the wrapper, so it is only conditionally immutable. List.of, by contrast, returns a self-contained instance with no live backing collection to leak through. Other differences: factory collections reject null elements/keys/values (the wrappers allow nulls if the backing collection does), factories use compact layouts where a wrapper adds an object, and Set.of/Map.of give an unspecified, run-varying iteration order. For a snapshot of existing data, use List.copyOf (Java 10+) or copy-then-wrap, and never wrap a collection you keep a mutable reference to.

code

java · 11 lines
java
List<String> original = new ArrayList<>(List.of("a"));

List<String> view = Collections.unmodifiableList(original);
original.add("b");
System.out.println(view);   // [a, b]  -- view reflects the change

List<String> snapshot = List.copyOf(original);
original.add("c");
System.out.println(snapshot); // [a, b]  -- copy is independent

List<String> literal = List.of("x", "y"); // unmodifiable, no backing list

go deeper

for a junior

Knows both prevent modification through the returned reference.

for a middle

Explains that unmodifiable* is a non-copying view that leaks backing mutations, while factories are standalone with no backing collection, plus null handling.

for a senior

Adds copyOf for snapshots, memory/order differences, and guidance on which to use when returning collections from APIs.

for a principal

Sets team conventions: prefer immutable returns to eliminate defensive copying, document escape-hatch cases for views, and reason about leakage in concurrent code.

## Two ways to get a "you can't change this" collection Java has long offered `Collections.unmodifiableList(...)`, `unmodifiableSet(...)`, and `unmodifiableMap(...)`. Java 9 added `List.of`, `Set.of`, `Map.of`. They look similar but the *kind* of guarantee is different. The Javadoc's terms make it precise: the wrappers return an **unmodifiable view**, the factories an **unmodifiable collection**. ## What a wrapper is A *wrapper* (or *view*) is an object that holds a reference to another object and forwards calls to it. `Collections.unmodifiableList(original)` returns a small object that says: for read methods (`get`, `size`, `iterator`), ask `original`; for write methods (`add`, `set`, `remove`), throw `UnsupportedOperationException`. The important consequence: the wrapper shares the *same underlying data* as `original`. It does **not** copy. So: ```java List<String> original = new ArrayList<>(List.of("a")); List<String> view = Collections.unmodifiableList(original); original.add("b"); // mutate the backing list System.out.println(view); // [a, b] <-- the view changed! ``` The view is read-only *through that reference*, but it is not immutable as a value — it is only as stable as whoever still holds `original`. This is sometimes called a "view of" rather than a "copy of." ## What the factories give `List.of("a", "b")` returns a self-contained collection. There is no separate backing collection that someone else could hold and mutate, so which elements it holds is fixed for its entire life: there is nothing to leak. A mutable element can still change internally, which is why the Javadoc avoids the word immutable here. ## Other concrete differences 1. **Null handling.** Factory methods throw `NullPointerException` on a null element, key, or value. The `unmodifiable*` wrappers happily contain nulls if the backing collection did. Lookups differ too: `List.of(1).contains(null)` throws `NullPointerException`, while a wrapper over an `ArrayList` answers `false`. 2. **Memory layout.** Factories use small internal classes (in the current JDK, the smallest sizes in fields, the rest in an array); a wrapper is one more object on top of its backing collection. JEP 269 counts six objects for a two-element `HashSet` behind a wrapper. 3. **Iteration order.** `Set.of` and `Map.of` have *unspecified* order that may even change between runs. `Collections.unmodifiableSet(new LinkedHashSet<>(...))` preserves whatever order the backing set had. 4. **Identity.** Neither family promises a new object. The factories' Javadoc says to make no assumptions about identity, and empty results are shared in the current JDK. Since JDK 17 `unmodifiableList` returns its argument when the argument is already such a view, rather than wrapping it again. ## How to make a snapshot of existing data If you have a mutable collection and want an unmodifiable, independent copy, *copy first*: ```java List<String> snapshot = List.copyOf(original); // Java 10+ ``` `List.copyOf` / `Set.copyOf` / `Map.copyOf` (Java 10) take any source and return an unmodifiable result holding its current contents, under the factory rules, so a null in the source throws `NullPointerException`. Given a source that is already such a collection they generally return it rather than copy, which is safe because it cannot change. A `Collections.unmodifiableList` view does get copied. Wrapping without copying (`unmodifiableList(original)`) is the classic mistake when you wanted a snapshot. ## Rule of thumb - New, fixed literal data → `List.of(...)`. - Unmodifiable copy of something you already have → `List.copyOf(...)`. - Read-only *view* that should intentionally reflect future changes to the backing collection → `Collections.unmodifiableList(...)`.

  • If I wrap a mutable list with Collections.unmodifiableList and then add to the original, what does the wrapper show?
    The wrapper reflects the new element, because it is a live view over the same backing list, not a copy.
  • What is the right way to get an immutable, independent copy of an existing collection?
    Use List.copyOf / Set.copyOf / Map.copyOf (Java 10+), which hold the current contents in an unmodifiable collection and generally return the argument if it is already one. A null in the source throws NullPointerException.

  • Claiming unmodifiableList copies the data (it does not)
  • Saying both are equally immutable regardless of who holds the original
  • Using unmodifiableList(original) when you needed a snapshot
  • Assuming List.copyOf always allocates a new list

List.of, Set.of and Map.of
Java 9
List.copyOf, Set.copyOf and Map.copyOf
Java 10
Collections.unmodifiableList and siblings
Java 1.2

Assumes JDK 27, the same at JDK 25. The factories date from Java 9 and copyOf from 10; since 17 the unmodifiable wrappers no longer re-wrap one of their own views.

context