Implementation of obtaining data precursor and successor elements in Java collections _java_ scripting home

An implementation of obtaining data precursor and successor elements in a Java collection

Updated: May 01, 2024 08:37:00 Author: Long Da.
When using consistent hash, how to find a hash corresponding to the adjacent node, you can use the precursor and successor elements of the collection to obtain data, so this article introduces the implementation of the Java collection to obtain data precursor and successor elements, there are relevant code examples for everyone to refer to, need friends can refer to

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 itnext() 和 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

  • SpringBoot接入支付宝支付的方法步骤

    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 below
    2020-12-12
  • SpringBoot实现redis延迟队列的示例代码

    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 understand
    2024-02-02
  • 深入理解springboot中配置文件application.properties

    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 it
    2021-10-10
  • springmvc使用JSR-303进行数据校验实例

    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排查一个线上死循环cpu暴涨的过程分析

    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 look
    2020-08-08
  • 分别在Groovy和Java中创建并初始化映射的不同分析

    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 helpful

    2022-03-03
  • Spring IOC与DI核心重点分析

    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 + DI
    2022-10-10
  • java的多线程用法编程总结

    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分布式锁的功能使用及实现

    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 next
    2022-08-08
  • Spring Security 单点登录简单示例详解

    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 look
    2019-02-02

Latest comments