Java TreeSet Methods

Beginner
6 min read
Updated: Nov 2026
25 method guides
Method index

About This Index

This page is the Java TreeSet Methods index. Each row links to a full guide with syntax, all overloads, parameters, return value, exceptions, three runnable Java 8+ examples with deterministic output, and FAQs.

Learn how TreeSet works on the TreeSet hub. Use this page when you need a specific method such as add(), ceiling(), or subSet().

Always sorted, never duplicated.

A TreeSet prints in ascending sorted order, like [10, 20, 30], determined by natural ordering or a custom Comparator. Adding null with natural ordering throws NullPointerException.

How to Call a TreeSet Method

Every method here is an instance method. Use NavigableSet or TreeSet as the variable type:

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

NavigableSet<Integer> scores = new TreeSet<>();
scores.add(85);
scores.add(70);
scores.add(92);                           // [70, 85, 92]
Integer nextUp = scores.ceiling(80);      // 85
Integer best = scores.last();             // 92
NavigableSet<Integer> top = scores.tailSet(85, true);  // [85, 92]

Java TreeSet Methods Table

All method guides in this Java tutorial series (25 tutorials), grouped by purpose.

Add and remove

7 methods

Insert unique elements in sorted order, add whole collections, pop the smallest or largest element, delete entries, or clone the set.

MethodDescriptionTutorial
add()Insert an element in sorted position; returns false if already present.Try it
addAll()Add every element of another collection in sorted order.Try it
remove()Delete a matching element in O(log n) time and report whether it existed.Try it
clear()Remove every element from the TreeSet (or from a range view).Try it
pollFirst()Remove and return the smallest element, or null if the set is empty.Try it
pollLast()Remove and return the largest element, or null if the set is empty.Try it
clone()Create a shallow copy of the TreeSet with the same comparator.Try it

Look up and size

4 methods

Check membership in O(log n) time, count elements, test for empty, or inspect the Comparator controlling sort order.

MethodDescriptionTutorial
contains()Test in O(log n) time whether a matching element is in the set.Try it
size()Count how many unique elements are stored in the set.Try it
isEmpty()Check whether the TreeSet holds zero elements.Try it
comparator()Return the custom Comparator, or null when using natural order.Try it

First, last and neighbours

6 methods

Read the lowest or highest element, or find the nearest matching threshold at, below, or above a given value.

MethodDescriptionTutorial
first()Read the smallest (lowest) element without removing it; throws if empty.Try it
last()Read the largest (highest) element without removing it; throws if empty.Try it
floor()Find the largest element less than or equal to a target, or null.Try it
ceiling()Find the smallest element greater than or equal to a target, or null.Try it
lower()Find the largest element strictly less than a target, or null.Try it
higher()Find the smallest element strictly greater than a target, or null.Try it

Ranges and reversed views

4 methods

Slice a TreeSet into live, backed views by lower and upper bounds, or reverse the entire view without copying.

MethodDescriptionTutorial
subSet()Get a live view of elements between a lower bound and an upper bound.Try it
headSet()Get a live view of elements smaller than (or up to) a bound.Try it
tailSet()Get a live view of elements greater than or equal to (or after) a bound.Try it
descendingSet()Get a live reversed view of the set from largest to smallest.Try it

Iterate and convert

4 methods

Step through sorted elements ascending or descending, split work with a Java 8 Spliterator, or copy elements into an array.

MethodDescriptionTutorial
iterator()Walk the elements in ascending order and remove safely during the loop.Try it
descendingIterator()Walk the elements one by one from largest to smallest.Try it
spliterator()Get a Java 8 late-binding Spliterator with SORTED and DISTINCT traits.Try it
toArray()Copy the sorted elements into a new Object[] or typed array.Try it

Frequently Asked Questions

They are the methods of java.util.TreeSet, a sorted set backed by a red-black tree. They add and remove unique elements, look up the smallest, largest, or nearest value, slice live range views, iterate ascending or descending, and convert to arrays.
This index lists 25 guides, from add() and addAll() to subSet(), headSet(), tailSet(), and toArray(), grouped into five categories.
Not with natural ordering: adding null throws NullPointerException. If you pass a null-friendly Comparator such as Comparator.nullsFirst(Comparator.naturalOrder()), null is allowed.
TreeSet checks equality using compareTo or the Comparator returning 0, not equals(). For example, with String.CASE_INSENSITIVE_ORDER, adding "java" after "Java" returns false and keeps "Java".
Read the Java TreeSet hub first. Then open add(), floor(), ceiling(), and subSet() from the tables below.

Start with add()

Insert unique elements in sorted order and see what add() returns for duplicates.

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