Java TreeMap ceilingEntry() Method

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

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.

Java
TreeMap<Integer, String> cars = new TreeMap<>();
cars.put(5, "Golf");
cars.put(7, "Touran");
Map.Entry<Integer, String> fit = cars.ceilingEntry(6);   // 7=Touran
CallWhat 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
// java.util.TreeMap<K, V>
public Map.Entry<K, V> ceilingEntry(K key)

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

Parameters

ParameterDescription
key (K)The probe value to round up from.

Return Value

Return ValueDescription
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:

MethodHow it differs
floorEntry(k)Rounds down instead.
higherEntry(k)Never returns k's own entry.
ceilingKey(k)Same key without the value.
tailMap(k)All entries from k upward.

Java Examples

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.

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

How It Works

  1. 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.
  2. Seven fills a Touran exactly. 7 is a key and counts as its own ceiling.
  3. 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.

Java
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());
            }
        }
    }
}

How It Works

  1. Readable after removal. The snapshot for 10:30 is printed after remove deleted that key, which works because the entry is a separate copy.
  2. Taken slots disappear. The 10:15 request can no longer get 10:30, so its ceiling is now 13:00 in Room A.
  3. 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.

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

How It Works

  1. 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.
  2. 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.
  3. 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

The mapping with the least key greater than or equal to the argument, or null if all keys are smaller.
ceilingKey() returns only the key; ceilingEntry() returns the key and its value as a snapshot.
Yes. The returned entry is an independent snapshot, so getKey() and getValue() keep working.
Call floorEntry(x) and ceilingEntry(x), handle a null on either side, and pick the one whose key is closer to x.
Yes, with natural ordering on a non-empty map it throws NullPointerException.

Next: ceilingKey()

Find the smallest key that is greater than or equal to a value.

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

7 people found this page helpful