Quick revision
The short answer
List.of, Set.of and Map.of return unmodifiable collections, so every mutator throws UnsupportedOperationException. They reject null elements, keys and values with NullPointerException, which is why List.of(1, null) throws, and Set.of and Map.of reject duplicate elements or keys with IllegalArgumentException, because a set or map literal with a duplicate is almost certainly a mistake. Their iteration order is unspecified and changes between runs, and Map.of takes up to ten pairs, while Map.ofEntries takes any number.
What to remember
In this answer, we saw that List.of, Set.of and Map.of create unmodifiable collections whose mutators throw UnsupportedOperationException. They reject nulls with NullPointerException, even in contains, to fail early at the real cause, and Set.of and Map.of reject duplicates with IllegalArgumentException because a duplicate in a literal is almost always a mistake. Their set and map iteration order is unspecified and deliberately changes between runs, and Map.of stops at ten pairs, after which we use Map.ofEntries.
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, Set.of and Map.of return unmodifiable collections, so every mutator throws UnsupportedOperationException. They reject null elements, keys and values with NullPointerException, which is why List.of(1, null) throws, and Set.of and Map.of reject duplicate elements or keys with IllegalArgumentException, because a set or map literal with a duplicate is almost certainly a mistake. Their iteration order is unspecified and changes between runs, and Map.of takes up to ten pairs, while Map.ofEntries takes any number.
Why this question is asked
Since Java 9, these factory methods have become the usual way to write small fixed collections, in production code and especially in tests. They look like a shorthand for new ArrayList plus a few adds, but they behave quite differently, and those differences are deliberate design choices. Interviewers ask about the surprising cases because a candidate who can explain why they throw understands what the factories are for. In this answer, we will recap what the factories create, then look at unmodifiability, nulls, duplicates, iteration order and the size limit of Map.of, with each line's result in the code.
1A Quick Recap of the Factories
Before Java 9, a small constant list needed Arrays.asList or a sequence of add calls wrapped in Collections.unmodifiableList, and a constant map needed a static block. Java 9's JEP 269 added static factory methods on the interfaces themselves: List.of(...), Set.of(...), Map.of(k1, v1, k2, v2, ...) and Map.ofEntries(...). Java 10 added List.copyOf, Set.copyOf and Map.copyOf, which create the same kind of collection from an existing one.
The results are compact, special-purpose classes inside the JDK. A List.of with one or two elements uses a tiny class that holds them in fields, and larger ones use an array of exactly the right size, with no spare capacity. They are meant for values that are known and fixed: constants, test data, return values that callers should not change.
2Unmodifiable: Every Mutator Throws
The collections cannot be changed after creation. Every method that would change them, such as add, remove, set, clear, sort or put, throws UnsupportedOperationException. This is true even when the call would not actually change anything.
They are unmodifiable, but not necessarily deeply immutable. If the elements themselves are mutable objects, such as a List.of(new StringBuilder()), the builder inside can still be changed. The collection only guarantees that its own structure and references do not change. With immutable elements such as strings and records of immutable values, the whole thing is immutable and can be shared freely between threads.
3Why Nulls Throw NullPointerException
The factories refuse null anywhere: as a list or set element, as a map key, or as a map value. List.of(1, null) throws NullPointerException at creation, and so does Map.of("k", null). The query methods follow the same rule, so contains(null) and indexOf(null) on these lists also throw.
The reason is the design's view of null. A null inside a collection is usually a bug, a value that was never set, and it tends to cause a NullPointerException much later, when someone reads it, far away from where it was put in. Failing at creation points straight at the real cause. Refusing nulls also lets the implementation use null internally as a marker, for example for empty slots in the hash table of Set.of, which keeps it simple and compact.
4Why Duplicates Throw IllegalArgumentException
Lists are allowed to contain duplicates, so List.of("a", "a") is fine. Sets and maps are not, and their factories go one step further than the normal collections: instead of silently keeping one element, they throw.
The reasoning is that Set.of is a literal written by a programmer. If we write the same element twice in a literal, we probably meant to write a different element, or copied a line and forgot to change it. With a map this is even more dangerous: Map.of("timeout", 30, "timeout", 60) would silently lose one value, and the program would run with a setting nobody intended. Throwing at once exposes the mistake. Notice that Set.copyOf behaves differently: it builds a set from a collection that may legitimately contain duplicates, so it keeps one of each.
5Unspecified Iteration Order
A List.of keeps its elements in the order given, because a list is ordered by definition. Set.of and Map.of make no promise at all about order. In practice, the JDK chooses a random value, a salt, each time the JVM starts, and mixes it into where elements are placed, so the iteration order differs from one run of the program to the next.
This is deliberate. If the order were stable, code and tests would start to depend on it without anyone noticing, and the JDK could never change its implementation. By making the order visibly random, the JDK makes such dependencies fail immediately, in development, rather than years later. If order matters, we use a LinkedHashSet or LinkedHashMap, or sort the result.
| Behaviour | List.of / Set.of / Map.of | ArrayList / HashSet / HashMap |
|---|---|---|
| Modify after creation | UnsupportedOperationException | Allowed |
| Null element, key or value | NullPointerException | Allowed (one null key in HashMap) |
contains(null) | NullPointerException | false or true |
Duplicate in Set.of or Map.of | IllegalArgumentException | Silently keeps one |
| Iteration order of sets and maps | Unspecified, changes per run | Unspecified, usually stable |
6Map.of Takes Up to Ten Pairs
Map.of is overloaded with fixed numbers of arguments: from zero up to ten key-value pairs, which means up to twenty parameters. There is no varargs version, because keys and values alternate and have different types, which varargs cannot express safely. For more pairs, Map.ofEntries takes any number of Map.Entry objects, created conveniently with Map.entry.
Map.entry rejects nulls in the same way, and Map.ofEntries applies the same duplicate-key and null rules as Map.of. List.of and Set.of do have a varargs version, along with fixed-argument overloads up to ten elements, which avoid creating an array for the common small cases.
7Follow-up questions
Are these collections serializable?
Yes. They are serializable through a special proxy form as long as their elements are, so they can be sent through Java serialization and come back as the same kind of unmodifiable collection.
How do I get a modifiable list with initial values?
Wrap the factory in a copy: new ArrayList<>(List.of("a", "b")). The factory gives us a concise way to write the values, and the ArrayList gives us mutability.
Is List.of thread-safe?
Yes, for reading, because it can never change. Any number of threads can read it without locks, provided the elements themselves are immutable or not modified.
8Conclusion
In this answer, we saw that List.of, Set.of and Map.of create unmodifiable collections whose mutators throw UnsupportedOperationException. They reject nulls with NullPointerException, even in contains, to fail early at the real cause, and Set.of and Map.of reject duplicates with IllegalArgumentException because a duplicate in a literal is almost always a mistake. Their set and map iteration order is unspecified and deliberately changes between runs, and Map.of stops at ten pairs, after which we use Map.ofEntries.
Open with
List.of, Set.of and Map.of, from Java 9, create compact unmodifiable collections, so any mutator, like add, set, put or even remove of an absent element, throws UnsupportedOperationException. They are strict in three ways.
Then, if they want more
First, nulls: no null elements, keys or values, so List.of of 1 and null throws NullPointerException at creation, and even contains of null throws. The idea is to fail where the bad value goes in, not later when someone reads it, and it also lets the implementation use null internally.
Second, duplicates: a list can have duplicates, but Set.of of a and a throws IllegalArgumentException, and Map.of with the same key twice throws too. Since these are literals written by a programmer, a duplicate is almost certainly a typo, and for a map it would silently lose a value. Set.copyOf, by contrast, deduplicates quietly because its input may legitimately repeat.
Third, order: List.of keeps the given order, but Set.of and Map.of have an unspecified order that the JDK randomises per JVM run, so nobody can come to depend on it. If I need order, I use LinkedHashMap or sort. And Map.of has overloads only up to ten pairs, because keys and values alternate; beyond that I use Map.ofEntries with Map.entry.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9, Java 10
- 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 "What are the properties of List.of, Set.of and Map.of? Why do Set.of("a", "a") and...": https://scrivio-interview-library.netlify.app/java/collections-overview/77f94ed631/