NavigableMap pollLastEntry() method in Java

The pollLastEntry() method of NavigableMap interface in Java is used to remove and return a key-value mapping associated with the greatest key in this map, or null if the map is empty.

Syntax:

Map.Entry< K, V > pollLastEntry()

Where, K is the type of key maintained by this map and V is the type of values mapped to the keys.

Parameters: This function does not accepts any parameter.

Return Value: It returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

Below programs illustrate the pollLastEntry() method in Java:

Program 1: When the key is integer.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Program 2: When the key is string.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/NavigableMap.html#pollLastEntry()



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.