Java中的NavigableMap pollLastEntry()方法

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

null

语法 :

Map.Entry< K, V > pollLastEntry()

其中,K是由该映射维护的键的类型,V是映射到键的值的类型。

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

返回值 :它返回与此映射中最大键关联的键值映射,如果映射为空,则返回null。

下面的程序演示了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 :当键为string时。

// 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()

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享