Skip to content

Fix misleading "Defensive copy" benefit in copying-collections-immutably pattern#145

Merged
brunoborges merged 2 commits intomainfrom
copilot/fix-copying-collections-immutably
Mar 25, 2026
Merged

Fix misleading "Defensive copy" benefit in copying-collections-immutably pattern#145
brunoborges merged 2 commits intomainfrom
copilot/fix-copying-collections-immutably

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 25, 2026

The third whyModernWins point claimed List.copyOf() provides a "defensive copy" advantage over the old approach—but the old code already shows Collections.unmodifiableList(new ArrayList<>(original)), which is also a defensive copy. The claim was only valid against a simple Collections.unmodifiableList(input) wrapper, not the code actually shown.

Changes

  • content/collections/copying-collections-immutably.yaml — Replace "Defensive copy" with "Any Collection": List.copyOf() accepts Collection<? extends E> directly, whereas Collections.unmodifiableList() requires a List—which is precisely why the old code needs the intermediate new ArrayList<>() step.
  • 12 translation files (ar, bn, de, es, fr, it, ja, ko, pl, pt-BR, tr, zh-CN) — Updated third whyModernWins entry 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
    • Triggering command: /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.

…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
Copilot AI requested a review from brunoborges March 25, 2026 14:22
@brunoborges brunoborges marked this pull request as ready for review March 25, 2026 15:12
@brunoborges brunoborges merged commit f0470ed into main Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Code Issue] Copying collections immutably

2 participants