Java TreeMap pollLastEntry() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 6+

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.

Java
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}
CallWhat 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
// java.util.TreeMap<K, V>
public Map.Entry<K, V> pollLastEntry()

// Declared in NavigableMap<K, V> (Java 6)

Parameters

ParameterDescription
(none)pollLastEntry takes no parameters.

Return Value

Return ValueDescription
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:

MethodHow it differs
pollFirstEntry()Removes the lowest entry.
lastEntry()Reads without removing.
tailMap(k).clear()Removes every key from k up.
descendingMap()Read from high to low without removing.

Java Examples

Example 1 — Award the Two Highest Bids

Each winner is removed with pollLastEntry(), leaving the losing bids behind.

Java
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);
    }
}

How It Works

  1. Highest bid wins first. 340 is the largest key, so Ben takes prize 1 and his bid leaves the map.
  2. The runner-up is now on top. With 340 gone, the next poll returns 275, so Cai takes the second prize.
  3. 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.

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

How It Works

  1. Last in the view's order. The descending view lists 172, 165, 150, so its last entry is 150=Lia, the shortest person.
  2. Removed from both. The view has no storage of its own; polling it deletes 150 from heights, which now holds two people.
  3. 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.

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

How It Works

  1. Assign and test in one line. (e = scores.pollLastEntry()) != null stores the polled entry and ends the loop on the first null.
  2. Reverse alphabetical order. "Cy" is the greatest string key, then "Ben", then "Ada", so the list is built from Z towards A.
  3. 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

It removes the mapping with the highest key and returns it as a snapshot, or returns null when the map is empty.
No, it returns null. lastKey() is the method that throws NoSuchElementException.
Iterate descendingMap() and stop after N entries. pollLastEntry() removes what it returns.
It removes the last entry in the view's order, which is the smallest key of the original map.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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