Java TreeMap higherKey() Method

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

Definition

In Java, higherKey(key) returns the least key strictly greater than key 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.

Syntax

Pass the current key to get the next one.

Java
TreeMap<String, String> events = new TreeMap<>();
events.put("2026-12-12", "Concert");
events.put("2026-12-20", "Winter break");
String next = events.higherKey("2026-12-12");   // "2026-12-20"
String none = events.higherKey("2026-12-20");   // null
CallWhat it does
map.higherKey(k)Least key > k, or null.
map.ceilingKey(k)Least key ≥ k (keeps an exact match).
map.higherEntry(k)The next key and its value.

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

ParameterDescription
key (K)The key (or any probe value) to move past.

Return Value

Return ValueDescription
KThe least key that compares strictly greater than key.
nullkey is at or past the last key, or the map is empty.

Compare with Similar Methods

Choosing between the "after" lookups:

MethodHow it differs
ceilingKey(k)Returns k itself when it is a key.
lowerKey(k)Moves the other way: the key before k.
higherEntry(k)Next key plus its value as a snapshot.
tailMap(k, false)Every key after k, as a view.

Java Examples

Example 1 — Show the Next Event After Today

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

How It Works

  1. Today is excluded. The concert's date equals the probe, and higherKey only accepts keys strictly greater, so the concert is not "coming up".
  2. The successor. The least date after 2026-12-12 is 2026-12-20, whose value is "Winter break".
  3. 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++;
        }
    }
}

How It Works

  1. Begin at the smallest order. The cursor starts at firstKey(), 101, and every higherKey call advances it to the next order number.
  2. 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.
  3. 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) + ")");
        }
    }
}

How It Works

  1. Advance one desk. Starting from desk 5, the strictly greater key is 9, so Sun gets the first ticket.
  2. Wrap at the top. Desk 9 has no successor, higherKey returns null, and the conditional falls back to firstKey(), desk 2.
  3. 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.

Next: isEmpty()

Check whether the map has no mappings.

isEmpty() 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.

11 people found this page helpful