Java中的NavigableMap pollFirstEntry()方法
Java中NavigableMap接口的pollFirstEntry()方法用于删除并返回与此地图中最小键相关联的键值映射,如果地图为空,则返回null。
语法 :
Map.Entry< K, V > pollFirstEntry()
其中,K是此映射维护的键的类型,V是映射到键的值的类型。
参数 : 此函数不接受任何参数。
返回值 : 它从此地图中删除最小键后返回与其关联的键值映射,如果地图为空,则返回null。
以下程序演示了Java中的pollFirstEntry()方法:
程序1 : 当键为整数时。
// Java code to demonstrate the working of
// pollFirstEntry() method
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("从映射中删除的第一个键值对为:"
+ nmmp.pollFirstEntry());
}
}
从映射中删除的第一个键值对为:2=two
程序2 : 当键为字符串时。
// Java code to demonstrate the working of
// pollFirstEntry() method
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("从映射中删除的第一个键值对为:"
+ tmmp.pollFirstEntry());
}
}
从映射中删除的第一个键值对为:one=two
参考 : https://docs.oracle.com/javase/10/docs/api/java/util/NavigableMap.html#pollFirstEntry()
极客教程