K ceilingKey(K key)
描述 (Description)
ceilingKey(K key)方法用于返回大于或等于给定键的最小键,如果没有这样的键,则返回null。
声明 (Declaration)
以下是java.util.TreeMap.ceilingKey()方法的声明。
public K ceilingKey(K key)
参数 (Parameters)
key - 这是要匹配的关键。
返回值 (Return Value)
方法调用返回大于或等于key的最小键,如果没有这样的键则返回null。
异常 (Exception)
ClassCastException - 如果无法将指定的键与当前映射中的键进行比较,则抛出异常。
NullPointerException - 如果指定的键为null并且此映射使用自然排序,或者其比较器不允许空键,则抛出异常。
例子 (Example)
以下示例显示了java.util.TreeMap.ceilingKey()方法的用法。
package com.iowiki;
import java.util.*;
public class TreeMapDemo {
public static void main(String[] args) {
// creating tree map
NavigableMap<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");
System.out.println("Ceiling key entry for 4: "+ treemap.ceilingKey(4));
System.out.println("Ceiling key entry for 5: "+ treemap.ceilingKey(5));
System.out.println("Ceiling key entry for 7: "+ treemap.ceilingKey(7));
}
}
让我们编译并运行上面的程序,这将产生以下结果。
Ceiling key entry for 4: 5
Ceiling key entry for 5: 5
Ceiling key entry for 7: null