Quick revision
The short answer
Collections.unmodifiableList(list) returns a read-only view of an existing list, so changes made to the backing list are visible through it. List.copyOf(list) makes an unmodifiable copy that never changes when the source changes, and List.of(...) creates an unmodifiable list from the elements we give it. List.of and List.copyOf reject null elements, while unmodifiableList shows whatever the backing list holds, nulls included.
What to remember
In this answer, we saw that Collections.unmodifiableList returns a read-only view that still reflects every change to its backing list, which is how an unmodifiable list can change: add to the source and the view grows. List.copyOf makes an independent unmodifiable copy, and returns its argument unchanged when it is already unmodifiable. List.of builds an unmodifiable list straight from elements. Both List.of and List.copyOf reject nulls, while the view shows whatever the backing list holds, and Stream.toList() returns an unmodifiable list that allows nulls.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Collections.unmodifiableList(list) returns a read-only view of an existing list, so changes made to the backing list are visible through it. List.copyOf(list) makes an unmodifiable copy that never changes when the source changes, and List.of(...) creates an unmodifiable list from the elements we give it. List.of and List.copyOf reject null elements, while unmodifiableList shows whatever the backing list holds, nulls included.
Why this question is asked
These three methods all produce a list whose add() throws, so they look interchangeable, and code reviews often treat them that way. They are not: one of them can still change in front of our eyes, which leads to bugs where data shifts after it was supposedly frozen. Interviewers ask this to check that we know the difference between read-only and unchanging. In this answer, we will recap what unmodifiable means, look at each method in turn, reproduce the case where an unmodifiable list changes, compare null handling, and finish with Stream.toList().
1A Quick Recap: Unmodifiable Is Not the Same as Unchanging
An unmodifiable list is one whose mutating methods, add, remove, set and clear, throw UnsupportedOperationException when we call them on it. That stops us from changing the list through that reference. It does not, by itself, say anything about whether the contents can change by some other route.
Java does not have a separate ImmutableList type. All three methods return an ordinary java.util.List, and the difference lies entirely in how each object behaves at run time. That is why we need to know what each method actually builds.
2Collections.unmodifiableList: A Read-Only View
Collections.unmodifiableList wraps an existing list in a thin object that forwards every read to the original and throws on every write. It does not copy anything. The original list, called the backing list, is still there, and anyone with a reference to it can still change it. Every change shows up through the wrapper, because the wrapper has no data of its own.
7 messages in order. Play them, or step through them one at a time.
The view blocks writes through itself but shows every write made to src.
This is the case the question asks for. The list v is unmodifiable, and yet its contents changed, because unmodifiable only describes what v lets us do, not what others can do to src. Views are useful when an object wants to give callers a live, read-only window onto a list it keeps changing, but they are not a snapshot.
3List.copyOf: An Unmodifiable Snapshot
List.copyOf, added in Java 10, copies the elements of a collection into a new unmodifiable list. After that, the copy and the source are completely independent: changes to the source do not affect the copy, and the copy cannot be changed at all. It is the right choice when we receive a list and want to keep it safely, for example in a constructor.
Notice the last line. If the argument was itself made by List.of or List.copyOf, copyOf returns the same object, since it can never change. Copying a view, though, always makes a real copy, because a view can still change through its backing list.
4List.of: A New Unmodifiable List From Elements
List.of, added in Java 9, builds an unmodifiable list directly from the elements we pass. There is no backing list, so nothing can change it. It is the shortest way to write a constant list, such as default roles or test data.
List.of and List.copyOf both produce what the JDK documentation calls unmodifiable lists: they cannot be changed, they reject nulls, and they are value-based, so we should not rely on their identity or synchronise on them.
5How Each One Handles Null
The three methods differ in one more way that surprises people. List.of and List.copyOf reject null elements with a NullPointerException, and they even throw on contains(null) and indexOf(null). Collections.unmodifiableList accepts whatever the backing list holds, nulls included, because it just forwards to it.
| Property | unmodifiableList(list) | List.copyOf(list) | List.of(...) |
|---|---|---|---|
| Copies the elements | no, wraps the original | yes | builds from arguments |
| Changes when source changes | yes | no | no source |
| add, set, remove | throw | throw | throw |
| Null elements | allowed | NullPointerException | NullPointerException |
contains(null) | false or true | NullPointerException | NullPointerException |
| Since | Java 1.2 | Java 10 | Java 9 |
So switching from one to another can change behaviour for data that contains nulls. If null elements are legitimate, we copy into a new ArrayList and wrap it: Collections.unmodifiableList(new ArrayList<>(src)). Since nothing else holds that new ArrayList, the result is effectively unchanging.
6Follow-up questions
What does Stream.toList() return?
Since Java 16, Stream.toList() returns an unmodifiable list: add and set throw UnsupportedOperationException. Unlike List.of and List.copyOf, it allows null elements, because streams can contain nulls. That is different from Collectors.toList(), which makes no promise about mutability and in practice returns an ArrayList that we can change. When we switch from collect(Collectors.toList()) to toList(), any code that later adds to the result will start failing.
Is Arrays.asList unmodifiable?
Only partly. Arrays.asList returns a fixed-size list backed by the array: add and remove throw, but set works and writes into the array, and changes to the array show up in the list. It is a view, like unmodifiableList, but one that allows replacing elements.
Which should a constructor use to store a list argument?
List.copyOf, because it gives an independent, unmodifiable copy, so neither the caller nor any code we return the list to can change our state, and the getter can return it without copying again. If nulls must be allowed, use an unmodifiable wrapper around a fresh ArrayList.
7Conclusion
In this answer, we saw that Collections.unmodifiableList returns a read-only view that still reflects every change to its backing list, which is how an unmodifiable list can change: add to the source and the view grows. List.copyOf makes an independent unmodifiable copy, and returns its argument unchanged when it is already unmodifiable. List.of builds an unmodifiable list straight from elements. Both List.of and List.copyOf reject nulls, while the view shows whatever the backing list holds, and Stream.toList() returns an unmodifiable list that allows nulls.
Open with
All three return a list whose add throws, but they are different things. Collections.unmodifiableList wraps an existing list in a read-only view; it copies nothing, so if anyone changes the backing list, the view shows it.
Then, if they want more
For example, if I create src as an ArrayList containing 1, wrap it as v, and then call src.add(2), v now has two elements, even though v.add would throw. So it is unmodifiable but not unchanging.
List.copyOf, from Java 10, makes a separate unmodifiable copy, so later changes to the source do not affect it, and if I pass it a list that is already unmodifiable, from List.of or copyOf, it just returns the same instance. List.of, from Java 9, builds an unmodifiable list from the given elements with no backing list at all.
The other difference is nulls: List.of and List.copyOf throw NullPointerException for null elements, even on contains(null), while the view allows whatever the backing list holds. In constructors I use List.copyOf to store list arguments. And as a related point, Stream.toList returns an unmodifiable list that does allow nulls, unlike Collectors.toList, which in practice gives a mutable ArrayList.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 10, Java 16, Java 9
- 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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "What is the difference between Collections.unmodifiableList(list), List.copyOf(list) and...": https://scrivio-interview-library.netlify.app/java/immutability/59b6de5099/