Home Java TreeMap Methods ceilingEntry() Java TreeMap ceilingEntry() Method Definition In Java, ceilingEntry(key) returns the mapping with the least key greater than or equal to key, or null if there is none. It gives the same position as ceilingKey() , with the value attached.
The entry is an immutable snapshot , so it stays readable even if you remove that key from the map right afterwards. The method comes from NavigableMap (Java 6) and runs in 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 Round a probe up to the next mapping.
TreeMap<Integer, String> cars = new TreeMap<>();
cars.put(5, "Golf");
cars.put(7, "Touran");
Map.Entry<Integer, String> fit = cars.ceilingEntry(6); // 7=Touran Call What it does map.ceilingEntry(k)Entry with the least key ≥ k, or null. map.higherEntry(k)Entry with the least key strictly above k. map.ceilingKey(k)Only the key.
Pair it with floorEntry() to find the closest key on either side of a value; whichever is nearer wins.
Library declarations // java.util.TreeMap<K, V>
public Map.Entry<K, V> ceilingEntry(K key)
// Declared in NavigableMap<K, V> (Java 6) Parameters Parameter Description key (K)The probe value to round up from.
Return Value Return Value Description Map.Entry<K, V>Snapshot of the mapping with the least key ≥ key. nullEvery key is smaller than key, or the map is empty.
Compare with Similar Methods Related entry lookups:
Java Examples 1. Smallest that fits 2. Book and remove 3. Closest on either side Example 1 — Rent the Smallest Car for a Group Keys are seat counts; the ceiling of the group size is the smallest car with enough seats.
import java.util.Map;
import java.util.TreeMap;
public class TreeMapCeilingEntryCars {
public static void main(String[] args) {
TreeMap<Integer, String> cars = new TreeMap<>();
cars.put(2, "Smart");
cars.put(5, "Golf");
cars.put(7, "Touran");
cars.put(9, "Transporter");
int[] groups = {4, 7, 12};
for (int people : groups) {
Map.Entry<Integer, String> car = cars.ceilingEntry(people);
System.out.println(people + " people -> " + (car == null ? "book two cars" : car.getValue() + " (" + car.getKey() + " seats)"));
}
}
} 4 people -> Golf (5 seats)
7 people -> Touran (7 seats)
12 people -> book two cars How It Works Round the group up. Four people need at least 4 seats; the least key at or above 4 is 5, so the Golf is offered rather than the bigger vans.Seven fills a Touran exactly. 7 is a key and counts as its own ceiling.Larger than the fleet. Twelve exceeds the 9-seat maximum, the lookup returns null, and the message suggests two cars.Example 2 — Book the Next Free Meeting Slot Each request takes the first free slot at or after its time; booked slots are removed.
import java.util.Map;
import java.util.TreeMap;
public class TreeMapCeilingEntrySlots {
public static void main(String[] args) {
TreeMap<String, String> free = new TreeMap<>();
free.put("09:00", "Room A");
free.put("10:30", "Room B");
free.put("13:00", "Room A");
String[] requests = {"10:00", "10:15", "13:00", "14:00"};
for (String r : requests) {
Map.Entry<String, String> slot = free.ceilingEntry(r);
if (slot == null) {
System.out.println(r + " -> fully booked");
} else {
free.remove(slot.getKey());
System.out.println(r + " -> " + slot.getKey() + " in " + slot.getValue());
}
}
}
} 10:00 -> 10:30 in Room B
10:15 -> 13:00 in Room A
13:00 -> fully booked
14:00 -> fully booked How It Works Readable after removal. The snapshot for 10:30 is printed after remove deleted that key, which works because the entry is a separate copy.Taken slots disappear. The 10:15 request can no longer get 10:30, so its ceiling is now 13:00 in Room A.Too late for the rest. With 13:00 booked, no key is at or after "13:00" or "14:00", and both requests are told the day is full.Example 3 — Find the Nearest Station on a Line floorEntry and ceilingEntry give the neighbours below and above a position; the closer one wins.
import java.util.Map;
import java.util.TreeMap;
public class TreeMapCeilingEntryNearest {
static String nearest(TreeMap<Integer, String> stations, int km) {
Map.Entry<Integer, String> below = stations.floorEntry(km);
Map.Entry<Integer, String> above = stations.ceilingEntry(km);
if (below == null) {
return above.getValue();
}
if (above == null) {
return below.getValue();
}
return (km - below.getKey() <= above.getKey() - km) ? below.getValue() : above.getValue();
}
public static void main(String[] args) {
TreeMap<Integer, String> stations = new TreeMap<>();
stations.put(0, "Central");
stations.put(12, "Airport");
stations.put(30, "Harbor");
stations.put(47, "Lakeside");
int[] positions = {20, 40, 50};
for (int km : positions) {
System.out.println("km " + km + " -> " + nearest(stations, km));
}
}
} km 20 -> Airport
km 40 -> Lakeside
km 50 -> Lakeside How It Works Two candidates. At km 20 the floor is Airport (8 km back) and the ceiling is Harbor (10 km ahead), so the shorter distance picks Airport.The ceiling can win. At km 40, Harbor is 10 km behind but Lakeside only 7 km ahead, so the ceiling entry is the answer.Past the end of the line. At km 50 there is no ceiling, above is null, and the helper falls back to the floor, Lakeside.Frequently Asked Questions What does TreeMap ceilingEntry() return? The mapping with the least key greater than or equal to the argument, or null if all keys are smaller.
What is the difference between ceilingEntry() and ceilingKey()? ceilingKey() returns only the key; ceilingEntry() returns the key and its value as a snapshot.
Can I still read the entry after removing its key? Yes. The returned entry is an independent snapshot, so getKey() and getValue() keep working.
How do I find the nearest key in a TreeMap? Call floorEntry(x) and ceilingEntry(x), handle a null on either side, and pick the one whose key is closer to x.
Does ceilingEntry() throw for a null key? Yes, with natural ordering on a non-empty map it throws NullPointerException.
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.
7 people found this page helpful
Helpful Share Copy link Suggestion