Friday, September 5, 2025
HomeLanguagesJavaJava.util.TreeMap.pollFirstEntry() and pollLastEntry() in Java

Java.util.TreeMap.pollFirstEntry() and pollLastEntry() in Java

Java.util.TreeMap also contains functions that support retrieval and deletion at both, high and low end of values and hence give a lot of flexibility in applicability and daily use. This function is poll() and has 2 variants discussed in this article.

1. pollFirstEntry() : It removes and retrieves a key-value pair with the least key value in the map and “null” is map is empty.

Syntax : 
public Map.Entry pollFirstEntry()
Parameters:
NA
Return Value:
Retrieves and removes the least key-value if map is filled else returns null.
Exception:
NA




// Java code to demonstrate the working
// of pollFirstEntry()
import java.io.*;
import java.util.*;
public class pollFirstEntry {
    public static void main(String[] args)
    {
  
        // Declaring the tree map of String and Integer
        TreeMap<String, Integer> tmp = new TreeMap<String, Integer>();
  
        // Trying to retrieve and remove in empty map
        // returns null
        System.out.println
        ("The smallest key value pair is : " + tmp.pollFirstEntry());
  
        // assigning the values in the tree map
        // using put()
        tmp.put("Geeks", 1);
        tmp.put("for", 4);
        tmp.put("geeks", 1);
  
        // Printing the initial map
        System.out.println
        ("The initial Map before deletion is : " + tmp);
  
        // Use of pollFirstEntry()
        // Removes the first entry and returns the least key
        // lexicographically smallest in case of String
        // prints Geeks-1
        System.out.println
        ("The smallest key value pair is : " + tmp.pollFirstEntry());
  
        // Printing the map after deletion
        System.out.println
        ("The resultant Map after deletion is : " + tmp);
    }
}


Output:

The smallest key value pair is : null
The initial Map before deletion is : {Geeks=1, for=4, geeks=1}
The smallest key value pair is : Geeks=1
The resultant Map after deletion is : {for=4, geeks=1}

2. pollLastEntry() : It removes and retrieves a key-value pair with the largest key value in the map and “null” is map is empty.

Syntax : 
public Map.Entry pollLastEntry()
Parameters:
NA
Return Value:
Retrieves and removes the largest key-value if map is filled else returns null.
Exception:
NA




// Java code to demonstrate the working
// of pollLastEntry()
import java.io.*;
import java.util.*;
public class pollLastEntry {
    public static void main(String[] args)
    {
  
        // Declaring the tree map of String and Integer
        TreeMap<String, Integer> tmp = new TreeMap<String, Integer>();
  
        // Trying to retrieve and remove in empty map
        // returns null
        System.out.println
        ("The largest key value pair is : " + tmp.pollFirstEntry());
  
        // assigning the values in the tree map
        // using put()
        tmp.put("Geeks", 1);
        tmp.put("for", 4);
        tmp.put("geeks", 1);
  
        // Printing the initial map
        System.out.println
        ("The initial Map before deletion is : " + tmp);
  
        // Use of pollLastEntry()
        // Removes the last(max) entry and returns the max key
        // lexicographically greatest in case of String
        // prints geeks-1
        System.out.println
        ("The largest key value pair is : " + tmp.pollLastEntry());
  
        // Printing the map after deletion
        System.out.println
        ("The resultant Map after deletion is : " + tmp);
    }
}


Output:

The largest key value pair is : null
The initial Map before deletion is : {Geeks=1, for=4, geeks=1}
The largest key value pair is : geeks=1
The resultant Map after deletion is : {Geeks=1, for=4}

Practical Application : There are many applications that can be thought using the concept of deque or priority queueing. One such example is shown in the code below.




// Java code to demonstrate the application
// of pollLastEntry() and pollFirstEntry()
import java.io.*;
import java.util.*;
public class pollAppli {
    public static void main(String[] args)
    {
  
        // Declaring the tree map of Integer and String
        TreeMap<Integer, String> que = new TreeMap<Integer, String>();
  
        // assigning the values in que
        // using put()
        que.put(10, "astha");
        que.put(4, "shambhavi");
        que.put(7, "manjeet");
        que.put(8, "nikhil");
  
        // Defining the priority
        // takes highest value, if priority is high
        // else takes lowest value
        String prio = "high";
  
        // Printing the initial queue
        System.out.println("The initial queue is : " + que);
        if (prio == "high") {
            System.out.println
            ("The largest valued person is : " + que.pollLastEntry());
            System.out.println
            ("The resultant queue after deletion is : " + que);
        }
        else {
            System.out.println
            ("The lowest valued person is : " + que.pollFirstEntry());
            System.out.println
            ("The resultant queue after deletion is : " + que);
        }
    }
}


Output:

The initial queue is : {4=shambhavi, 7=manjeet, 8=nikhil, 10=astha}
The largest valued person is : 10=astha
The resultant queue after deletion is : {4=shambhavi, 7=manjeet, 8=nikhil}
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS