Fix misleading "Defensive copy" benefit in copying-collections-immutably pattern#145
Merged
brunoborges merged 2 commits intomainfrom Mar 25, 2026
Merged
Conversation
…tions-immutably The old third point claimed 'Changes to the original don't affect the copy' as a benefit of List.copyOf(). However, the old code already shows Collections.unmodifiableList(new ArrayList<>(original)) which ALSO creates a defensive copy — making this a non-differentiator. Replace with 'Any Collection: Accepts any Collection as input—no intermediate ArrayList conversion needed.' This accurately explains why the old code needs new ArrayList<>() (because Collections.unmodifiableList needs a List), while List.copyOf() accepts any Collection directly. Updated English source + all 12 translation files. Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com> Agent-Logs-Url: https://github.com/javaevolved/javaevolved.github.io/sessions/0217fc9d-d60c-42e1-814a-b735f4451104
Copilot
AI
changed the title
[WIP] Fix copying collections immutably to improve memory and performance
Fix misleading "Defensive copy" benefit in copying-collections-immutably pattern
Mar 25, 2026
brunoborges
approved these changes
Mar 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The third
whyModernWinspoint claimedList.copyOf()provides a "defensive copy" advantage over the old approach—but the old code already showsCollections.unmodifiableList(new ArrayList<>(original)), which is also a defensive copy. The claim was only valid against a simpleCollections.unmodifiableList(input)wrapper, not the code actually shown.Changes
content/collections/copying-collections-immutably.yaml— Replace "Defensive copy" with "Any Collection":List.copyOf()acceptsCollection<? extends E>directly, whereasCollections.unmodifiableList()requires aList—which is precisely why the old code needs the intermediatenew ArrayList<>()step.whyModernWinsentry accordingly.The two code blocks remain equivalent; only the explanatory copy is corrected.
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
sh.jbang.dev/usr/bin/curl curl -sL REDACTED(dns block)If you need me to access, download, or install something from one of these locations, you can either:
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.