Java中的ConcurrentSkipListMap的ceilingKey()方法及示例

Java中的ConcurrentSkipListMap的ceilingKey()方法及示例

java.util.concurrent.ConcurrentSkipListMapceilingKey() 方法是Java中的内置函数,它返回大于或等于给定值的最小键值。如果没有这样的值,则返回null。当没有键时,该方法抛出NullPointerException。

语法:

public K ceilingKey(K key)

参数: 该函数接受一个强制性的单一参数 key ,它指定了键值。

返回值: 该函数返回大于或等于key的最小键值,如果不存在这样的key,则返回null。

异常: 该方法会抛出两种类型的异常:

  • ClassCastException: 如果指定的key无法与当前映射中的键进行比较和
  • NullPointerException: 如果指定的key为null。

下面的程序说明了上述方法:

程序1:

// Java program to demonstrate
// ceilingkey method in java
 
import java.util.concurrent.ConcurrentSkipListMap;
 
class GFG {
    public static void main(String[] args)
    {
 
        // Initializing the set
        // using ConcurrentSkipListMap()
        ConcurrentSkipListMap<Integer, Integer>
            mpp = new ConcurrentSkipListMap<Integer,
                                            Integer>();
 
        // Adding elements to this set
        mpp.put(1, 1);
        mpp.put(5, 2);
        mpp.put(2, 7);
 
        // Printing the ConcurrentSkipListMap
        // Always in ascending order
 
        System.out.println("Map: "
                           + mpp);
 
        System.out.println("key greater than or equal 3: "
                           + mpp.ceilingKey(3));
 
        System.out.println("key greater than or equal 2: "
                           + mpp.ceilingKey(2));
    }
}

输出:

Map: {1=1, 2=7, 5=2}
key greater than or equal 3: 5
key greater than or equal 2: 2

程序2:

// Java program to demonstrate
// ceilingkey method in java
import java.util.concurrent.ConcurrentSkipListMap;
 
class GFG {
    public static void main(String[] args)
    {
 
        // Initializing the set
        // using ConcurrentSkipListMap()
        ConcurrentSkipListMap<Integer, Integer>
            mpp = new ConcurrentSkipListMap<Integer,
                                            Integer>();
 
        // Adding elements to this set
        mpp.put(11, 1);
        mpp.put(51, 42);
        mpp.put(92, 7);
 
        // Printing the ConcurrentSkipListMap
        // Always in ascending order
 
        System.out.println("Map: "
                           + mpp);
 
        System.out.println("key greater than or equal 11: "
                           + mpp.ceilingKey(11));
 
        System.out.println("key greater than or equal 51: "
                           + mpp.ceilingKey(51));
    }
}

输出:

Map: {11=1, 51=42, 92=7}
key greater than or equal 11: 11
key greater than or equal 51: 51

参考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListMap.html#ceilingKey-K-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程