Call List.of(...), Set.of(...) or Map.of(...) to create an immutable collection in one line. The result cannot be changed: add, put, remove and clear throw UnsupportedOperationException, and null elements are rejected. Use List.copyOf, Set.copyOf and Map.copyOf to take an unmodifiable snapshot of an existing collection.
These immutable collections list.of factory methods arrived in Java 9 and are the standard way to write constants, test data and return values that callers must not modify.
Each interface has static of methods that take the elements directly.
List.of() with no arguments gives an empty immutable list. Overloads take up to ten elements, and a varargs overload accepts any number beyond that. Map.of takes up to ten key and value pairs.
Mutating methods always throw, even when they would not change anything.
The exception is thrown at the call, not at creation, so a mistaken add shows up as a run time failure. Treat it as a design signal: if code needs to change the collection, it should build a new one.
The factory methods are strict by design.
| Rule | Behaviour |
|---|---|
| Null elements, keys or values | NullPointerException at creation |
Set.of with duplicate elements | IllegalArgumentException |
Map.of with duplicate keys | IllegalArgumentException |
contains(null) on the result | NullPointerException for List.of and Set.of |
Iteration order of Set.of and Map.of | Unspecified, and can differ between JVM runs |
Never rely on the printed order of a Set.of or Map.of result. The order is deliberately randomised per JVM instance so that code does not depend on it. If you need a stable order, use List.of, or wrap a LinkedHashMap or LinkedHashSet with Collections.unmodifiableMap or Collections.unmodifiableSet.
Map.of stops at ten pairs. For more, or for readability, use Map.entry with Map.ofEntries.
List.copyOf(source) makes an independent immutable copy. Collections.unmodifiableList(source) only wraps the original in a read only view, so later changes to the source show through the view.
copyOf also avoids copying when the argument is already an immutable collection from the of family, so it is cheap to call defensively. Streams offer the same result: Stream.toList() (Java 16+) returns an unmodifiable list, though unlike List.of it allows null elements.
Immutable collections are best used for:
private static final List<String> ROLES = List.of("admin", "user");List.copyOf(items) from a getter so callers cannot change internal state.An important caveat: the collection is immutable, but the elements may not be. A List.of(new StringBuilder()) still lets you change the builder. For true immutability, store immutable elements such as strings, records of immutable fields, or java.time values.
No. Arrays.asList returns a fixed size list that still allows set and writes through to the array. List.of is fully immutable and rejects nulls.
Java 9 added them. copyOf methods came in Java 10, and Stream.toList() in Java 16.
Not with List.of, Set.of or Map.of; they throw NullPointerException. Use Collections.unmodifiableList over a normal list if you really need nulls.
Yes for sharing. Because their contents cannot change after creation, any number of threads can read them without locking, provided the elements are themselves safe to share.
Use List.of, Set.of and Map.of (or Map.ofEntries) to build immutable collections, and copyOf to snapshot existing ones. They reject nulls and duplicates, throw UnsupportedOperationException on mutation, and give no guarantee about Set or Map order. Remember that only the collection is frozen, not the objects inside it.