Quick revision
The short answer
List.of returns an unmodifiable list, so Collections.sort, add and remove on it throw UnsupportedOperationException; we either copy it into a mutable list first, new ArrayList<>(List.of(3, 1, 2)), or produce a sorted copy with stream().sorted().toList(). List.of also rejects null elements entirely, and its contains(null) throws NullPointerException instead of returning false. Remember that Collections.unmodifiableList is only a read-only view of a modifiable list, while List.of and List.copyOf are true immutable lists.
What to remember
In this answer, we saw that List.of returns an unmodifiable list, so sort, add and remove throw UnsupportedOperationException, and we fixed it by copying into an ArrayList or producing a sorted copy with stream().sorted().toList(). List.of also rejects nulls entirely, so contains(null) throws NullPointerException, and we handle null before querying. Finally we compared immutable lists such as List.of and List.copyOf with the read-only view from Collections.unmodifiableList, and with Stream.toList() and Collectors.toList().
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.
List.of returns an unmodifiable list, so Collections.sort, add and remove on it throw UnsupportedOperationException; we either copy it into a mutable list first, new ArrayList<>(List.of(3, 1, 2)), or produce a sorted copy with stream().sorted().toList(). List.of also rejects null elements entirely, and its contains(null) throws NullPointerException instead of returning false. Remember that Collections.unmodifiableList is only a read-only view of a modifiable list, while List.of and List.copyOf are true immutable lists.
Why this question is asked
Since Java 9 and 10, List.of, List.copyOf and Stream.toList have become the normal way to create small lists, and with them came two exceptions that surprise developers used to ArrayList. Interviewers ask this question to check that we know the contract of the immutable collections, not just their syntax, and that we can tell an immutable list from an unmodifiable view. In this answer, we will reproduce both exceptions, explain why each one is deliberate, fix them, and then compare the different kinds of read-only lists Java offers.
1Reproducing Both Exceptions
Here are the two failures from the question.
Both are thrown on purpose by the immutable list implementation. Neither is a bug in the JDK; both are part of the documented contract of List.of.
2Why Sorting Throws: List.of Is Unmodifiable
List.of returns an unmodifiable list: no method can change its size or its elements. The List interface declares mutating methods such as add, remove, set and sort as optional operations, which an implementation may refuse by throwing UnsupportedOperationException. The immutable lists refuse all of them. Collections.sort(ids) simply calls ids.sort(null), which throws, even though the list could in principle be sorted.
This is a feature. An immutable list can be shared freely between threads and between callers without copying, because nobody can change it. Code that receives it does not need to wonder whether someone else will modify it.
Which fix we use depends on ownership. If the code needs a list it will keep changing, we copy into an ArrayList. If it only needs a sorted result, the stream version gives one without any mutable intermediate state.
3Why contains(null) Throws
The immutable lists are null-hostile: List.of("a", null) throws NullPointerException at creation, and List.copyOf throws if the source contains a null. Because no null can ever be inside, the designers chose to treat a query for null as a programming error too, so contains(null) and indexOf(null) throw instead of returning false or -1. The Collection interface allows this: it says that an implementation that prohibits null elements may throw NullPointerException from contains(null).
The practical problem arises when generic code checks membership of a value that may be null, for example a request parameter. With an ArrayList the check returns false; with a List.of it throws.
- Can the value be null?YesHandle null firstNoCall contains(value)
- Handle null firstvalue != null &&
list.contains(value) - Must the list itself hold nulls?YesUse ArrayList, not List.ofNoKeep List.of
3 steps in order. Play them, or step through them one at a time.
The fix is to handle null before the query, which usually also makes the intent clearer. If a collection really must hold nulls, we do not use List.of for it; an ArrayList, or Collections.unmodifiableList over one, allows them.
4Immutable Copies Versus Unmodifiable Views
Java has several kinds of read-only list, and they behave differently. Collections.unmodifiableList(list) returns a view: callers cannot change it through the view, but the owner of the underlying list can, and the view shows those changes. It also allows nulls, if the underlying list has them. List.of and List.copyOf are true immutable lists: nobody can change them, ever, and they reject nulls.
| How it is made | Can anyone change it? | Nulls | Notes |
|---|---|---|---|
List.of(...) | no | rejected, contains(null) throws | truly immutable |
List.copyOf(c) | no | rejected | returns c itself if already immutable |
Collections.unmodifiableList(l) | the owner of l | allowed | read-only view |
stream.toList() | no | allowed | Java 16+ |
Collectors.toUnmodifiableList() | no | rejected | Java 10+ |
Collectors.toList() | not specified | allowed | ArrayList in practice |
Arrays.asList(...) | set only, no add/remove | allowed | fixed-size view of the array |
Streams add two more variants. Stream.toList(), since Java 16, returns an unmodifiable list that, unlike List.of, does allow null elements. Collectors.toList() returns a list with no guarantee of mutability; in current JDKs it is an ArrayList, and code often relies on that, but the documentation does not promise it. If we need a mutable result, Collectors.toCollection(ArrayList::new) makes it explicit; if we need an immutable one, toList() or Collectors.toUnmodifiableList(), which rejects nulls.
5Follow-up questions
Why does Arrays.asList allow sort but not add?
Arrays.asList returns a fixed-size list backed by the array: set changes an array slot, and sort only rearranges slots, so both work and write through to the array. add and remove would change the size, which an array cannot do, so they throw UnsupportedOperationException.
Is List.copyOf always a copy?
No. If the argument is already an immutable list created by List.of or List.copyOf, it returns the same instance, because there is nothing that could change. Otherwise it makes a new immutable list. That makes it cheap to call defensively at API boundaries.
How do we tell at runtime whether a list is modifiable?
There is no public method for it. The only way is to try a modifying call and catch UnsupportedOperationException, which is a poor design. Instead, the API contract should say whether returned lists can be modified, and code that needs a mutable list should make its own copy.
6Conclusion
In this answer, we saw that List.of returns an unmodifiable list, so sort, add and remove throw UnsupportedOperationException, and we fixed it by copying into an ArrayList or producing a sorted copy with stream().sorted().toList(). List.of also rejects nulls entirely, so contains(null) throws NullPointerException, and we handle null before querying. Finally we compared immutable lists such as List.of and List.copyOf with the read-only view from Collections.unmodifiableList, and with Stream.toList() and Collectors.toList().
Open with
Both exceptions are by design. List.of returns an unmodifiable list. Mutating methods on List, like add, remove, set and sort, are optional operations, and the immutable implementation refuses all of them with UnsupportedOperationException.
Then, if they want more
Collections.sort just calls list.sort, so it throws. The fix depends on what I need. If I want a list I can keep changing, I copy it: new ArrayList of List.of(3, 1, 2), then sort. If I only need a sorted result, stream().sorted().toList() gives a new immutable list.
The second exception comes from the fact that the immutable lists reject nulls completely. List.of with a null throws at creation, and since a null can never be inside, contains(null) and indexOf(null) are treated as errors and throw NullPointerException. The Collection contract allows that for null-prohibiting collections.
So with a value that might be null, like a request parameter, I check for null first: value != null && list.contains(value). If the data genuinely needs nulls, I don't use List.of. I would also point out the difference with Collections.unmodifiableList.
That is only a read-only view: callers can't change it, but the owner of the underlying list can, and the view reflects it, and it allows nulls. List.of and List.copyOf are truly immutable snapshots. Stream.toList is also unmodifiable but allows nulls, while Collectors.toList makes no promise about mutability, even though it is an ArrayList today.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- 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 Collections Framework, overview, java.util, package 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 "This code throws UnsupportedOperationException:": https://scrivio-interview-library.netlify.app/java/lists/c0ee38c028/