Java TreeSet Methods

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().
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:
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 methodsInsert unique elements in sorted order, add whole collections, pop the smallest or largest element, delete entries, or clone the set.
| Method | Description | Tutorial |
|---|---|---|
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 methodsCheck membership in O(log n) time, count elements, test for empty, or inspect the Comparator controlling sort order.
| Method | Description | Tutorial |
|---|---|---|
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 methodsRead the lowest or highest element, or find the nearest matching threshold at, below, or above a given value.
| Method | Description | Tutorial |
|---|---|---|
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 methodsSlice a TreeSet into live, backed views by lower and upper bounds, or reverse the entire view without copying.
| Method | Description | Tutorial |
|---|---|---|
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 methodsStep through sorted elements ascending or descending, split work with a Java 8 Spliterator, or copy elements into an array.
| Method | Description | Tutorial |
|---|---|---|
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
Start with add()
Insert unique elements in sorted order and see what add() returns for duplicates.
12 people found this page helpful
