Java NavigableMap pollLastEntry()方法

Java NavigableMap pollLastEntry()方法

Java中NavigableMap接口的pollLastEntry()方法用于删除并返回与该地图中最大键相关的键值映射,如果该地图为空,则为空。

语法:

Map.Entry< K, V > pollLastEntry()

其中,K是这个地图所维护的键的类型,V是映射到键的值的类型。

参数 :该函数不接受任何参数。

返回值 :它返回一个与该地图中最大的键相关的键值映射,如果该地图为空,则返回空。

以下程序说明了Java中的pollLastEntry()方法。

程序1 :当键为整数时。

// Java code to demonstrate the working of
// pollLastEntry() 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("Removed key-value associated with"+
                     " greatest key : " + nmmp.pollLastEntry());
    }
}

输出:

Removed key-value associated with greatest key : 7=seven

程序2 :当按键为字符串时。

// Java code to demonstrate the working of
// pollLastEntry() 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("Removed key-value associated with"+
                  " greatest the key : " + tmmp.pollLastEntry());
    }
}

输出:

Removed key-value associated with greatest the key : two=three

参考资料 : https://docs.oracle.com/javase/10/docs/api/java/util/NavigableMap.html#pollLastEntry()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程