Java中NavigableMap的floorKey()方法
Java中NavigableMap接口的floorKey()方法用于返回小于或等于给定键的最大键,如果没有这样的键,则返回null。
语法 :
K floorKey(K key)
其中,K是此映射维护的键的类型。
参数 : 此函数接受一个参数Key,该参数是此映射容器维护的键的类型。
返回值 : 返回小于或等于给定键的最大键,如果没有这样的键,则返回null。
下面的程序演示了Java中的floorKey()方法:
程序1 : 当键为整数时。
// Java code to demonstrate the working of
// floorKey() method
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the NavigableMap of Integer and String
NavigableMap<Integer, String> nmmp = new TreeMap<>();
// assigning the values in the NavigableMap
// using put()
nmmp.put(2, "two");
nmmp.put(7, "seven");
nmmp.put(3, "three");
System.out.println("最大键的映射是: " + nmmp.floorKey(7));
}
}
输出结果:最大键的映射是: 7
程序2 : 当键是字符串时。
// Java code to demonstrate the working of
// floorKey() method
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// 声明Integer和String的NavigableMap
NavigableMap<String, String> tmmp = new TreeMap<>();
// 分配NavigableMap中的值
// 使用put()
tmmp.put("one", "two");
tmmp.put("six", "seven");
tmmp.put("two", "three");
System.out.println("与最大键相关联的映射是:" + tmmp.floorKey("one"));
}
}
输出结果:与最大键相关联的映射是:one
引用文献 : https://docs.oracle.com/javase/10/docs/api/java/util/NavigableMap.html#floorKey(K)
极客教程