Clean • Professional
Java 9 introduced several improvements in the Collection API, including factory methods like List.of(), Set.of(), and Map.of(). These methods help developers create collections in a clean, concise, and modern way.
They are mainly used when you need fixed (immutable) collections without writing extra lines of code.
List.of(), Set.of(), Map.of()?List.of(), Set.of(), and Map.of() are static factory methods used to create immutable collections in a single line.
Instead of creating a collection and adding elements one by one, these methods let you initialize collections directly with values.
In simple words: You can create collections quickly and easily, without using
newand multipleadd()calls.
Before Java 9, creating collections required multiple lines of code, which made it long and less readable. The new factory methods were introduced to simplify this process and make code more concise.
Before Java 9 (Old Way)
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
Java 9 (Modern Way)
List<String> list = List.of("A", "B", "C");

Java 9 provides three main factory methods:
List.of() – Create Immutable ListList.of() is used to create an immutable list in Java. It allows you to initialize a list with values in a single line, making the code shorter and cleaner.
Example
List<String> list = List.of("Java", "Python", "C++");
System.out.println(list);
Key Points
Modification Attempt
Since the list created using List.of() is immutable, any attempt to modify it will result in an exception.
list.add("C++"); // UnsupportedOperationException

Set.of() – Create Immutable SetSet.of() is used to create an immutable set in Java. It allows you to define a set with elements in a single line, making the code simple and concise.
Example
Set<String> set = Set.of("Java", "Python", "C++");
System.out.println(set);
Key Points
Duplicate Example
Set.of("A", "A"); // IllegalArgumentException
Map.of() – Create Immutable MapMap.of() is used to create an immutable map in Java. It allows you to define key-value pairs in a single step, making the code short and readable.
Map<Integer, String> map = Map.of(
1, "Java",
2, "Python",
3, "C++"
);
System.out.println(map);
Key Points:
Duplicate Key Example:
Map.of(1, "A", 1, "B"); // IllegalArgumentException
👉 Duplicate keys are not allowed, so Java throws an exception.

Map.ofEntries())When you need to create a map with more than 10 entries, Map.of() is not suitable. In such cases, Java provides Map.ofEntries().
Map<String, Integer> map = Map.ofEntries(
Map.entry("A", 1),
Map.entry("B", 2)
);
👉 It allows you to create large immutable maps in a clean and readable way.

All these methods (List.of(), Set.of(), Map.of()) create immutable collections, meaning their data cannot be changed after creation.
That means:
👉 Any modification attempt will throw an exception at runtime.
These factory methods do not allow null values. If you try to add null, Java will throw an exception.
List.of("A", null); // NullPointerException
null values are not allowed in List.of(), Set.of(), or Map.of()
👉 This restriction helps avoid unexpected errors and makes collections more reliable.
| Feature | Before Java 9 | Java 9 (Factory Methods) |
|---|---|---|
| Code Length | Long (multiple lines required) | Short (single-line initialization) |
| Readability | Medium (more boilerplate code) | High (clean and concise) |
| Mutability | Mutable (can modify later) | Immutable (cannot modify) |
| Initialization | Multiple steps (new + add()) | Direct initialization in one step |
Factory methods like List.of(), Set.of(), and Map.of() make collection creation simple and efficient, especially for fixed data.
add() calls.While these methods are useful, they also have some restrictions.
null is used.Use these factory methods when you need simple, fixed, and read-only collections.
Avoid these methods when your data needs to change over time.
These factory methods are commonly used in situations where data is fixed and does not change.
Example (Real Use Case)
var countries = List.of("India", "USA", "UK");
var codes = Map.of(
"IN", "India",
"US", "USA"
);
countries.forEach(System.out::println);
The introduction of List.of(), Set.of(), and Map.of() has made Java code shorter, cleaner, and more secure by promoting immutability.
👉 These APIs are essential for modern Java development, especially when working with fixed data and clean coding practices.