Home Java TreeMap Methods higherEntry() Java TreeMap higherEntry() Method 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.
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 Call What 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.util.TreeMap<K, V>
public Map.Entry<K, V> higherEntry(K key)
// Declared in NavigableMap<K, V> (Java 6) Parameters Parameter Description key (K)The key (or probe value) to move past.
Return Value Return Value Description 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:
Java Examples 1. Next milestone 2. Strict vs ceiling 3. Ranges from neighbours 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.
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());
}
}
}
} 180 XP: 70 more for level 3
250 XP: 250 more for level 4
640 XP: max level reached How It Works Distance to the next threshold. At 180 XP the next key is 250 (level 3), so the player needs 70 more points.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.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.
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));
}
} now playing: 3=Blue Hour
up next: 4=Night Drive
up next after removing 4: 6=Echoes How It Works Ceiling stays put. Track 3 is a key, so ceilingEntry(3) returns it unchanged: that is what is playing now.Higher moves on. The strict version skips 3 and returns track 4, the natural "up next" item.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.
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());
}
}
} 0-29 min: free
30-119 min: 2 EUR
120-479 min: 5 EUR
480+ min: 12 EUR How It Works Band end from the next start. After key 0 comes 30, so the free band covers 0 to 29 minutes.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.The open-ended top band. 480 has no higher entry, so the conditional prints 480+ instead of an upper limit.Frequently Asked Questions What does TreeMap higherEntry() return? The mapping with the least key strictly greater than the argument, or null if no key is larger.
What is the difference between higherEntry() and ceilingEntry()? ceilingEntry() returns the argument's own entry when it exists; higherEntry() always returns the next one.
What does higherEntry() return for the last key? null, since no key is greater than the highest one.
Is the entry from higherEntry() live? No. It is a snapshot; it does not follow later changes and setValue throws UnsupportedOperationException.
How do I loop forward with higherEntry()? Start with firstEntry() and repeat e = map.higherEntry(e.getKey()) until e is null. For a plain pass, iterating entrySet() is simpler and faster.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion