Skip to content

Commit 73bec43

Browse files
Copilotbrunoborges
andcommitted
Highlight stream.toList() as the modern approach in Unmodifiable collectors
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
1 parent 89ed6e0 commit 73bec43

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

content/collections/unmodifiable-collectors.json

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,35 @@
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. For lists specifically, Java 16+ stream.toList() is even simpler — it returns an unmodifiable List without the collect() call.",
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",
@@ -42,6 +42,10 @@
4242
"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()"

0 commit comments

Comments
 (0)