Java TreeSet tailSet() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
TreeSet

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.

Java
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]
CallWhat 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
// 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

ParameterDescription
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 ValueDescription
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:

MethodHow 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

Example 1 — Filter Honors Scores With Inclusive vs Strict tailSet()

Compare tailSet(90) (inclusive by default) against tailSet(90, false) (strictly above 90).

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

How It Works

  1. Inclusive default. One-argument tailSet(90) keeps 90 itself alongside 94 and 99.
  2. Exclude exact bound. Adding false skips 90 and yields only scores strictly exceeding 90.
  3. 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.

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

How It Works

  1. Main add enters the view. Adding "14:45" to times places it inside the >= "12:00" slice automatically.
  2. View remove deletes from main set. Calling afternoon.remove("16:00") removes "16:00" from times too.
  3. 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.

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

How It Works

  1. Valid write works. 500 is at least 200, so premium.add(500) succeeds.
  2. Lower bound guarded. 150 falls below 200, causing IllegalArgumentException when added through premium.
  3. Nested slice cannot widen bounds. Asking premium for headSet(100) reaches below 200 and throws IllegalArgumentException.

Frequently Asked Questions

Yes. Unlike headSet(), tailSet(fromElement) includes fromElement (>= fromElement). Call tailSet(fromElement, false) to exclude it.
It returns a live view backed by the original TreeSet. Any additions or removals through either side appear in both.
Because y is smaller than the view lower bound x (or equal to x when inclusive was set to false).
It returns false without removing y from the original set and does not throw IllegalArgumentException.
tailSet(x) returns a live SortedSet view of all elements >= x, while ceiling(x) returns only the single smallest element >= x (or null).

Next: toArray()

Copy the sorted elements into a new Object[] or typed array.

toArray() 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.

12 people found this page helpful