In Java, pollFirstEntry()removes the mapping with the lowest key from a TreeMap and returns it, or returns null if the map is empty. It combines firstEntry() and remove() in one O(log n) step.
The returned entry is a snapshot of the removed mapping, so it is still readable after the removal. Added in Java 6 with NavigableMap, it turns a TreeMap into a simple priority queue keyed by sort order.
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 takes the current lowest mapping out of the map.
Java
TreeMap<Integer, String> tasks = new TreeMap<>();
tasks.put(2, "review PR");
tasks.put(1, "fix outage");
Map.Entry<Integer, String> e = tasks.pollFirstEntry(); // 1=fix outage
// tasks is now {2=review PR}
Call
What it does
map.pollFirstEntry()
Removes and returns the lowest entry, or null.
map.firstEntry()
Same entry without removing it.
map.headMap(k).clear()
Removes every key below k at once.
Unlike Queue.remove() or firstKey(), pollFirstEntry never throws on an empty map; the null result doubles as the loop-ending signal.
Library declarations
Java
// java.util.TreeMap<K, V>
public Map.Entry<K, V> pollFirstEntry()
// Declared in NavigableMap<K, V> (Java 6)
Parameters
Parameter
Description
(none)
pollFirstEntry takes no parameters.
Return Value
Return Value
Description
Map.Entry<K, V>
Snapshot of the removed mapping with the lowest key.
Taken out, still readable. The snapshot prints SPRING10=10 even though the map is already empty.
No coupon, no exception. The second poll finds nothing and simply returns null.
A removed mapping cannot be edited.setValue(15) throws, which is sensible: there is no longer any tree node to write the value into.
Example 3 — Expire Sessions Older Than a Cutoff
Polling a headMap view removes the oldest sessions from the real map and stops at the cutoff.
Java
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
public class TreeMapPollFirstEntryExpire {
public static void main(String[] args) {
TreeMap<Integer, String> sessions = new TreeMap<>();
sessions.put(5, "ann");
sessions.put(18, "raj");
sessions.put(26, "lee");
sessions.put(40, "mo");
NavigableMap<Integer, String> stale = sessions.headMap(20, false);
for (Map.Entry<Integer, String> e = stale.pollFirstEntry(); e != null; e = stale.pollFirstEntry()) {
System.out.println("expired " + e.getValue() + " (login at " + e.getKey() + ")");
}
System.out.println("active: " + sessions);
}
}
Output
expired ann (login at 5)
expired raj (login at 18)
active: {26=lee, 40=mo}
How It Works
A window of old logins.headMap(20, false) covers minutes 5 and 18 only; 26 and 40 are outside the view.
Polling the view edits the map. Each pollFirstEntry on stale deletes that session from sessions as well, because the view is live.
The cutoff ends the loop. Once 5 and 18 are gone the view is empty, the poll returns null, and the newer sessions survive untouched.
Frequently Asked Questions
It removes the mapping with the lowest key and returns it as a snapshot entry, or returns null if the map is empty.
No. It returns null, which makes it convenient as a loop condition.
Both return the lowest entry; pollFirstEntry() also removes it from the map.
Yes, as long as priorities (keys) are unique: put tasks by priority and take them with pollFirstEntry(). For duplicate priorities, store a list per key or use PriorityQueue.
Yes. Views are live, so the polled mapping is removed from the backing TreeMap.