Fix nextKey skipping a key and throwing for keys not in the map - #728
Open
rootvector2 wants to merge 1 commit into
Open
Fix nextKey skipping a key and throwing for keys not in the map#728rootvector2 wants to merge 1 commit into
rootvector2 wants to merge 1 commit into
Conversation
garydgregory
requested changes
Aug 25, 2026
garydgregory
left a comment
Member
There was a problem hiding this comment.
Hello @rootvector2
Thank you for the PR. How about adding tests for:
- An explicit test for
DualTreeBidiMap.nextKey()with an absent key that is between existing keys. The abstract test usesgetOtherKeys()which are outside the sample range, so the bug would still be hidden if the key were in-range. - An explicit test for
UnmodifiableSortedMap.nextKey(). It inherits the decorator fix but has no dedicated test.
Should the code be using NavigableMap.higherKey() when available? TreeMap implements NavigableMap, so higherKey(key) would give the correct successor in one call and would be null for absent keys automatically.
Should we guard the OrderedMap branch in DualTreeBidiMap.nextKey() for consistency; either ensure the delegated implementation also checks presence or apply the same containsKey guard before delegation.
Isn't there a similar problem with previousKey()? That could be addressed in a different PR.
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.
AbstractSortedMapDecorator.nextKeywalkstailMap(key)and discards the first element to step overkeyitself, but never checks thatkeyis present. Whenkeyis absent that first element is the successor, so it gets dropped and the wrong key comes back; whenkeyis at or past the endtailMapis empty andit.next()throwsNoSuchElementExceptioninstead of returningnull.DualTreeBidiMap.nextKeyrepeats the sametailMap().iterator().next()shape and fails the same two ways, since itsisEmpty()guard only covers a fully empty map.Return
nullwhen the map does not containkey, matching the documentednull if no matchcontract and every othernextKeyin the library (AbstractLinkedMap,ListOrderedMap,TreeBidiMap,PatriciaTrieall returnnullfor an absent key); the skip-first logic then runs only for a present key, the case it was written for.FixedSizeSortedMapandUnmodifiableSortedMapinherit the decorator fix. Found by auditing theOrderedMap.nextKeyimplementations against the contract.mvn; that'smvnon the command line by itself.