In Java, lastEntry() returns the mapping with the highest key in a TreeMap as a Map.Entry, or null when the map is empty. It is the null-safe, key-plus-value companion of lastKey().
The entry is a snapshot: later changes to the map are not reflected in it, and setValue is not supported. Added in Java 6 with NavigableMap, it runs in O(log n) time.
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
Call it without arguments and read getKey() and getValue().
Java
TreeMap<Double, String> jumps = new TreeMap<>();
jumps.put(7.45, "Ines");
jumps.put(8.02, "Rae");
Map.Entry<Double, String> best = jumps.lastEntry(); // 8.02=Rae
Call
What it does
map.lastEntry()
Highest entry, or null when empty.
map.lastKey()
Highest key only; throws when empty.
map.pollLastEntry()
Highest entry, removed from the map.
Because TreeMap sorts by key, "last" means largest key, never "most recently added". If two values share a key, the later put replaced the earlier one, so only one of them can be the last entry.
Library declarations
Java
// java.util.TreeMap<K, V>
public Map.Entry<K, V> lastEntry()
// Declared in NavigableMap<K, V> (Java 6)
Distances in metres are Double keys; the last entry is the best jump and its athlete.
Java
import java.util.Map;
import java.util.TreeMap;
public class TreeMapLastEntryRecord {
public static void main(String[] args) {
TreeMap<Double, String> jumps = new TreeMap<>();
jumps.put(7.45, "Ines");
jumps.put(8.02, "Rae");
jumps.put(7.91, "Tom");
Map.Entry<Double, String> best = jumps.lastEntry();
System.out.println("best jump: " + best);
System.out.println("winner: " + best.getValue() + " with " + best.getKey() + " m");
}
}
Output
best jump: 8.02=Rae
winner: Rae with 8.02 m
How It Works
Double keys sort numerically. 7.45, 7.91, and 8.02 are ordered by value, putting Rae's 8.02 at the right end of the tree.
Winner and distance together.getValue() names the athlete and getKey() gives the distance from the very same snapshot.
Ties would collide. Two athletes with exactly 8.02 would share one key, so the second put would replace the first; a list per key avoids losing anyone.
Example 2 — A Saved lastEntry() Does Not See Later Updates
Firmware version 12 is promoted from beta to stable after the snapshot was taken.
oldest: Gus (71)
youngest: Leo (8)
age span: 63 years
How It Works
Largest age at the end. With ages as keys, the last entry belongs to Gus at 71.
The other end for free.firstEntry() yields Leo at 8, the smallest key, in the same O(log n) time.
Arithmetic on keys. Subtracting the two Integer keys (auto-unboxed) gives the span of 63 years; with an empty map both entries would be null, so real code checks first.
Frequently Asked Questions
The mapping with the highest key as a Map.Entry, or null if the map is empty.
lastEntry() returns key and value and gives null when empty; lastKey() returns only the key and throws NoSuchElementException when empty.
No. The entry is a snapshot and setValue throws UnsupportedOperationException. Use put with the same key.
No. It is the entry with the largest key. TreeMap keeps sorted order, not insertion order.
No. pollLastEntry() is the version that returns and removes the highest entry.