Repository navigation
Modern collections: immutable factories, unmodifiable views and sequenced collections - #108
Merged
Merged
Conversation
…uenced collections
|
@mperor ⏰ This daily topic is still waiting for your review. |
1 similar comment
|
@mperor ⏰ This daily topic is still waiting for your review. |
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.
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 sourceCollections.unmodifiableList()is a live view: it blocks direct mutation through itself, but still reflects changes made to the backing listStream.toList()guarantees an unmodifiable result, whileCollectors.toList()makes no mutability guarantee (it happens to return a mutableArrayListtoday)SequencedCollection/SequencedMapaddgetFirst()/getLast()/reversed()uniformly acrossList,LinkedHashSetandLinkedHashMapWhy it matters
Picking the wrong "immutable-looking" collection factory is a common source of bugs — an
UnsupportedOperationExceptionat 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
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
./gradlewhere. 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