Java TreeMap higherEntry() Method

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

Definition

In Java, higherEntry(key) returns the mapping with the least key strictly greater than key, or null when nothing comes after it. An exact match is skipped, so you always move forward.

The returned entry is a read-only snapshot. The method was added in Java 6 with NavigableMap 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 fetch the next mapping.

Java
TreeMap<Integer, Integer> levels = new TreeMap<>();
levels.put(100, 2);
levels.put(250, 3);
Map.Entry<Integer, Integer> next = levels.higherEntry(100);   // 250=3
Map.Entry<Integer, Integer> none = levels.higherEntry(250);   // null
CallWhat it does
map.higherEntry(k)Entry with the least key > k, or null.
map.ceilingEntry(k)Includes k's own entry.
map.higherKey(k)Only the next key.

The key of the next entry minus one is the last value of the current band, which is how the third example below prints readable ranges such as 30-119.

Library declarations
Java
// java.util.TreeMap<K, V>
public Map.Entry<K, V> higherEntry(K key)

// Declared in NavigableMap<K, V> (Java 6)

Parameters

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

Return Value

Return ValueDescription
Map.Entry<K, V>Snapshot of the mapping with the least key > key.
nullNo key is greater than key, or the map is empty.

Compare with Similar Methods

Choosing an "after" lookup:

MethodHow it differs
ceilingEntry(k)Keeps an exact match.
lowerEntry(k)The entry before k.
higherKey(k)Key only.
tailMap(k, false)Every entry after k.

Java Examples

Example 1 — XP Needed for the Next Level

Keys are the XP needed to reach each level; the next level is the first threshold strictly above the player's XP.

Java
import java.util.Map;
import java.util.TreeMap;

public class TreeMapHigherEntryXp {
    public static void main(String[] args) {
        TreeMap<Integer, Integer> levels = new TreeMap<>();
        levels.put(0, 1);
        levels.put(100, 2);
        levels.put(250, 3);
        levels.put(500, 4);

        int[] players = {180, 250, 640};
        for (int xp : players) {
            Map.Entry<Integer, Integer> next = levels.higherEntry(xp);
            if (next == null) {
                System.out.println(xp + " XP: max level reached");
            } else {
                System.out.println(xp + " XP: " + (next.getKey() - xp) + " more for level " + next.getValue());
            }
        }
    }
}

How It Works

  1. Distance to the next threshold. At 180 XP the next key is 250 (level 3), so the player needs 70 more points.
  2. Already on a threshold. A player with exactly 250 XP has reached level 3; the strict lookup skips 250 and points at level 4, 250 points away.
  3. Beyond the table. 640 is above every key, higherEntry returns null, and the player is told they are at the top.

Example 2 — Now Playing and Up Next

ceilingEntry(3) is the current track itself; higherEntry(3) is the one after it.

Java
import java.util.TreeMap;

public class TreeMapHigherEntryPlaylist {
    public static void main(String[] args) {
        TreeMap<Integer, String> tracks = new TreeMap<>();
        tracks.put(1, "Sunrise");
        tracks.put(2, "Paper Boats");
        tracks.put(3, "Blue Hour");
        tracks.put(4, "Night Drive");
        tracks.put(6, "Echoes");

        System.out.println("now playing: " + tracks.ceilingEntry(3));
        System.out.println("up next: " + tracks.higherEntry(3));

        tracks.remove(4);
        System.out.println("up next after removing 4: " + tracks.higherEntry(3));
    }
}

How It Works

  1. Ceiling stays put. Track 3 is a key, so ceilingEntry(3) returns it unchanged: that is what is playing now.
  2. Higher moves on. The strict version skips 3 and returns track 4, the natural "up next" item.
  3. Gaps are jumped. With track 4 deleted and no track 5, the next key after 3 is 6, so "Echoes" moves up without any renumbering.

Example 3 — Print a Parking Fee Table as Ranges

Each key starts a band; the next entry's key tells where the band ends.

Java
import java.util.Map;
import java.util.TreeMap;

public class TreeMapHigherEntryRanges {
    public static void main(String[] args) {
        TreeMap<Integer, String> fees = new TreeMap<>();
        fees.put(0, "free");
        fees.put(30, "2 EUR");
        fees.put(120, "5 EUR");
        fees.put(480, "12 EUR");

        for (Map.Entry<Integer, String> e : fees.entrySet()) {
            Map.Entry<Integer, String> next = fees.higherEntry(e.getKey());
            String range = (next == null) ? e.getKey() + "+" : e.getKey() + "-" + (next.getKey() - 1);
            System.out.println(range + " min: " + e.getValue());
        }
    }
}

How It Works

  1. Band end from the next start. After key 0 comes 30, so the free band covers 0 to 29 minutes.
  2. One lookup per row. Each higherEntry(e.getKey()) fetches the following band while the loop reads the current one, building 30-119 and 120-479.
  3. The open-ended top band. 480 has no higher entry, so the conditional prints 480+ instead of an upper limit.

Frequently Asked Questions

The mapping with the least key strictly greater than the argument, or null if no key is larger.
ceilingEntry() returns the argument's own entry when it exists; higherEntry() always returns the next one.
null, since no key is greater than the highest one.
No. It is a snapshot; it does not follow later changes and setValue throws UnsupportedOperationException.
Start with firstEntry() and repeat e = map.higherEntry(e.getKey()) until e is null. For a plain pass, iterating entrySet() is simpler and faster.

Next: higherKey()

Find the smallest key strictly greater than a value.

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

10 people found this page helpful