Use scenarios
When using a consistent hash, how to find a nearby node corresponding to a hash value can be implemented using the predecessor and successor elements that obtain data in the collection.
1. NavigableSet and NavigableMap
-
characteristic:
- The NavigableSet and NavigableMap interfaces provide rich ways to obtain predecessor and successor elements of a given element.
-
higher(E e)
The method returns the smallest element larger than the given element. If such element does not exist, it returnsnull
。 -
lower(E e)
The method returns the largest element smaller than the given element. If such element does not exist, it returnsnull
。 -
ceiling(E e)
The method returns the smallest element greater than or equal to the given element. If such element does not exist, it returnsnull
。 -
floor(E e)
The method returns the maximum element less than or equal to the given element. If such element does not exist, it returnsnull
。
-
Implementation Class:
- TreeSet and TreeMap are concrete implementations of NavigableSet and NavigableMap, both of which are ordered collections.
2. ListIterator
-
characteristic:
- A collection of List interfaces, such as ArrayList or LinkedList, can be used to traverse the collection using the ListIterator iterator.
- ListIterator provides
next()
andprevious()
Method to get the next and previous elements respectively.
3. ConcurrentSkipListSet and ConcurrentSkipListMap
-
characteristic:
- These two classes are thread-safe implementations of NavigableSet and NavigableMap.
- They provide the same predecessor and successor methods as TreeSet and TreeMap, suitable for concurrent environments.
4. Show list
1. Use NavigableSet (TreeSet example)
import ; import ; public class NavigableSetExample { public static void main(String[] args) { NavigableSet<Integer> set = new TreeSet<>(); (1); (3); (5); (7); // Get the successor element of the given element Integer higher = (5); // Return to 7 // Get the predecessor element of the given element Integer lower = (5); // Return to 3 ("Higher than 5: " + higher); ("Lower than 5: " + lower); } }
import ; import ; public class CeilingExample { public static void main(String[] args) { NavigableSet<Integer> set = new TreeSet<>(); (1); (3); (5); (7); // Get the smallest element greater than or equal to the given element Integer ceiling = (5); // Return to 5 ("Ceiling of 5: " + ceiling); } }
In this example, when calling(5);
When , it will return5
. This is because5
Already exists in the set, so according toceiling(E e)
The definition of the method, which will return the smallest element greater than or equal to the given element, in this case, is5
Original.
Summarize
In Java, if you need to get the next or previous data of a certain data, you can use a collection of NavigableSet or NavigableMap interfaces, such as TreeSet and TreeMap, or its thread-safe versions ConcurrentSkipListSet and ConcurrentSkipListMap. For collections that implement the List interface, the previous and last elements can be obtained through ListIterator. Choosing the right set depends on the type of data, the sorting requirements of the set, and whether it is thread-safe.
The above is the detailed content of the implementation of obtaining data precursors and successor elements in Java collections. For more information about obtaining data elements in Java collections, please pay attention to my other related articles!