Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions content/collections/unmodifiable-collectors.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,48 @@
"title": "Unmodifiable collectors",
"category": "collections",
"difficulty": "intermediate",
"jdkVersion": "10",
"jdkVersion": "16",
"oldLabel": "Java 8",
"modernLabel": "Java 10+",
"modernLabel": "Java 16+",
"oldApproach": "collectingAndThen",
"modernApproach": "toUnmodifiable*()",
"modernApproach": "stream.toList()",
"oldCode": "List<String> list = stream.collect(\n Collectors.collectingAndThen(\n Collectors.toList(),\n Collections::unmodifiableList\n )\n);",
"modernCode": "List<String> list = stream.collect(\n Collectors.toUnmodifiableList()\n);",
"summary": "Collect directly to unmodifiable collections.",
"explanation": "Java 10 added toUnmodifiableList(), toUnmodifiableSet(), and toUnmodifiableMap() collectors. No more wrapping with collectingAndThen.",
"modernCode": "List<String> list = stream.toList();",
"summary": "Collect directly to an unmodifiable list with stream.toList().",
"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.",
"whyModernWins": [
{
"icon": "📏",
"title": "Direct",
"desc": "One collector instead of wrapping with collectingAndThen."
"title": "Shortest yet",
"desc": "stream.toList() needs no collect() or Collectors import at all."
},
{
"icon": "🔒",
"title": "Immutable",
"desc": "Result cannot be modified — no accidental mutations."
},
{
"icon": "🚫",
"title": "Null-safe",
"desc": "Rejects null elements during collection."
"icon": "📖",
"title": "Readable",
"desc": "Reads naturally as the terminal step of any stream pipeline."
}
],
"support": {
"state": "available",
"description": "Widely available since JDK 10 (March 2018)"
"description": "Widely available since JDK 16 (March 2021)"
},
"prev": "collections/reverse-list-iteration",
"next": "strings/string-isblank",
"related": [
"collections/immutable-map-creation",
"collections/immutable-set-creation",
"collections/map-entry-factory"
"streams/stream-tolist"
],
"docs": [
{
"title": "Stream.toList()",
"href": "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/stream/Stream.html#toList()"
},
{
"title": "Collectors.toUnmodifiableList()",
"href": "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/stream/Collectors.html#toUnmodifiableList()"
Expand Down
2 changes: 1 addition & 1 deletion content/streams/stream-tolist.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"prev": "streams/collectors-flatmapping",
"next": "streams/stream-mapmulti",
"related": [
"streams/virtual-thread-executor",
"collections/unmodifiable-collectors",
"streams/stream-gatherers",
"streams/stream-iterate-predicate"
],
Expand Down