An implementation of obtaining data precursor and successor elements in a Java collection
Usage scenario
When using consistent hash, how to find the adjacent node corresponding to a hash value can be implemented using the predecessor and successor elements of the collection to obtain data.
1. NavigableSet and NavigableMap
Features:
- The NavigableSet and NavigableMap interfaces provide rich methods to get the precursor and successor elements of a given element.
higher(E e)
Method returns the smallest element larger than the given element, or if no such element existsnull
.lower(E e)
Method returns the largest element smaller than the given element, or if no such element existsnull
.ceiling(E e)
Method returns the smallest element greater than or equal to the given element, or if no such element existsnull
.floor(E e)
Method returns the largest element that is less than or equal to the given element, or if no such element existsnull
.
Implementation class:
- TreeSet and TreeMap are concrete implementations of NavigableSet and NavigableMap, which are both ordered sets.
2. ListIterator
- peculiarity:
- Collections that implement the List interface, such as ArrayList or LinkedList, can be traversed using ListIterator iterators.
- The ListIterator provides it
next()
和previous()
Method to get the next and previous elements, respectively.
3. ConcurrentSkipListSet and ConcurrentSkipListMap
- peculiarity:
- These two classes are thread-safe implementations of NavigableSet and NavigableMap.
- They provide the same precursor and successor methods as TreeSet and TreeMap and are suitable for concurrent environments.
4. Display columns
1. Use NavigableSet (TreeSet example)
import java.util.NavigableSet; import java.util.TreeSet; public class NavigableSetExample { public static void main(String[] args) { NavigableSet<Integer> set = new TreeSet<>(); set.add(1); set.add(3); set.add(5); set.add(7); Integer higher = set.higher(5); // Gets the successor element of the given element. Integer higher = set.higher(5); Integer lower = set.lower(5); // Return 7 // Gets the precursor element of the given element. Integer lower = set.lower(5); // Returns 3 System.out.println("Higher than 5: "+ higher); System.out.println("Lower than 5: " + lower); }}
import java.util.NavigableSet; import java.util.TreeSet; public class CeilingExample { public static void main(String[] args) { NavigableSet<Integer> set = new TreeSet<>(); set.add(1); set.add(3); set.add(5); set.add(7); Integer ceiling = set.ceiling(5); // Gets the smallest element greater than or equal to the given element. Integer ceiling = set.ceiling(5); // Returns 5 System.out.println("Ceiling of 5: "+ ceiling); }}
In this example, when calling set.ceiling(5); When, 5 will be returned. This is because 5 already exists in the set, so by the definition of the ceiling(E e) method, it will return the smallest element greater than or equal to the given element, in this case, 5 itself.
Sum up
In Java, if you need to get the next or previous data of a piece of data, you can use collections that implement 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 preceding and following elements can be retrieved through the ListIterator. Choosing the right collection depends on the type of data, the sorting needs of the collection, and whether thread safety is required.
These are the details of the implementation of obtaining data precursor and successor elements in Java collections. For more information about obtaining data elements in Java collections, please pay attention to other related articles in Script House!
Related article
-
Methods and steps of SpringBoot connecting to Alipay
This article mainly introduces the methods and steps of SpringBoot access to Alipay payment, the article introduces very detailed through the example code, which has certain reference value for everyone's study or work, and the friends who need to study together with Xiaobian below2020-12-12 -
SpringBoot implementation of redis delay queue example code
Delay queue scenario is often encountered in our daily business development, it is a special type of message queue, this article to introduce SpringBoot implementation of redis delay queue example code, has a certain reference value, interested you can understand2024-02-02 -
Take a deep dive into the springboot configuration file application.properties
This article mainly introduces the springboot configuration file application.properties, the article through the example code introduction is very detailed, has a certain reference value, interested partners can refer to it2021-10-10 -
springmvc uses JSR-303 for data verification instances
This article mainly introduces the detailed understanding of springmvc using JSR-303 for data verification, with a certain reference value, interested partners can refer to it.2017-02-02 -
java check an online loop cpu inflation process analysis
This article mainly introduces the process analysis of java checking an online dead loop cpu explosion, with a good reference value, I hope to help you. Let's take a look2020-08-08 -
Create and initialize different analyses of the maps in Groovy and Java, respectively
This article mainly introduces the different analysis of creating and initializing mappings in Groovy and Java respectively. If you need it, you can use it for reference. I hope it can be helpful2022-03-03 -
Core analysis of Spring IOC and DI
IOC is also one of the core of Spring, before learning is to use the xml configuration file to achieve, later which is also somewhat interspersed with a few annotations, but did not say that the complete use of annotations to achieve. So this article will share with you, all use annotations to achieve IOC + DI2022-10-10 -
java multithreaded usage programming summary
This paper mainly describes the use of multithreading in java, thread synchronization, thread data transfer, thread state and some corresponding thread function usage, overview and so on.2016-10-10 -
Java-Redis-Redisson distributed lock function use and implementation
This article mainly introduces the Java-Redis-Redison-distributed lock function use and implementation, this article through the example code to give you a very detailed introduction, everyone's study or work has a certain reference value, the need of friends can refer to the next2022-08-08 -
Spring Security Single sign-on simple example detailed
This article mainly introduces Spring Security single sign-on simple example detailed, Xiaobian feel very good, now share with you, also give you a reference. Let's take a look2019-02-02
Latest comments