Java中NavigableMap lowerEntry()方法
Java中NavigableMap接口的lowerEntry()方法用于返回与给定键严格小于最大键关联的键值映射,如果不存在这样的键则返回null。
语法:
Map.Entry< K, V > lowerEntry(K key)
其中,K是此映射维护的键的类型,V是映射到键的值的类型。
参数: 此函数接受一个单一参数Key,该参数是此映射容器所维护的键的类型。
返回值: 返回与给定键严格小于最大键关联的键值映射,如果不存在这样的键则返回null。
以下程序说明Java中的lowerEntry()方法:
程序1: 当键为整数时。
// Java代码以演示工作原理
// lowerEntry()方法
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
//声明整数和字符串的NavigableMap
NavigableMap<Integer, String> nmmp = new TreeMap<>();
//使用put()在NavigableMap中分配值
nmmp.put(2, “two”);
nmmp.put(7, “seven”);
nmmp.put(3, “three”);
System.out.println("The mapping with greatest key strictly"
+ " less than 7 is : " + nmmp.lowerEntry(7));
}
}
最大键严格小于7的映射是:3=three
程序2: 当键为字符串时。
// Java代码以演示工作原理
// lowerEntry()方法
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
//声明整数和字符串的NavigableMap
NavigableMap<String, String> tmmp = new TreeMap<>();
//使用put()在NavigableMap中分配值
tmmp.put("one", "two");
tmmp.put("six", "seven");
tmmp.put("two", "three");
System.out.println("The mapping with greatest key strictly"
+ " less than 7 is : " + tmmp.lowerEntry("two"));
}
}
最大键严格小于2的映射是:six=seven
参考: https://docs.oracle.com/javase/10/docs/api/java/util/NavigableMap.html#lowerEntry(K)
极客教程