The List.of, Set.of, and Map.of factory methods were introduced in Java 9 to create immutable collections in a simpler and more concise way. These methods directly create unmodifiable instances of List, Set, or Map.
Usage of List.of
The List.of method is used to create an unmodifiable List containing the provided elements. The key characteristics are:
- The list is immutable, meaning you cannot modify its contents (e.g., add, remove, replace elements).
- If you attempt to modify the list, a
java.lang.UnsupportedOperationExceptionwill be thrown.
Key Points:
- It is null-safety aware, meaning
nullis not allowed as an element. - The created list maintains the insertion order.
Example
List<String> names = List.of("Rosa", "John", "Mary", "Alice");
System.out.println(names); // Output: [Rosa, John, Mary, Alice]
names.add("Bob"); // Throws java.lang.UnsupportedOperationException
Note: Attempting to pass a
nullelement will result inNullPointerException.
Usage of Set.of
The Set.of method creates an immutable Set containing the given elements. The key characteristics are:
- The set is unmodifiable, so you cannot add or remove elements after creation.
- It does not allow duplicate elements.
- It does not allow
nullelements.
Key Points:
- A
java.lang.IllegalArgumentExceptionis thrown if duplicate elements are provided during creation. - Since a
Setdoes not guarantee order, the order of elements in the resulting set is not predictable.
Example
Set<String> items = Set.of("apple", "banana", "orange");
System.out.println(items); // Output: [apple, banana, orange] (order may vary)
items.add("kiwi"); // Throws java.lang.UnsupportedOperationException
Note: If duplicate elements are passed, an exception will be thrown:
Set.of("apple", "banana", "apple"); // Throws IllegalArgumentException
Usage of Map.of
The Map.of method creates an immutable Map with key-value pairs. The characteristics are:
- The created map is unmodifiable.
- Both
nullkeys andnullvalues are not allowed. - Duplicate keys are not allowed, and attempting to use duplicate keys will throw
IllegalArgumentException.
Example
Map<String, Integer> map = Map.of("John", 25, "Mary", 30, "Alice", 27, "Rosa", 22);
System.out.println(map);
// Output (order may vary): {John=25, Mary=30, Alice=27, Rosa=22}
map.put("Bob", 31); // Throws java.lang.UnsupportedOperationException
Note: Avoid duplicate keys. For example:
Map.of("Key1", 1, "Key2", 2, "Key1", 3); // Throws IllegalArgumentException
Advantages of List.of, Set.of, and Map.of
- Immutable collections help ensure thread safety without additional synchronization.
- Improved code conciseness compared to using
Collections.unmodifiableList,Collections.unmodifiableSet, orCollections.unmodifiableMap. - Simplified initialization with a clean and readable API.
For larger maps (more than 10 entries), use Map.ofEntries for better clarity:
Map<String, Integer> largeMap = Map.ofEntries(
Map.entry("Alice", 27),
Map.entry("John", 25),
Map.entry("Mary", 30),
Map.entry("Rosa", 22)
);
