Quick revision
The short answer
Lists from List.of refuse nulls completely, so even the queries contains(null) and indexOf(null) throw NullPointerException, while Arrays.asList allows null elements and returns a fixed-size list backed by the array, which also supports set(). Set.of and Map.of mix a random salt, chosen once per JVM run, into the position of each element, so their iteration order changes between runs; the test was relying on an order that was never specified, and should compare without order, use a LinkedHashSet, or sort.
What to remember
In this answer, we saw that List.of differs from Arrays.asList in more than syntax. List.of throws NullPointerException even for contains(null) and indexOf(null), and rejects set(), while Arrays.asList allows nulls and is a fixed-size view over its array. Set.of and Map.of mix a per-JVM random salt into their layout, so their iteration order changes between runs, and the test was relying on an order that was never promised. We fixed the code by handling null before the query, and the test by comparing without order or making the order explicit.
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.
Lists from List.of refuse nulls completely, so even the queries contains(null) and indexOf(null) throw NullPointerException, while Arrays.asList allows null elements and returns a fixed-size list backed by the array, which also supports set(). Set.of and Map.of mix a random salt, chosen once per JVM run, into the position of each element, so their iteration order changes between runs; the test was relying on an order that was never specified, and should compare without order, use a LinkedHashSet, or sort.
Why this question is asked
Replacing Arrays.asList with List.of looks like a safe, mechanical clean-up: both create a small list from a few values, and List.of reads better. Yet the two have different contracts, and a codebase quietly relies on details of the old one. Interviewers like this question because it combines two separate surprises, and explaining each requires knowing what the collections promise rather than what they happened to do. In this answer, we will recap the two methods, reproduce the NullPointerException, explain why List.of behaves this way, then turn to the flaky test, explain the randomised order, and see how to fix both and do the migration safely.
1A Quick Recap of the Two Methods
Arrays.asList(a, b, c) is from Java 1.2. It wraps an array in a List view. The list has the array's size, which cannot change, so add and remove throw UnsupportedOperationException, but set works and writes through to the array. It accepts any element, including null.
List.of(a, b, c) is from Java 9. It creates an unmodifiable list, a separate copy that nothing can change. It is part of a family with strict rules: no null elements anywhere, and for Set.of and Map.of, no duplicates and an unspecified iteration order.
| Behaviour | Arrays.asList | List.of |
|---|---|---|
| Null elements | Allowed | NullPointerException at creation |
contains(null), indexOf(null) | Returns false or the index | NullPointerException |
set(i, x) | Works, writes to the array | UnsupportedOperationException |
| add, remove | UnsupportedOperationException | UnsupportedOperationException |
| Backed by caller's array | Yes, changes show both ways | No, an independent copy |
2Reproducing the NullPointerException
Here is a typical piece of code that worked before the change. It checks whether a list of allowed values contains the value from a request, and the value can be null when a field is missing.
Notice that nobody tried to put a null into the list. The list itself is fine. The failure comes from asking whether it contains null. The stack trace points into the JDK's immutable collection classes, which is the clue.
3Why List.of Rejects contains(null)
The Collection interface allows an implementation that does not support null elements to throw NullPointerException from contains(null), as an optional restriction. The List.of family takes that option. Its view is that null has no place in these collections at all, and that a null query usually means a bug upstream: a value that should have been validated earlier. Throwing at the query exposes that bug instead of quietly answering false.
Whether that is helpful depends on the code. In our example, a missing currency is a normal input case, so the fix belongs in the method: handle null explicitly before asking the list. That is clearer than relying on the collection to answer false.
Notice also that ALLOWED could be a Set.of, which expresses the intent better for membership checks; it has the same null rule. The other difference worth searching for is set(). Code that called set on an Arrays.asList list, for example to replace an element in place before sorting, now throws UnsupportedOperationException. And code that relied on the list writing through to the original array, or the array writing through to the list, no longer sees those changes.
4The Flaky Test: Set.of Order Changes Between Runs
The second failure looks unrelated: a test that sometimes passes and sometimes fails, with no code change in between.
Set.of and Map.of make no promise about iteration order, and the JDK enforces that in a striking way. When the JVM starts, it picks a random salt value, and the immutable sets and maps mix that salt into the position of each element in their internal table, and into the direction they iterate. Within one JVM run the order is stable; across runs it changes. So a test run by a developer might pass five times in a row and then fail in CI.
The JDK does this on purpose. If the order were stable, programs and tests would come to depend on it, and any future change to the implementation would break them. Randomising per run makes accidental dependencies fail quickly, while the code is still fresh. The test was never correct; the old collection simply happened to produce a stable order, often a HashSet whose order depends on hash codes and capacity.
5Fixing the Test and the Migration
The fix depends on what the test means. If order does not matter, compare as sets, so the test checks membership only. If the code under test should produce a defined order, then the production code must use an ordered collection, such as a LinkedHashSet for insertion order or a sorted stream, and the test then checks that order.
For the migration itself, the safe approach is to treat it as a behaviour change, not a text replacement. Before replacing Arrays.asList, check whether any caller may put or query nulls, whether anything calls set or relies on the array view, and whether iteration order of sets and maps is asserted or serialized anywhere. Running the test suite several times, or with a few different JVM runs in CI, quickly shows the order dependencies, which is exactly what the random salt is for.
6Follow-up questions
Does Collections.unmodifiableList also throw on contains(null)?
No. It is a view over the original list and delegates queries to it, so an unmodifiable view of an ArrayList answers contains(null) normally. The strict null rule belongs to the List.of family, including List.copyOf.
Is Stream.toList() strict about nulls too?
No. Stream.toList returns an unmodifiable list, but it allows null elements, unlike List.of. It is unmodifiable in the same way, so set and add throw.
Why does the random order appear only for Set.of and Map.of, not List.of?
A list is ordered by definition: element 0 comes first. Sets and maps have no defined order, so the JDK is free to choose one, and it chooses to make it visibly unreliable.
7Conclusion
In this answer, we saw that List.of differs from Arrays.asList in more than syntax. List.of throws NullPointerException even for contains(null) and indexOf(null), and rejects set(), while Arrays.asList allows nulls and is a fixed-size view over its array. Set.of and Map.of mix a per-JVM random salt into their layout, so their iteration order changes between runs, and the test was relying on an order that was never promised. We fixed the code by handling null before the query, and the test by comparing without order or making the order explicit.
Open with
These are two separate contract differences. For the NullPointerException: Arrays.asList returns a fixed-size list backed by the array, and it allows nulls, so contains of null just returns false.
Then, if they want more
List.of belongs to the Java 9 immutable family, which rejects nulls completely, not only when creating the list but also in queries, so contains of null and indexOf of null throw NullPointerException. The Collection interface allows that for implementations that do not support nulls.
The real fix is to handle the null input explicitly before the lookup, for example currency not equal to null and the set contains it. While we are at it, I would check for calls to set, because Arrays.asList supports set and writes through to the array, while List.of throws.
For the test: Set.of and Map.of have unspecified iteration order, and the JDK deliberately mixes a random salt, chosen when the JVM starts, into the element positions. So order is stable within one run but changes between runs, which is why the test is flaky. The test depended on an order that was never promised.
If order does not matter, I compare as sets; if it matters, the production code should use a LinkedHashSet or sort, and the test checks that. For future clean-ups like this, I treat them as behaviour changes and run the suite several times.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- 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 "After replacing Arrays.asList with List.of in a codebase, some calls start throwing...": https://scrivio-interview-library.netlify.app/java/collections-overview/eb4de4779c/