Java ConcurrentSkipListMap ceilingKey()方法及示例

Java ConcurrentSkipListMap ceilingKey()方法及示例

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

语法 :

public K ceilingKey(K key)

参数: 该函数接受一个强制参数 key ,它指定了键。
返回值: 该函数返回大于或等于key的最小键,如果没有这样的键,则返回null。
异常: 该方法抛出两类异常。

  • ClassCastException: 如果指定的键不能与当前地图中的键进行比较,以及
  • NullPointerException: 如果指定的键为空。

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

程序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教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程