Repository navigation
Add neighbor navigation to LinkedHashMap and LinkedHashSet - #3393
yashdotdev13 wants to merge 1 commit into
Conversation
| * @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) { |
There was a problem hiding this comment.
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);|
Hi @patrickguenther, Thanks for the detailed feedback! I see your point about repeatedly calling I agree that the simpler approach may be preferable here:
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! |
Summary
This PR adds methods to navigate to the next and previous entries in insertion order for
LinkedHashMapandLinkedHashSet.Changes
LinkedHashMap
nextEntry(K key)to retrieve the next entry after the given key.previousEntry(K key)to retrieve the previous entry before the given key.Option<Tuple2<K, V>>.LinkedHashSet
next(T element)to retrieve the next element.previous(T element)to retrieve the previous element.Option<T>and delegate to the correspondingLinkedHashMapmethods.Implementation details
Option.none()when the specified key or element does not exist, or when no next/previous entry is available.Tests
Added tests covering:
Verification
mvn -pl vavr "-Dtest=LinkedHashMapTest,LinkedHashSetTest" test— passed.mvn spotless:check— passed.git diff --check— passed.Closes #3363