Make filtered NavigableMap views return snapshot entries from pollFirstEntry(), etc. (and similarly for ForwardingNavigableMap.standard*Entry). - #8699
Merged
Conversation
copybara-service
Bot
force-pushed
the
test_987445650
branch
from
September 25, 2026 20:50
e72aad9 to
fa9327f
Compare
NavigableMap views return snapshot entries from pollFirstEntry(), etc.NavigableMap views return snapshot entries from pollFirstEntry(), etc. (and similarly for ForwardingNavigableMap.standard*Entry).
copybara-service
Bot
force-pushed
the
test_987445650
branch
from
September 26, 2026 23:25
fa9327f to
750c656
Compare
…FirstEntry()`, etc. (and similarly for `ForwardingNavigableMap.standard*Entry`).
And update `NavigableMapTestSuiteBuilder` to require immutable entries from those methods.
For simplicity, I'll focus on the `NavigableMap` case in the description below:
`Maps.filterKeys`, `filterValues` and `filterEntries` on a `NavigableMap` returned the backing map's live entries from their navigation methods. This caused two problems:
### Removing an `Entry` from the backing map might mutate the `Entry`
`pollFirstEntry()` and `pollLastEntry()` removed the entry through the backing `entrySet` iterator and then returned that same entry object.
When `TreeMap` removes a node with two children, it copies the successor's key and value into that node, so the returned entry could show a different mapping.
For example:
- `Maps.filterKeys(TreeMap{1=one, 2=two, 3=three}, k -> k != 1) .pollFirstEntry()` returned `3=three` instead of `2=two`.
- Draining `filterKeys(TreeMap{0..9}, odd)` with `pollFirstEntry()` returned keys [2, 4, 6, 7, 9] instead of [1, 3, 5, 7, 9].
### Navigation methods are supposed to return snapshots
`firstEntry()`, `lastEntry()`, `ceilingEntry()`, `floorEntry()`, `higherEntry()` and `lowerEntry()` returned live entries whose `setValue` wrote through to the backing map. This is bad for two reasons:
- [`NavigableMap`](https://docs.oracle.com/en/java/javase/27/docs/api/java.base/java/util/NavigableMap.html) specifies that these methods return snapshot entries that don't support `setValue`.
- Even if they were to support `setValue`, maps that are filtered by value (or by the full entry contents) should check that the predicate allows the new value.
---
Now, all these methods return snapshots.
But we continue to override `entrySet()` in a way that allows its entries to support `setValue` (with the predicate check).
Finally, I've moved `Iterables.removeFirstMatching` from `Iterables` to `Sets`. That is now the only location that it's used from, since it is no longer used from `Maps`. Along the way, I renamed it to "`pollFirstMatching`" to more closely match the methods that it's used from.
Fixes #8692
I filed b/566253750 for a similar question in `SortedMultiset`.
RELNOTES=n/a
PiperOrigin-RevId: 988966779
copybara-service
Bot
force-pushed
the
test_987445650
branch
from
September 26, 2026 23:44
750c656 to
a279b88
Compare
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.
Make filtered
NavigableMapviews return snapshot entries frompollFirstEntry(), etc. (and similarly forForwardingNavigableMap.standard*Entry).And update
NavigableMapTestSuiteBuilderto require immutable entries from those methods.For simplicity, I'll focus on the
NavigableMapcase in the description below:Maps.filterKeys,filterValuesandfilterEntrieson aNavigableMapreturned the backing map's live entries from their navigation methods. This caused two problems:Removing an
Entryfrom the backing map might mutate theEntrypollFirstEntry()andpollLastEntry()removed the entry through the backingentrySetiterator and then returned that same entry object.When
TreeMapremoves a node with two children, it copies the successor's key and value into that node, so the returned entry could show a different mapping.For example:
Maps.filterKeys(TreeMap{1=one, 2=two, 3=three}, k -> k != 1) .pollFirstEntry()returned3=threeinstead of2=two.filterKeys(TreeMap{0..9}, odd)withpollFirstEntry()returned keys [2, 4, 6, 7, 9] instead of [1, 3, 5, 7, 9].Navigation methods are supposed to return snapshots
firstEntry(),lastEntry(),ceilingEntry(),floorEntry(),higherEntry()andlowerEntry()returned live entries whosesetValuewrote through to the backing map. This is bad for two reasons:NavigableMapspecifies that these methods return snapshot entries that don't supportsetValue.setValue, maps that are filtered by value (or by the full entry contents) should check that the predicate allows the new value.Now, all these methods return snapshots.
But we continue to override
entrySet()in a way that allows its entries to supportsetValue(with the predicate check).Finally, I've moved
Iterables.removeFirstMatchingfromIterablestoSets. That is now the only location that it's used from, since it is no longer used fromMaps. Along the way, I renamed it to "pollFirstMatching" to more closely match the methods that it's used from.Fixes #8692
I filed b/566253750 for a similar question in
SortedMultiset.RELNOTES=n/a