diff --git a/content/collections/unmodifiable-collectors.json b/content/collections/unmodifiable-collectors.json index 0a9b1f5..57f8705 100644 --- a/content/collections/unmodifiable-collectors.json +++ b/content/collections/unmodifiable-collectors.json @@ -4,20 +4,20 @@ "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 list = stream.collect(\n Collectors.collectingAndThen(\n Collectors.toList(),\n Collections::unmodifiableList\n )\n);", - "modernCode": "List 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 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": "🔒", @@ -25,23 +25,27 @@ "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()" diff --git a/content/streams/stream-tolist.json b/content/streams/stream-tolist.json index 43b510c..4c6dbcc 100644 --- a/content/streams/stream-tolist.json +++ b/content/streams/stream-tolist.json @@ -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" ],