Java TreeSet ceiling() Method

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

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.

Java
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)
CallWhat 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
// java.util.TreeSet<E>  (since Java 6)
public E ceiling(E e)

// Declared in NavigableSet<E>

Parameters

ParameterDescription
e (E)The minimum target value to meet or exceed. Must be comparable with set elements.

Return Value

Return ValueDescription
EThe smallest element >= e in the set's ordering.
nullIf the set is empty or every element is strictly less than e.

Compare with Similar Methods

Compare ceiling() with its sibling navigation methods:

MethodHow 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

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.

Java
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

  1. Exact departure caught. Arriving right at "09:45" matches "09:45" because ceiling() allows equality.
  2. Next scheduled bus. At "10:10", 09:45 has already left, so the earliest remaining departure is "11:30".
  3. 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.

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

How It Works

  1. Minimum waste. For 3 people, room 4 is the smallest room satisfying capacity >= 3.
  2. Exact fit reused. A team of 8 fits cleanly into the 8-seat room.
  3. 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.

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

How It Works

  1. Natural order steps up. In [10, 20, 30, 40], the first element at or after 25 is 30.
  2. 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.
  3. Same on descendingSet(). Calling ceiling(25) on a reversed view matches the original set's floor(25).

Frequently Asked Questions

It returns the smallest element in the TreeSet that is greater than or equal to e, or null if no such element exists.
ceiling(e) includes e itself (>= e), whereas higher(e) requires a strictly greater element (> e).
It returns null without throwing an exception.
Because ceiling() follows the TreeSet comparator order, finding the first element at or after e in that ordering.
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

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.

9 people found this page helpful