📌  相关文章
📜  Java中的 ConcurrentSkipListMap ceilingKey() 方法及示例

📅  最后修改于: 2022-05-13 01:54:27.260000             🧑  作者: Mango

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

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

句法:

public K ceilingKey(K key)

参数:该函数接受一个指定键的强制参数

返回值:该函数返回大于或等于key的最小key,如果没有这样的key,则返回null。

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

  • ClassCastException:如果指定的键无法与地图中当前的键进行比较,并且
  • NullPointerException:如果指定的键为空。

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

// 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
            mpp = new ConcurrentSkipListMap();
  
        // 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

方案二:

// 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
            mpp = new ConcurrentSkipListMap();
  
        // 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: Java/util/concurrent/ConcurrentSkipListMap.html#ceilingKey-K-