Home Java TreeMap Methods pollLastEntry() Java TreeMap pollLastEntry() Method Definition In Java, pollLastEntry() removes the mapping with the highest key from a TreeMap and returns it, or returns null when the map is empty. It is lastEntry() and a removal in one O(log n) call.
The result is a snapshot of the removed mapping. "Highest" follows the map or view ordering, so on a descendingMap() view the "last" entry is the smallest key. The method was added in Java 6 with NavigableMap.
An instance method of java.util.TreeMap, added in Java 6 (JDK 1.6) with the NavigableMap interface. A TreeMap keeps its key/value pairs sorted by key in a red-black tree, so lookups and updates take O(log n) time; it is not thread-safe .
Syntax Each call removes the current maximum and hands it back.
TreeMap<Integer, String> bids = new TreeMap<>();
bids.put(120, "Ana");
bids.put(340, "Ben");
Map.Entry<Integer, String> top = bids.pollLastEntry(); // 340=Ben
// bids is now {120=Ana} Call What it does map.pollLastEntry()Removes and returns the highest entry, or null. map.lastEntry()Same entry, left in the map. map.descendingMap().pollLastEntry()Removes the lowest key.
Because it removes what it returns, pollLastEntry is destructive. If you only want to read the top entries, iterate descendingMap() instead.
Library declarations // java.util.TreeMap<K, V>
public Map.Entry<K, V> pollLastEntry()
// Declared in NavigableMap<K, V> (Java 6) Parameters Parameter Description (none) pollLastEntry takes no parameters.
Return Value Return Value Description Map.Entry<K, V>Snapshot of the removed mapping with the highest key. nullThe map (or view) was empty.
Compare with Similar Methods Ways to take entries off the high end:
Java Examples 1. Top N, destructively 2. On a reversed view 3. Drain in reverse Example 1 — Award the Two Highest Bids Each winner is removed with pollLastEntry(), leaving the losing bids behind.
import java.util.Map;
import java.util.TreeMap;
public class TreeMapPollLastEntryTopBids {
public static void main(String[] args) {
TreeMap<Integer, String> bids = new TreeMap<>();
bids.put(120, "Ana");
bids.put(340, "Ben");
bids.put(275, "Cai");
bids.put(90, "Dee");
for (int prize = 1; prize <= 2; prize++) {
Map.Entry<Integer, String> win = bids.pollLastEntry();
System.out.println("prize " + prize + ": " + win.getValue() + " (" + win.getKey() + ")");
}
System.out.println("not awarded: " + bids);
}
} prize 1: Ben (340)
prize 2: Cai (275)
not awarded: {90=Dee, 120=Ana} How It Works Highest bid wins first. 340 is the largest key, so Ben takes prize 1 and his bid leaves the map.The runner-up is now on top. With 340 gone, the next poll returns 275, so Cai takes the second prize.Leftovers stay sorted. Only Dee and Ana remain, printed in ascending order as {90=Dee, 120=Ana}.Example 2 — pollLastEntry() on a Descending View The last entry of a descending view is the smallest key, and removing it changes the original map.
import java.util.NavigableMap;
import java.util.TreeMap;
public class TreeMapPollLastEntryDescending {
public static void main(String[] args) {
TreeMap<Integer, String> heights = new TreeMap<>();
heights.put(150, "Lia");
heights.put(172, "Omar");
heights.put(165, "Pia");
NavigableMap<Integer, String> tallestFirst = heights.descendingMap();
System.out.println("view: " + tallestFirst);
System.out.println("view.pollLastEntry(): " + tallestFirst.pollLastEntry());
System.out.println("map now: " + heights);
System.out.println("map.pollLastEntry(): " + heights.pollLastEntry());
}
} view: {172=Omar, 165=Pia, 150=Lia}
view.pollLastEntry(): 150=Lia
map now: {165=Pia, 172=Omar}
map.pollLastEntry(): 172=Omar How It Works Last in the view's order. The descending view lists 172, 165, 150, so its last entry is 150=Lia, the shortest person.Removed from both. The view has no storage of its own; polling it deletes 150 from heights, which now holds two people.Direct call on the map. On the ascending map itself, pollLastEntry() removes the true maximum, 172=Omar.Example 3 — Empty a Map From Z to A Polling until null collects the keys in descending order and leaves the map empty.
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class TreeMapPollLastEntryDrain {
public static void main(String[] args) {
TreeMap<String, Integer> scores = new TreeMap<>();
scores.put("Ada", 7);
scores.put("Ben", 9);
scores.put("Cy", 4);
List<String> order = new ArrayList<>();
Map.Entry<String, Integer> e;
while ((e = scores.pollLastEntry()) != null) {
order.add(e.getKey() + "=" + e.getValue());
}
System.out.println("Z to A: " + order);
System.out.println("map afterwards: " + scores + ", empty: " + scores.isEmpty());
}
} Z to A: [Cy=4, Ben=9, Ada=7]
map afterwards: {}, empty: true How It Works Assign and test in one line. (e = scores.pollLastEntry()) != null stores the polled entry and ends the loop on the first null.Reverse alphabetical order. "Cy" is the greatest string key, then "Ben", then "Ada", so the list is built from Z towards A.Nothing left behind. Every iteration removed one mapping, so the map ends as {} and isEmpty() is true; use descendingMap() if the data must be kept.Frequently Asked Questions What does TreeMap pollLastEntry() do? It removes the mapping with the highest key and returns it as a snapshot, or returns null when the map is empty.
Does pollLastEntry() throw on an empty map? No, it returns null. lastKey() is the method that throws NoSuchElementException.
How do I get the top N entries without removing them? Iterate descendingMap() and stop after N entries. pollLastEntry() removes what it returns.
What does pollLastEntry() do on a descendingMap() view? It removes the last entry in the view's order, which is the smallest key of the original map.
Can I change the value of the polled entry? No. The snapshot throws UnsupportedOperationException from setValue, and the mapping is no longer in the map anyway.
Next: put() Add a mapping or overwrite the value of an existing key.
put() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion