Skip to content

Modern collections: immutable factories, unmodifiable views and sequenced collections - #108

Merged
mperor merged 1 commit into
masterfrom
DAILY/95-modern-collections
Nov 4, 2026
Merged

mperor merged 1 commit into
masterfrom
DAILY/95-modern-collections

Conversation

@mperor

@mperor mperor commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

What you'll learn

  • List.of() / List.copyOf() return immutable, null-hostile lists; List.copyOf() takes a snapshot that is unaffected by later mutation of the source
  • Collections.unmodifiableList() is a live view: it blocks direct mutation through itself, but still reflects changes made to the backing list
  • Stream.toList() guarantees an unmodifiable result, while Collectors.toList() makes no mutability guarantee (it happens to return a mutable ArrayList today)
  • SequencedCollection / SequencedMap add getFirst() / getLast() / reversed() uniformly across List, LinkedHashSet and LinkedHashMap

Why it matters

Picking the wrong "immutable-looking" collection factory is a common source of bugs — an UnsupportedOperationException at runtime, or a list that mutates under you because it was only ever a view. Knowing which guarantee each API actually makes avoids both.

Read more

JEP 431: Sequenced Collections

Quiz

var backing = new ArrayList<>(List.of("Ann", "Bob"));
var view = Collections.unmodifiableList(backing);
backing.add("Cid");
System.out.println(view.size());

What does this print?

Answer 3 — `unmodifiableList()` wraps the same backing list rather than copying it, so changes made directly to `backing` are visible through `view`. Only mutation attempted *through* `view` itself (e.g. `view.add(...)`) throws `UnsupportedOperationException`.

Note on local verification: this environment only has JDK 21 installed, and the Gradle build requires JDK 25 via toolchain; JDK 25 auto-provisioning (foojay) and all alternative JDK download hosts tried were blocked by this session's network policy, so I couldn't run ./gradlew here. All APIs used (List.of/copyOf, Stream.toList(), SequencedCollection/SequencedMap) are available since JDK 21, and I compiled and ran the test with the JUnit Console launcher against JDK 21 to confirm all 7 assertions pass. Opening as a draft until CI (JDK 25) confirms.

Closes #95

@mperor mperor added the daily Queued for the daily learning PR label Nov 2, 2026
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Test Results

294 tests  +7   291 ✅ +7   4s ⏱️ -1s
 82 suites +1     3 💤 ±0 
 82 files   +1     0 ❌ ±0 

Results for commit 7fc0041. ± Comparison against base commit ea35b4f.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

@mperor ⏰ This daily topic is still waiting for your review.

1 similar comment
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

@mperor ⏰ This daily topic is still waiting for your review.

@mperor
mperor marked this pull request as ready for review November 4, 2026 15:53
@mperor
mperor merged commit a9c068a into master Nov 4, 2026
2 checks passed
@mperor
mperor deleted the DAILY/95-modern-collections branch November 4, 2026 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

daily Queued for the daily learning PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Modern collections: immutable factories, unmodifiable views and sequenced collections

2 participants