Java中NavigableMap的firstEntry()方法
Java中NavigableMap接口的firstEntry()方法用于返回与此映射中最小键相关联的键-值映射,如果映射为空,则返回null。
语法 :
Map.Entry< K, V > firstEntry()
其中,K是此映射维护的键的类型,V是映射到键的值的类型。
参数 :不接受任何参数。
返回值 : 返回与此映射中最小键相关联的键-值映射,如果映射为空,则返回null。
下面的程序说明了Java中的firstEntry()方法:
程序1 :当键是整数时。
// Java代码演示
// firstEntry()方法
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// 声明一个Integer和String类型的NavigableMap
NavigableMap<Integer, String> nmmp = new TreeMap<>();
// 使用put()方法在NavigableMap中分配值
nmmp.put(2, "two");
nmmp.put(7, "seven");
nmmp.put(3, "three");
System.out.println("最小键的映射是:"
+ nmmp.firstEntry());
}
}
最小键的映射是:2=two
程序2 :当键是字符串时。
// Java代码演示
// firstEntry()方法
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// 声明一个String类型的NavigableMap
NavigableMap<String, String> tmmp = new TreeMap<>();
// 使用put()方法在NavigableMap中分配值
tmmp.put("one", "two");
tmmp.put("six", "seven");
tmmp.put("two", "three");
System.out.println("与最小键相关联的映射是:"
+ tmmp.firstEntry());
}
}
与最小键相关联的映射是:one=two
参考文献 : https://docs.oracle.com/javase/10/docs/api/java/util/NavigableMap.html#firstEntry()
极客教程