In Java, higherKey(key) returns the least key strictly greater thankey in a TreeMap, or null when nothing comes after it. An exact match is skipped, so passing a key that is in the map gives you its successor.
That makes it the natural "next" operation: step from one key to the following one, page through keys with a cursor, or rotate through a list. It belongs to NavigableMap (Java 6) and takes 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.
Each call is a fresh O(log n) search, so stepping through every key with higherKey costs O(n log n). For a plain full pass, an iterator over keySet() is cheaper; higherKey shines when you resume from a saved key.
Library declarations
Java
// java.util.TreeMap<K, V>
public K higherKey(K key)
// Declared in NavigableMap<K, V> (Java 6)
Parameters
Parameter
Description
key (K)
The key (or any probe value) to move past.
Return Value
Return Value
Description
K
The least key that compares strictly greater than key.
null
key is at or past the last key, or the map is empty.
Today's date is a key, and higherKey skips it to find the following event.
Java
import java.util.TreeMap;
public class TreeMapHigherKeyEvents {
public static void main(String[] args) {
TreeMap<String, String> events = new TreeMap<>();
events.put("2026-12-05", "Science fair");
events.put("2026-12-12", "Concert");
events.put("2026-12-20", "Winter break");
String today = "2026-12-12";
String next = events.higherKey(today);
System.out.println("today: " + events.get(today));
System.out.println("coming up: " + next + " " + events.get(next));
System.out.println("after the break: " + events.higherKey("2026-12-20"));
}
}
Output
today: Concert
coming up: 2026-12-20 Winter break
after the break: null
How It Works
Today is excluded. The concert's date equals the probe, and higherKey only accepts keys strictly greater, so the concert is not "coming up".
The successor. The least date after 2026-12-12 is 2026-12-20, whose value is "Winter break".
Nothing beyond the last key. Asking about the final date yields null; the earlier science fair is ignored because it lies before the probe.
Example 2 — Page Through Order Numbers Two at a Time
A cursor key remembers where the previous page stopped; higherKey moves it forward.
Java
import java.util.TreeMap;
public class TreeMapHigherKeyPagination {
public static void main(String[] args) {
TreeMap<Integer, String> orders = new TreeMap<>();
orders.put(101, "pens");
orders.put(104, "ink");
orders.put(107, "paper");
orders.put(110, "stapler");
orders.put(113, "folders");
Integer cursor = orders.firstKey();
int page = 1;
while (cursor != null) {
StringBuilder line = new StringBuilder("page " + page + ":");
for (int i = 0; i < 2 && cursor != null; i++) {
line.append(' ').append(cursor);
cursor = orders.higherKey(cursor);
}
System.out.println(line);
page++;
}
}
}
Output
page 1: 101 104
page 2: 107 110
page 3: 113
How It Works
Begin at the smallest order. The cursor starts at firstKey(), 101, and every higherKey call advances it to the next order number.
Gaps do not matter. The numbers jump by 3, yet higherKey(101) still lands on 104; the method finds the next stored key, not the next integer.
null ends both loops. After 113 the cursor becomes null, which stops the inner loop mid-page and then the outer one, leaving a short last page.
Example 3 — Round-Robin Ticket Assignment
Support desks take tickets in turn; after the highest desk number, assignment wraps back to the lowest.
Java
import java.util.TreeMap;
public class TreeMapHigherKeyRoundRobin {
public static void main(String[] args) {
TreeMap<Integer, String> desks = new TreeMap<>();
desks.put(2, "Ivy");
desks.put(5, "Marco");
desks.put(9, "Sun");
int current = 5;
for (int i = 0; i < 4; i++) {
Integer next = desks.higherKey(current);
current = (next != null) ? next : desks.firstKey();
System.out.println("next ticket goes to desk " + current + " (" + desks.get(current) + ")");
}
}
}
Output
next ticket goes to desk 9 (Sun)
next ticket goes to desk 2 (Ivy)
next ticket goes to desk 5 (Marco)
next ticket goes to desk 9 (Sun)
How It Works
Advance one desk. Starting from desk 5, the strictly greater key is 9, so Sun gets the first ticket.
Wrap at the top. Desk 9 has no successor, higherKey returns null, and the conditional falls back to firstKey(), desk 2.
A fair cycle. The order 9, 2, 5, 9 visits every desk once before repeating, and adding a desk number later slots it into the rotation automatically.
Frequently Asked Questions
The least key strictly greater than the argument, or null if no key is larger.
ceilingKey() returns the argument when it is a key; higherKey() always moves past it to the next key.
higherKey() returns null for the last key. To wrap around, fall back to firstKey() when the result is null.
Yes. Keep the last key you showed as a cursor and call higherKey(cursor) to continue. tailMap(cursor, false) gives the same keys as a view.
No. It only reads the tree; use pollFirstEntry() or remove() if you want to take keys out.