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


Java TreeMap ceilingKey()用法及代碼示例


TreeMap類的ceilingKey()函數返回大於或等於給定鍵的最小鍵;如果不存在這樣的鍵,則返回null。

用法:

public K ceilingKey(K key)

參數:此方法接受必需的參數 key ,這是要搜索的 key 。


返回值:此方法返回大於或等於給定鍵值的最小鍵。
如果缺少這樣的 key ,則返回null。

異常:此方法引發以下異常:

  • ClassCastException–如果無法將指定鍵與給定鍵值進行比較,則拋出該異常。
  • NullPointerException –如果給定鍵為空並且映射使用自然順序,或者比較器不允許為空值,則拋出該異常。

下麵是說明ceilingKey()方法的示例:

示例1:演示如何將ceilingKey()方法用於具有比較器的TreeMap

import java.util.*; 
  
public class Main { 
    public static void main(String[] args) 
    { 
  
        // creating tree map 
        NavigableMap<Integer, String> 
            treemap = new TreeMap<Integer, 
                                  String>((a, b) 
                                              -> ((a > b) 
                                                      ? 1
                                                      : ((a == b) 
                                                             ? 0
                                                             : -1))); 
  
        // populating tree map 
        treemap.put(1, " A "); 
        treemap.put(2, " B "); 
        treemap.put(3, " C "); 
        treemap.put(4, " D "); 
        treemap.put(6, " E "); 
        try { 
            System.out.println("Ceiling key entry for 5: "
                               + treemap.ceilingKey(5)); 
        } 
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
輸出:
Ceiling key entry for 5: 6

示例2:演示如何使用ceilingKey()方法在沒有任何比較器的TreeMap中使用

import java.util.*; 
  
public class Main { 
    public static void main(String[] args) 
    { 
  
        // creating tree map 
        NavigableMap<Integer, String> 
            treemap = new TreeMap<Integer, String>(); 
  
        // populating tree map 
        treemap.put(1, " A "); 
        treemap.put(2, " B "); 
        treemap.put(3, " C "); 
        treemap.put(4, " D "); 
        treemap.put(6, " E "); 
        treemap.put(7, " F "); 
  
        // Since 6 is the least value greater than 5, 
        // it is returned as the key. 
        System.out.println("Ceiling key entry for 5: "
                           + treemap.ceilingKey(5)); 
    } 
}
輸出:
Ceiling key entry for 5: 6

示例3:演示當ceilingKey()方法將返回null時的用法

import java.util.*; 
  
public class Main { 
    public static void main(String[] args) 
    { 
  
        // creating tree map 
        NavigableMap<Integer, String> 
            treemap = new TreeMap<Integer, String>(); 
  
        // populating tree map 
        treemap.put(1, " A "); 
        treemap.put(2, " B "); 
        treemap.put(3, " C "); 
        treemap.put(4, " E "); 
        treemap.put(5, " D "); 
  
        // Since 10 is not present in the Map 
        // and neither any Key is present greater than 10 
        // Hence this will return null 
        System.out.println("Ceiling key entry for 10: "
                           + treemap.ceilingKey(10)); 
    } 
}
輸出:
Ceiling key entry for 10: null

示例4:顯示NullPointerException

import java.util.*; 
  
public class Main { 
    public static void main(String[] args) 
    { 
  
        // creating tree map 
        TreeMap<Integer, String> 
            treemap = new TreeMap<Integer, String>(); 
  
        // populating tree map 
        treemap.put(2, " two "); 
        treemap.put(1, " one "); 
        treemap.put(3, " three "); 
        treemap.put(6, " six "); 
        treemap.put(5, " five "); 
  
        try { 
            // returns a NullPointerException 
            // as key value can't be null 
            // because of natural ordering 
            System.out.println("Ceiling key entry for null value : "
                               + treemap.ceilingKey(null)); 
        } 
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
輸出:
Exception: java.lang.NullPointerException

示例5:演示ClassCastException

import java.util.*; 
  
public class Main { 
    public static void main(String[] args) 
    { 
  
        // creating tree map 
        NavigableMap<Object, String> 
            treemap = new TreeMap<Object, String>(); 
  
        // populating tree map 
        treemap.put(1, " A "); 
        treemap.put(2, " B "); 
        treemap.put(3, " C "); 
        treemap.put(4, " E "); 
        treemap.put(5, " D "); 
  
        try { 
            // returns ClassCastException 
            // as we cannot compare a String object with an Integer object 
            System.out.println("Ceiling key entry for \"asd\": "
                               + treemap.ceilingKey(new String("asd"))); 
        } 
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
輸出:
Exception: java.lang.ClassCastException: 
           java.lang.Integer cannot be cast to java.lang.String


相關用法


注:本文由純淨天空篩選整理自psil123大神的英文原創作品 TreeMap ceilingKey() in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。