Java NavigableMap lowerEntry()方法
Java中NavigableMap接口的lowerEntry()方法用于返回与严格小于给定键的最大键相关的键值映射,如果不存在这样的键,则返回null。
语法:
Map.Entry< K, V > lowerEntry(K key)
其中,K是这个地图所维护的键的类型,V是映射到键的值的类型。
参数 :这个函数接受一个参数Key,它指的是这个地图容器所维护的键的类型。
返回值 :它返回一个与严格小于给定键的最大键相关的键值映射,如果没有这样的键存在,则返回空。
以下程序说明了Java中的lowerEntry()方法。
程序1 :当键为整数时。
// Java code to demonstrate the working of
// lowerEntry() 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("The mapping with greatest key strictly"
+ " less than 7 is : " + nmmp.lowerEntry(7));
}
}
输出:
The mapping with greatest key strictly less than 7 is : 3=three
程序2 :当按键为字符串时。
// Java code to demonstrate the working of
// lowerEntry() method
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the NavigableMap of Integer and String
NavigableMap<String, String> tmmp = new TreeMap<>();
// assigning the values in the NavigableMap
// using put()
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"));
}
}
输出:
The mapping with greatest key strictly less than 7 is : six=seven
参考资料 : https://docs.oracle.com/javase/10/docs/api/java/util/NavigableMap.html#lowerEntry(K)