Home Java TreeSet Methods ceiling() Java TreeSet ceiling() Method Definition In Java, ceiling(e) returns the least (smallest) element in the set that is greater than or equal to e , or null if every element in the set is smaller than e.
Think of it as stepping up to the earliest element that still meets or exceeds your target: if e is already present, ceiling(e) returns e; otherwise it returns the next element above it.
An instance method of java.util.TreeSet (Java 6+ (added via NavigableSet in JDK 1.6)): a red-black tree NavigableSet backed by a TreeMap that keeps unique elements sorted in O(log n) time and is not thread-safe .
Syntax Pass the minimum threshold you want to meet.
NavigableSet<Integer> rooms = new TreeSet<>(Arrays.asList(4, 8, 12, 20));
Integer exact = rooms.ceiling(8); // 8 (exact capacity match)
Integer fit = rooms.ceiling(9); // 12 (smallest room >= 9)
Integer tooBig = rooms.ceiling(25); // null (none >= 25) Call What it does set.ceiling(e)Least element >= e, or null. set.higher(e)Strictly greater (> e); skips e even when present. set.floor(e)Opposite direction: greatest element <= e. set.tailSet(e, true)View of all elements >= e, starting with ceiling(e).
On a TreeSet built with a custom descending comparator, "greater than or equal" means after or equal in that comparator's order , not mathematically larger.
Library declarations // java.util.TreeSet<E> (since Java 6)
public E ceiling(E e)
// Declared in NavigableSet<E> Parameters Parameter Description e (E)The minimum target value to meet or exceed. Must be comparable with set elements.
Return Value Return Value Description EThe smallest element >= e in the set's ordering. nullIf the set is empty or every element is strictly less than e.
Important
Be careful not to unbox directly into int if ceiling() can return null, as that throws NullPointerException. Passing null under natural ordering throws NullPointerException when elements exist, and incompatible types throw ClassCastException.
Compare with Similar Methods Compare ceiling() with its sibling navigation methods:
Method How it differs higher(e)Strictly greater (> e): skips exact matches. floor(e)Opposite direction (<= e): steps down instead of up. lower(e)Strictly below (< e). tailSet(e, true)Returns all matching elements from ceiling(e) onward.
Java Examples 1. Bus departure schedule 2. Room capacity fit 3. Comparator flips ceiling Example 1 — Find the Next Bus Departure at or After Arrival Time Look up the earliest 24-hour departure time at or after a passenger reaches the bus stop.
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeSet;
public class TreeSetCeilingBus {
public static void main(String[] args) {
NavigableSet<String> buses = new TreeSet<>(Arrays.asList("08:15", "09:45", "11:30", "14:00"));
System.out.println("arrive 09:45 -> bus " + buses.ceiling("09:45"));
System.out.println("arrive 10:10 -> bus " + buses.ceiling("10:10"));
System.out.println("arrive 15:00 -> bus " + buses.ceiling("15:00"));
}
} How It Works Exact departure caught. Arriving right at "09:45" matches "09:45" because ceiling() allows equality.Next scheduled bus. At "10:10", 09:45 has already left, so the earliest remaining departure is "11:30".After last bus. No time in the timetable is >= "15:00", so ceiling() returns null.Example 2 — Pick the Smallest Meeting Room That Fits a Team Given sorted room capacities, pick the smallest room large enough for each group size.
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeSet;
public class TreeSetCeilingRooms {
public static void main(String[] args) {
NavigableSet<Integer> capacities = new TreeSet<>(Arrays.asList(4, 8, 12, 20));
int[] teams = {3, 8, 15, 25};
for (int size : teams) {
Integer chosen = capacities.ceiling(size);
System.out.println("team of " + size + " -> room " + (chosen == null ? "too large" : chosen));
}
}
} team of 3 -> room 4
team of 8 -> room 8
team of 15 -> room 20
team of 25 -> room too large How It Works Minimum waste. For 3 people, room 4 is the smallest room satisfying capacity >= 3.Exact fit reused. A team of 8 fits cleanly into the 8-seat room.Null fallback for overflow. A 25-person team exceeds all rooms, so ceiling(25) returns null and prints "too large".Example 3 — How reverseOrder() Changes ceiling() Behaviour Compare natural ceiling(25) against a descending TreeSet where "greater" means further toward 0.
import java.util.Arrays;
import java.util.Comparator;
import java.util.NavigableSet;
import java.util.TreeSet;
public class TreeSetCeilingReversed {
public static void main(String[] args) {
NavigableSet<Integer> asc = new TreeSet<>(Arrays.asList(10, 20, 30, 40));
NavigableSet<Integer> desc = new TreeSet<>(Comparator.reverseOrder());
desc.addAll(asc);
System.out.println("ascending ceiling(25): " + asc.ceiling(25));
System.out.println("descending ceiling(25): " + desc.ceiling(25));
System.out.println("asc.descendingSet().ceiling(25): " + asc.descendingSet().ceiling(25));
}
} ascending ceiling(25): 30
descending ceiling(25): 20
asc.descendingSet().ceiling(25): 20 How It Works Natural order steps up. In [10, 20, 30, 40], the first element at or after 25 is 30.Reversed comparator flips meaning. In [40, 30, 20, 10], 20 is the first element appearing at or after 25, so desc.ceiling(25) returns 20.Same on descendingSet(). Calling ceiling(25) on a reversed view matches the original set's floor(25).Frequently Asked Questions What does TreeSet ceiling(e) return? It returns the smallest element in the TreeSet that is greater than or equal to e, or null if no such element exists.
What is the difference between ceiling() and higher()? ceiling(e) includes e itself (>= e), whereas higher(e) requires a strictly greater element (> e).
What does ceiling() return if every element is smaller than e? It returns null without throwing an exception.
Why did ceiling() give a smaller number when using Comparator.reverseOrder()? Because ceiling() follows the TreeSet comparator order, finding the first element at or after e in that ordering.
Which Java version added TreeSet ceiling()? Java 6 (JDK 1.6), through the NavigableSet interface.
Next: clear() Remove every element from the TreeSet (or from a range view).
clear() tutorial → 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.
9 people found this page helpful
Helpful Share Copy link Suggestion