The pollFirstEntry() method of NavigableMap interface in Java is used to remove and return a key-value mapping associated with the least key in this map, or null if the map is empty.
Syntax:
Map.Entry< K, V > pollFirstEntry()
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 least key after removing it from this map, or null if the map is empty.
Below programs illustrate the pollFirstEntry() method in Java:
Program 1: When the key is integer.
// Java code to demonstrate the working of // pollFirstEntry() 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("First removed key-value from the map : " + nmmp.pollFirstEntry()); } } |
First removed key-value from the map : 2=two
Program 2: When the key is string.
// Java code to demonstrate the working of // pollFirstEntry() 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("First removed key-value from the map : " + tmmp.pollFirstEntry()); } } |
First removed key-value from the map : one=two
Reference: https://docs.oracle.com/javase/10/docs/api/java/util/NavigableMap.html#pollFirstEntry()
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.util.TreeMap.pollFirstEntry() and pollLastEntry() in Java
- NavigableMap lastEntry() method in Java
- NavigableMap firstEntry() method in Java
- NavigableMap ceilingKey() method in Java
- NavigableMap ceilingEntry() method in Java
- NavigableMap pollLastEntry() method in Java
- NavigableMap lowerEntry() method in Java
- NavigableMap higherEntry() method in Java
- NavigableMap floorKey() method in Java
- NavigableMap floorEntry() method in Java
- NavigableMap lowerKey() Method in Java
- NavigableMap higherKey() Method in Java
- NavigableMap isEmpty() Method in Java
- NavigableMap clear() Method in Java
- NavigableMap size() Method in Java
- NavigableMap put() Method in Java
- NavigableMap Interface in Java with Example
- NavigableMap headMap() in Java
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
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.

