|
4 | 4 | "title": "Unmodifiable collectors", |
5 | 5 | "category": "collections", |
6 | 6 | "difficulty": "intermediate", |
7 | | - "jdkVersion": "10", |
| 7 | + "jdkVersion": "16", |
8 | 8 | "oldLabel": "Java 8", |
9 | | - "modernLabel": "Java 10+", |
| 9 | + "modernLabel": "Java 16+", |
10 | 10 | "oldApproach": "collectingAndThen", |
11 | | - "modernApproach": "toUnmodifiable*()", |
| 11 | + "modernApproach": "stream.toList()", |
12 | 12 | "oldCode": "List<String> list = stream.collect(\n Collectors.collectingAndThen(\n Collectors.toList(),\n Collections::unmodifiableList\n )\n);", |
13 | | - "modernCode": "List<String> list = stream.collect(\n Collectors.toUnmodifiableList()\n);", |
14 | | - "summary": "Collect directly to unmodifiable collections.", |
15 | | - "explanation": "Java 10 added toUnmodifiableList(), toUnmodifiableSet(), and toUnmodifiableMap() collectors. No more wrapping with collectingAndThen.", |
| 13 | + "modernCode": "List<String> list = stream.toList();", |
| 14 | + "summary": "Collect directly to an unmodifiable list with stream.toList().", |
| 15 | + "explanation": "Java 10 added toUnmodifiableList(), toUnmodifiableSet(), and toUnmodifiableMap() to replace the verbose collectingAndThen wrapper. For lists specifically, Java 16's stream.toList() provides an even simpler alternative — no collect() call at all. Use toUnmodifiableSet() and toUnmodifiableMap() for other collection types.", |
16 | 16 | "whyModernWins": [ |
17 | 17 | { |
18 | 18 | "icon": "📏", |
19 | | - "title": "Direct", |
20 | | - "desc": "One collector instead of wrapping with collectingAndThen." |
| 19 | + "title": "Shortest yet", |
| 20 | + "desc": "stream.toList() needs no collect() or Collectors import at all." |
21 | 21 | }, |
22 | 22 | { |
23 | 23 | "icon": "🔒", |
24 | 24 | "title": "Immutable", |
25 | 25 | "desc": "Result cannot be modified — no accidental mutations." |
26 | 26 | }, |
27 | 27 | { |
28 | | - "icon": "🚫", |
29 | | - "title": "Null-safe", |
30 | | - "desc": "Rejects null elements during collection." |
| 28 | + "icon": "📖", |
| 29 | + "title": "Readable", |
| 30 | + "desc": "Reads naturally as the terminal step of any stream pipeline." |
31 | 31 | } |
32 | 32 | ], |
33 | 33 | "support": { |
34 | 34 | "state": "available", |
35 | | - "description": "Widely available since JDK 10 (March 2018)" |
| 35 | + "description": "Widely available since JDK 16 (March 2021)" |
36 | 36 | }, |
37 | 37 | "prev": "collections/reverse-list-iteration", |
38 | 38 | "next": "strings/string-isblank", |
39 | 39 | "related": [ |
40 | 40 | "collections/immutable-map-creation", |
41 | 41 | "collections/immutable-set-creation", |
42 | | - "collections/map-entry-factory" |
| 42 | + "streams/stream-tolist" |
43 | 43 | ], |
44 | 44 | "docs": [ |
| 45 | + { |
| 46 | + "title": "Stream.toList()", |
| 47 | + "href": "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/stream/Stream.html#toList()" |
| 48 | + }, |
45 | 49 | { |
46 | 50 | "title": "Collectors.toUnmodifiableList()", |
47 | 51 | "href": "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/stream/Collectors.html#toUnmodifiableList()" |
|
0 commit comments