GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing javaevolved.dev — Immutable list creation | java.evolved
Collections
Beginner
Immutable list creationCreate immutable lists in one clean expression. Code Comparison
✕ Java 8
List<String> list =
Collections.unmodifiableList(
new ArrayList<>(
Arrays.asList("a", "b", "c")
)
);
✓ Java 9+
List<String> list =
List.of("a", "b", "c");
See a problem with this code? Let us know.
Why the modern way wins
📏
One callReplace three nested calls with a single factory method. 🔒
Truly immutableNot just a wrapper — the list itself is immutable. 🛡️
Null-safeRejects null elements at creation time, failing fast. Old Approach
Verbose Wrapping
Modern Approach
List.of()
Since JDK
9
Difficulty
Beginner
JDK Support
Immutable list creation
Available
Widely available since JDK 9 (Sept 2017) How it works
List.of() creates a truly immutable list — no wrapping, no defensive copy. It's null-hostile (rejects null elements) and structurally immutable. The old way required three nested calls. Related Documentation
Proof
Related patterns
Collections
Beginner
Immutable map creationJava 8
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map = Collections.unmodifiableMap(map);
Java 9+
Map<String, Integer> map =
Map.of("a", 1, "b", 2, "c", 3);
JDK 9+
→
Collections
Beginner
Immutable set creationJava 8
Set<String> set =
Collections.unmodifiableSet(
new HashSet<>(
Arrays.asList("a", "b", "c")
)
);
Java 9+
Set<String> set =
Set.of("a", "b", "c");
JDK 9+
→
Collections
Beginner
Sequenced collectionsJava 8
// Get last element Object last = list.get(list.size() - 1); // Get first Object first = list.get(0); // Reverse iteration: manual Java 21+
var last = list.getLast(); var first = list.getFirst(); var reversed = list.reversed();
JDK 21+
→
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |