Home Java TreeSet Methods higher() Java TreeSet higher() Method 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.
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) Call What 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.util.TreeSet<E> (since Java 6)
public E higher(E e)
// Declared in NavigableSet<E> Parameters Parameter Description e (E)The exclusive lower reference value.
Return Value Return Value Description 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:
Method How 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 1. Next available seat 2. Shipping weight upgrade 3. Cursor loop with higher 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.
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);
}
} after seat 15: 21
after seat 24: null How It Works Strict successor. Seat 15 is present in openSeats, so higher(15) skips it and picks the next open seat, 21.Stops at the top. Seat 24 is the highest seat in the set, so asking for a seat strictly above 24 returns null.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().
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));
}
} ceiling(500): 500
higher(500): 1000
higher(501): 1000 How It Works Ceiling accepts exact hit. 500 is in the set, so ceiling(500) returns 500.Higher forces an upgrade. higher(500) requires a bracket strictly over 500, returning 1000.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.
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);
}
} code: A10
code: B05
code: C12
code: C99
code: D08
final codes: [A10, B05, C12, C99, D08] How It Works Start before "A10". Because "A" < "A10", the first higher("A") finds "A10".Live modification safe. Adding "C99" mid-loop does not throw ConcurrentModificationException because higher() runs fresh lookups instead of using a fail-fast iterator.New item picked up in order. After "C12", higher("C12") naturally finds "C99" before "D08".Frequently Asked Questions What does TreeSet higher(e) return? The smallest element in the TreeSet that is strictly greater than e (> e), or null if no larger element exists.
What is the difference between higher() and ceiling()? higher(e) requires strictly greater (> e) and skips e even if it is present. ceiling(e) is greater than or equal (>= e).
What does higher() return when passed the largest element in the set? null, because no element in the TreeSet is strictly greater than last().
Does higher() throw an exception on an empty TreeSet? No. It returns null.
Can higher() throw NullPointerException? Yes, if you pass null to a TreeSet that uses natural ordering and contains elements (or a comparator that rejects null).
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