當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。