Java TreeMap pollLastEntry()方法与示例
TreeMap类pollLastEntry()方法
pollLastEntry()方法在java.util包中可用。
pollLastEntry()方法用于返回然后删除与此TreeMap中存在的最大键元素值链接的条目(键值对)。
pollLastEntry()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
返回最后一个条目时,pollLastEntry()方法不会引发异常。
语法:
public Map.Entry pollLastEntry();
参数:
它不接受任何参数。
返回值:
该方法的返回类型为Map.Entry,如果存在,则检索具有最大键值的条目,否则返回null。
示例
//Java程序演示示例 
//Map.EntrypollLastEntry()的 
//TreeMap的方法 
import java.util.*;
public class PollLastEntryOfTreeMap {
    public static void main(String[] args) {
        //实例化TreeMap-
        TreeMap < Integer, String > tm = new TreeMap < Integer, String > ();
        //通过使用put()方法是
        //将键值对放在
        //树状图TM-
        tm.put(1, "C");
        tm.put(4, "C++");
        tm.put(3, "Java");
        tm.put(2, "Php");
        //显示TreeMaptm-
        System.out.println("tm: " + tm);
        //通过使用pollLastEntry()方法是
        //返回并删除键值对
        //与最大的关键元素值链接
        //即“4”"4"
        tm.pollLastEntry();
        //显示更新的TreeMapTM-
        System.out.println("tm.pollLastEntry(): " + tm);
    }
}输出结果
tm: {1=C, 2=Php, 3=Java, 4=C++}
tm.pollLastEntry(): {1=C, 2=Php, 3=Java}