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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。