Home Java TreeSet Methods tailSet() Java TreeSet tailSet() Method Definition In Java, tailSet(fromElement) returns a live view of every element in the TreeSet that is greater than or equal to fromElement.
Notice the difference from headSet(): tailSet(from) includes from by default. Use the two-argument form tailSet(from, false) to get a NavigableSet strictly above from.
An instance method of java.util.TreeSet (Java 1.2+): a red-black tree NavigableSet backed by a TreeMap that keeps unique elements sorted in O(log n) time and is not thread-safe . The one-argument form dates back to Java 1.2 (SortedSet); the two-argument overload with an inclusive flag was added in Java 6 (NavigableSet).
Syntax Pass the lower cutoff; set the second argument to false to exclude the cutoff value itself.
NavigableSet<Integer> scores = new TreeSet<>(Arrays.asList(75, 90, 94, 98));
SortedSet<Integer> atLeast90 = scores.tailSet(90); // [90, 94, 98]
NavigableSet<Integer> above90 = scores.tailSet(90, false); // [94, 98] Call What it does set.tailSet(from)All elements >= from (inclusive by default; SortedSet). set.tailSet(from, false)All elements strictly > from (NavigableSet). set.tailSet(from).first()Same value as set.ceiling(from) when non-empty. set.tailSet(from).clear()Removes every element at or above from.
Because Java slices follow half-open convention [from, to), tailSet(from) includes its start bound while headSet(to) excludes its end bound.
Library declarations // java.util.TreeSet<E>
public SortedSet<E> tailSet(E fromElement)
public NavigableSet<E> tailSet(E fromElement, boolean inclusive)
// Declared in SortedSet<E> and NavigableSet<E> Parameters Parameter Description fromElement (E)Lower bound for elements included in the view. inclusive (boolean)Two-argument overload: true to include fromElement, false to exclude it.
Return Value Return Value Description SortedSet<E> / NavigableSet<E>A live view backed by the original TreeSet.
Compare with Similar Methods Ways to inspect or slice the upper tail of a TreeSet:
Method How it differs headSet(to)Slices from the start up to an upper bound (exclusive by default). subSet(a, b)Slices between both a lower and an upper bound. ceiling(from)Finds only the single lowest element >= from. higher(from)Finds only the single lowest element strictly > from.
Java Examples 1. Inclusive vs exclusive lower bound 2. Two-way live sync 3. Lower bound violation Example 1 — Filter Honors Scores With Inclusive vs Strict tailSet() Compare tailSet(90) (inclusive by default) against tailSet(90, false) (strictly above 90).
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.SortedSet;
import java.util.TreeSet;
public class TreeSetTailSetHonors {
public static void main(String[] args) {
NavigableSet<Integer> scores = new TreeSet<>(Arrays.asList(68, 82, 90, 94, 99));
SortedSet<Integer> honors = scores.tailSet(90);
NavigableSet<Integer> strictlyAbove = scores.tailSet(90, false);
System.out.println("honors (>= 90): " + honors);
System.out.println("strictly > 90: " + strictlyAbove);
}
} honors (>= 90): [90, 94, 99]
strictly > 90: [94, 99] How It Works Inclusive default. One-argument tailSet(90) keeps 90 itself alongside 94 and 99.Exclude exact bound. Adding false skips 90 and yields only scores strictly exceeding 90.Halves split cleanly. Notice headSet(90) and tailSet(90) partition the set with zero overlap and no missing element.Example 2 — Changes Write Both Ways Between TreeSet and tailSet() Add a late appointment through the original set, remove one through tailSet(), and confirm both stay synchronized.
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeSet;
public class TreeSetTailSetSync {
public static void main(String[] args) {
NavigableSet<String> times = new TreeSet<>(Arrays.asList("09:00", "11:00", "13:30", "16:00"));
NavigableSet<String> afternoon = times.tailSet("12:00", true);
System.out.println("initial afternoon: " + afternoon);
times.add("14:45");
System.out.println("after main add: " + afternoon);
afternoon.remove("16:00");
System.out.println("after view remove: " + times);
}
} initial afternoon: [13:30, 16:00]
after main add: [13:30, 14:45, 16:00]
after view remove: [09:00, 11:00, 13:30, 14:45] How It Works Main add enters the view. Adding "14:45" to times places it inside the >= "12:00" slice automatically.View remove deletes from main set. Calling afternoon.remove("16:00") removes "16:00" from times too.Shared backing tree. No extra set was copied; both variables read and modify the exact same tree nodes.Example 3 — Enforcing Lower Bounds on Writes and Sub-Slices Add a valid value above the bound, then try adding below the bound and slicing past it.
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeSet;
public class TreeSetTailSetBounds {
public static void main(String[] args) {
NavigableSet<Integer> bids = new TreeSet<>(Arrays.asList(100, 250, 400));
NavigableSet<Integer> premium = bids.tailSet(200, true);
premium.add(500);
System.out.println("premium after add 500: " + premium);
try {
premium.add(150);
} catch (IllegalArgumentException e) {
System.out.println("add 150 rejected: " + e.getClass().getSimpleName());
}
try {
premium.headSet(100);
} catch (IllegalArgumentException e) {
System.out.println("headSet(100) rejected: " + e.getClass().getSimpleName());
}
}
} premium after add 500: [250, 400, 500]
add 150 rejected: IllegalArgumentException
headSet(100) rejected: IllegalArgumentException How It Works Valid write works. 500 is at least 200, so premium.add(500) succeeds.Lower bound guarded. 150 falls below 200, causing IllegalArgumentException when added through premium.Nested slice cannot widen bounds. Asking premium for headSet(100) reaches below 200 and throws IllegalArgumentException.Frequently Asked Questions Is TreeSet tailSet(fromElement) inclusive by default? Yes. Unlike headSet(), tailSet(fromElement) includes fromElement (>= fromElement). Call tailSet(fromElement, false) to exclude it.
Does tailSet() return a copy or a live view? It returns a live view backed by the original TreeSet. Any additions or removals through either side appear in both.
Why did tailSet(x).add(y) throw IllegalArgumentException? Because y is smaller than the view lower bound x (or equal to x when inclusive was set to false).
What does tailSet(x).remove(y) do if y is smaller than x? It returns false without removing y from the original set and does not throw IllegalArgumentException.
What is the difference between tailSet(x) and ceiling(x)? tailSet(x) returns a live SortedSet view of all elements >= x, while ceiling(x) returns only the single smallest element >= x (or 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.
12 people found this page helpful
Helpful Share Copy link Suggestion