Java中的NavigableMap pollLastEntry()方法
Java中NavigableMap接口的pollLastEntry()方法用于删除和返回与此映射中的最大键相关联的键值对,如果映射为空,则返回null。
语法:
Map.Entry< K, V > pollLastEntry()
其中,K是此地图维护的键的类型,V是映射到键的值的类型。
参数: 此函数不接受任何参数。
返回值: 它返回与此地图中最大键相关联的键值对,如果映射为空,则返回null。
以下程序说明了Java中的pollLastEntry()方法:
程序1: 当键为整数时。
//Java代码演示了
// pollLastEntry()方法
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.pollLastEntry());
}
}
最大键相关联的已删除键值:7=seven
程序2: 当键为字符串时。
//Java代码演示了
// pollLastEntry()方法
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// 声明String和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.pollLastEntry());
}
}
最大键相关联的已删除键值:two=three
参考: https://docs.oracle.com/javase/10/docs/api/java/util/NavigableMap.html#pollLastEntry()
极客教程