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