当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ConcurrentSkipListMap ceilingKey()用法及代码示例


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

用法:

public K ceilingKey(K key)

参数:该函数接受单个强制性参数key。


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

异常:该方法引发两种类型的异常:

  • ClassCastException:如果指定的键无法与Map中当前的键进行比较,并且
  • NullPointerException :如果指定的键为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-



相关用法


注:本文由纯净天空筛选整理自Twinkl Bajaj大神的英文原创作品 ConcurrentSkipListMap ceilingKey() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。