In Java, lastKey() returns the highest key in a TreeMap, the one that comes last in the map's sort order. The map is not changed.
It is the mirror image of firstKey(): the lookup follows right children from the root, so it takes O(log n) time. Called on a range view, it returns the highest key inside that range, which makes "latest value before a cutoff" a one-liner.
An instance method of java.util.TreeMap, part of TreeMap since Java 1.2. 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 with no arguments to get the maximum key.
Java
TreeMap<Integer, String> builds = new TreeMap<>();
builds.put(3, "Lime");
builds.put(12, "Mango");
int latest = builds.lastKey(); // 12
Call
What it does
map.lastKey()
The highest key; throws when the map is empty.
map.lastEntry()
The highest key with its value, or null.
map.headMap(k).lastKey()
The highest key strictly below k.
lastKey() is the largest key by the comparator. String keys such as "12" and "7" compare character by character, so "7" would be last; store numbers as Integer when you want numeric order.
Library declarations
Java
// java.util.TreeMap<K, V>
public K lastKey()
// Declared in SortedMap<K, V>
Parameters
Parameter
Description
(none)
lastKey takes no parameters.
Return Value
Return Value
Description
K
The last (highest) key according to the map's ordering.
morning hours: [6, 9]
last morning reading at 9: 14.0
before 6: NoSuchElementException
How It Works
Noon is left out.headMap(12) is exclusive, so the view holds hours 6 and 9 but not 12.
Highest inside the window. The view's lastKey() is 9, and the reading 14.0 is fetched from the full map with that key.
An empty window still throws. No hour lies below 6, so headMap(6) is empty and its lastKey() raises NoSuchElementException.
Example 3 — Hand Out the Next Invoice Number
The next id is lastKey() + 1, with a starting value for an empty book.
Java
import java.util.TreeMap;
public class TreeMapLastKeyNextId {
static int nextId(TreeMap<Integer, String> invoices) {
return invoices.isEmpty() ? 1001 : invoices.lastKey() + 1;
}
public static void main(String[] args) {
TreeMap<Integer, String> invoices = new TreeMap<>();
System.out.println("empty book, next id: " + nextId(invoices));
invoices.put(nextId(invoices), "Garden chairs");
invoices.put(nextId(invoices), "Paint");
invoices.put(nextId(invoices), "Ladder");
System.out.println(invoices);
invoices.remove(1003);
System.out.println("after removing 1003, next id: " + nextId(invoices));
}
}
Output
empty book, next id: 1001
{1001=Garden chairs, 1002=Paint, 1003=Ladder}
after removing 1003, next id: 1003
How It Works
No crash on the first invoice.isEmpty() is checked before lastKey(), so the empty book returns the starting number 1001 instead of throwing.
Each id builds on the last. Every put raises the maximum by one, producing 1001, 1002, and 1003.
Watch for reuse. Deleting the top invoice lowers lastKey() to 1002, so 1003 would be issued a second time; keep a separate counter if numbers must never repeat.
Frequently Asked Questions
The highest key according to the map's ordering, for example the largest Integer or the alphabetically last String under natural ordering.
The map (or the view you called it on) has no keys. Use lastEntry(), which returns null, or check isEmpty() first.
Call lastEntry().getValue(), or get(map.lastKey()). Both read the value without changing the map.
Yes: headMap(limit).lastKey() returns the highest key strictly below limit. lowerKey(limit) does the same but returns null instead of throwing when there is none.
No. TreeMap does not remember insertion order; lastKey() is the largest key by sorting. Use LinkedHashMap if you need insertion order.