Skip to content

Commit 89ed6e0

Browse files
Copilotbrunoborges
andcommitted
Suggest Stream.toList() in Unmodifiable collectors pattern
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
1 parent ebe887f commit 89ed6e0

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

content/collections/unmodifiable-collectors.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"oldCode": "List<String> list = stream.collect(\n Collectors.collectingAndThen(\n Collectors.toList(),\n Collections::unmodifiableList\n )\n);",
1313
"modernCode": "List<String> list = stream.collect(\n Collectors.toUnmodifiableList()\n);",
1414
"summary": "Collect directly to unmodifiable collections.",
15-
"explanation": "Java 10 added toUnmodifiableList(), toUnmodifiableSet(), and toUnmodifiableMap() collectors. No more wrapping with collectingAndThen.",
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.",
1616
"whyModernWins": [
1717
{
1818
"icon": "📏",
@@ -39,7 +39,7 @@
3939
"related": [
4040
"collections/immutable-map-creation",
4141
"collections/immutable-set-creation",
42-
"collections/map-entry-factory"
42+
"streams/stream-tolist"
4343
],
4444
"docs": [
4545
{

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)