Skip to content

Add neighbor navigation to LinkedHashMap and LinkedHashSet - #3393

Open
yashdotdev13 wants to merge 1 commit into
vavr-io:mainfrom
yashdotdev13:issue-3363-neighbor-navigation
Open

yashdotdev13 wants to merge 1 commit into
vavr-io:mainfrom
yashdotdev13:issue-3363-neighbor-navigation

Conversation

@yashdotdev13

Copy link
Copy Markdown

Summary

This PR adds methods to navigate to the next and previous entries in insertion order for LinkedHashMap and LinkedHashSet.

Changes

LinkedHashMap

  • Added nextEntry(K key) to retrieve the next entry after the given key.
  • Added previousEntry(K key) to retrieve the previous entry before the given key.
  • Both methods return Option<Tuple2<K, V>>.

LinkedHashSet

  • Added next(T element) to retrieve the next element.
  • Added previous(T element) to retrieve the previous element.
  • Both methods return Option<T> and delegate to the corresponding LinkedHashMap methods.

Implementation details

  • Navigation follows insertion order.
  • Removed entries (tombstones) are skipped.
  • Returns Option.none() when the specified key or element does not exist, or when no next/previous entry is available.
  • Reuses the existing internal slot index and offset to navigate from the specified entry.

Tests

Added tests covering:

  • Next and previous navigation in insertion order.
  • Missing keys and elements.
  • First and last entry boundaries.
  • Navigation after removing entries.
  • Consecutive removed entries.
  • Navigation after reindexing.

Verification

  • mvn -pl vavr "-Dtest=LinkedHashMapTest,LinkedHashSetTest" test — passed.
  • mvn spotless:check — passed.
  • git diff --check — passed.

Closes #3363

* @param key the key
* @return the next entry, or {@code None} if the key is absent or has no next entry
*/
public Option<Tuple2<K, V>> nextEntry(K key) {

@patrickguenther patrickguenther Nov 20, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I don't quite like this API. Why for example, wouldn't someone just iterate these containers, which is probably faster, or have some sort of slice-method.
If one were to call nextEntry or previousEntry in a loop, the work of slotOrNull will be multiplied.
Have you considered some iterator type instead, that carries the result of slotOrNull:

// LinkedHashMap:
public Option<BiDirectionalIterator<K, V>> biDirectionalIteratorFrom(K key);

// BiDirectionalIterator:
public class BiDirectionalIterator<K, V> {
  //...
  Tuple2<K, V> current();

  Option<MapEntryIterator<K, V>> next();

  Option<MapEntryIterator<K, V>> previous();
}

But even this is probably not ideal. Maybe something like this wold be even better (though less functional):

// LinkedHashMap:
public Iterator<Tuple2<K, V>> iteratorFrom(K key);
public Iterator<Tuple2<K, V>> reverseIteratorFrom(K key);

@yashdotdev13

yashdotdev13 commented Nov 20, 2026 •

Copy link
Copy Markdown
Author

Hi @patrickguenther, Thanks for the detailed feedback!

I see your point about repeatedly calling slotOrNull() when navigating through multiple entries. An iterator-based API would avoid performing a fresh key lookup on every step and would provide a more natural traversal interface.

I agree that the simpler approach may be preferable here:

  • iteratorFrom(K key)
  • reverseIteratorFrom(K key)

I'll look into adapting the implementation to this API while reusing the existing insertion-order structure and avoiding unnecessary lookups.

Thanks for suggesting this alternative!

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.

Find the next/previous entry from a given key in LinkedHashMap / LinkedHashSet

2 participants