Java TreeSet higher() Method

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

Definition

In Java, higher(e) returns the least (smallest) element strictly greater than e, or null if no element in the TreeSet exceeds e.

It acts as a strict successor lookup: even if e exists in the set, higher(e) skips past e to return the next larger element in O(log n) time.

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 a cutoff or current item to find the immediate next larger element.

Java
NavigableSet<Integer> seats = new TreeSet<>(Arrays.asList(101, 104, 109, 115));
Integer nextSeat = seats.higher(104);   // 109 (strictly > 104)
Integer none = seats.higher(115);       // null (nothing > 115)
CallWhat it does
set.higher(e)Least element strictly > e, or null.
set.ceiling(e)Least element >= e (includes e if present).
set.lower(e)Opposite direction: greatest element strictly < e.
set.tailSet(e, false)Live view of every element strictly greater than e.

For pagination cursors or stepping forward from a known ID, higher(lastSeenId) finds the next record without repeating lastSeenId.

Library declarations
Java
// java.util.TreeSet<E>  (since Java 6)
public E higher(E e)

// Declared in NavigableSet<E>

Parameters

ParameterDescription
e (E)The exclusive lower reference value.

Return Value

Return ValueDescription
EThe smallest element strictly > e.
nullIf the set is empty or e is greater than or equal to the last element.

Compare with Similar Methods

Compare higher() with related lookup methods:

MethodHow it differs
ceiling(e)Includes e itself on an exact match (>= e).
lower(e)Steps backward instead of forward (strictly < e).
floor(e)Steps backward allowing equality (<= e).
tailSet(e, false)Live slice of all elements strictly greater than e.

Java Examples

Example 1 — Jump to the Next Available Seat After the Current One

Advance through sorted available seat numbers using higher() without revisiting the current seat.

Java
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeSet;

public class TreeSetHigherSeats {
    public static void main(String[] args) {
        NavigableSet<Integer> openSeats = new TreeSet<>(Arrays.asList(12, 15, 21, 24));

        int current = 15;
        Integer next = openSeats.higher(current);
        Integer afterLast = openSeats.higher(24);

        System.out.println("after seat 15: " + next);
        System.out.println("after seat 24: " + afterLast);
    }
}

How It Works

  1. Strict successor. Seat 15 is present in openSeats, so higher(15) skips it and picks the next open seat, 21.
  2. Stops at the top. Seat 24 is the highest seat in the set, so asking for a seat strictly above 24 returns null.
  3. O(log n) cursor step. No full traversal is needed to step forward to the next seat.

Example 2 — Upgrade to the Next Shipping Tier Strictly Above 500 Grams

Find the next parcel bracket strictly heavier than a cutoff, contrasting higher() with ceiling().

Java
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeSet;

public class TreeSetHigherBrackets {
    public static void main(String[] args) {
        NavigableSet<Integer> brackets = new TreeSet<>(Arrays.asList(250, 500, 1000, 2000));

        System.out.println("ceiling(500): " + brackets.ceiling(500));
        System.out.println("higher(500):  " + brackets.higher(500));
        System.out.println("higher(501):  " + brackets.higher(501));
    }
}

How It Works

  1. Ceiling accepts exact hit. 500 is in the set, so ceiling(500) returns 500.
  2. Higher forces an upgrade. higher(500) requires a bracket strictly over 500, returning 1000.
  3. Between brackets behaves identically. For 501 (not in the set), both ceiling(501) and higher(501) return 1000.

Example 3 — Iterate Sorted Batch Keys Using a Stateless higher() Cursor

Step through sorted product codes starting above "A" using higher() so the set can safely change between steps.

Java
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeSet;

public class TreeSetHigherCursor {
    public static void main(String[] args) {
        NavigableSet<String> codes = new TreeSet<>(Arrays.asList("A10", "B05", "C12", "D08"));

        String cursor = "A";
        while ((cursor = codes.higher(cursor)) != null) {
            System.out.println("code: " + cursor);
            if ("B05".equals(cursor)) {
                codes.add("C99");
            }
        }
        System.out.println("final codes: " + codes);
    }
}

How It Works

  1. Start before "A10". Because "A" < "A10", the first higher("A") finds "A10".
  2. Live modification safe. Adding "C99" mid-loop does not throw ConcurrentModificationException because higher() runs fresh lookups instead of using a fail-fast iterator.
  3. New item picked up in order. After "C12", higher("C12") naturally finds "C99" before "D08".

Frequently Asked Questions

The smallest element in the TreeSet that is strictly greater than e (> e), or null if no larger element exists.
higher(e) requires strictly greater (> e) and skips e even if it is present. ceiling(e) is greater than or equal (>= e).
null, because no element in the TreeSet is strictly greater than last().
No. It returns null.
Yes, if you pass null to a TreeSet that uses natural ordering and contains elements (or a comparator that rejects null).

Next: isEmpty()

Check whether the TreeSet holds zero elements.

isEmpty() 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