Skip to content

Commit 3ed8a04

Browse files
authored
Merge pull request #80 from javaevolved/copilot/update-unmodifiable-collectors
Highlight Stream.toList() as the modern approach in Unmodifiable collectors
2 parents 772adf9 + 73bec43 commit 3ed8a04

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

content/collections/unmodifiable-collectors.json

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,48 @@
44
"title": "Unmodifiable collectors",
55
"category": "collections",
66
"difficulty": "intermediate",
7-
"jdkVersion": "10",
7+
"jdkVersion": "16",
88
"oldLabel": "Java 8",
9-
"modernLabel": "Java 10+",
9+
"modernLabel": "Java 16+",
1010
"oldApproach": "collectingAndThen",
11-
"modernApproach": "toUnmodifiable*()",
11+
"modernApproach": "stream.toList()",
1212
"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.",
1616
"whyModernWins": [
1717
{
1818
"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."
2121
},
2222
{
2323
"icon": "🔒",
2424
"title": "Immutable",
2525
"desc": "Result cannot be modified — no accidental mutations."
2626
},
2727
{
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."
3131
}
3232
],
3333
"support": {
3434
"state": "available",
35-
"description": "Widely available since JDK 10 (March 2018)"
35+
"description": "Widely available since JDK 16 (March 2021)"
3636
},
3737
"prev": "collections/reverse-list-iteration",
3838
"next": "strings/string-isblank",
3939
"related": [
4040
"collections/immutable-map-creation",
4141
"collections/immutable-set-creation",
42-
"collections/map-entry-factory"
42+
"streams/stream-tolist"
4343
],
4444
"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+
},
4549
{
4650
"title": "Collectors.toUnmodifiableList()",
4751
"href": "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/stream/Collectors.html#toUnmodifiableList()"

content/streams/stream-tolist.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"prev": "streams/collectors-flatmapping",
3838
"next": "streams/stream-mapmulti",
3939
"related": [
40-
"streams/virtual-thread-executor",
40+
"collections/unmodifiable-collectors",
4141
"streams/stream-gatherers",
4242
"streams/stream-iterate-predicate"
4343
],

0 commit comments

Comments
 (0)