📜  Java中的 TreeMap ceilingKey() 和示例

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

Java中的 TreeMap ceilingKey() 和示例

TreeMap 类ceilingKey()函数返回大于或等于给定键的最小键,如果不存在这样的键,则返回 null。

句法:

public K ceilingKey(K key)

参数:此方法接受一个强制参数,它是要搜索的键。

返回值:此方法返回大于或等于给定键值的最小键
如果没有这样的键,则返回 null。

异常:此方法引发以下异常:

  • ClassCastException – 如果指定的键无法与给定的键值进行比较,则抛出该异常。
  • NullPointerException – 如果给定键为空且映射使用自然排序或比较器不允许空值,则抛出此异常。

以下是说明 ceilingKey() 方法的示例:

程序 1:演示对带有比较器的 TreeMap 使用 ceilingKey() 方法

import java.util.*;
  
public class Main {
    public static void main(String[] args)
    {
  
        // creating tree map
        NavigableMap
            treemap = new TreeMap((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:演示在没有任何比较器的 TreeMap 中使用 ceilingKey() 方法

import java.util.*;
  
public class Main {
    public static void main(String[] args)
    {
  
        // creating tree map
        NavigableMap
            treemap = new TreeMap();
  
        // 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:演示在返回 null 时使用 ceilingKey() 方法

import java.util.*;
  
public class Main {
    public static void main(String[] args)
    {
  
        // creating tree map
        NavigableMap
            treemap = new TreeMap();
  
        // 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
            treemap = new TreeMap();
  
        // 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
            treemap = new TreeMap();
  
        // 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