Java TreeSet
In this page:
Introduction to TreeSet
TreeSet is an implementation of the Set interface that stores its elements in a red-black tree, a self-balancing binary search tree, which keeps every element automatically arranged in sorted order at all times.
Example: Introduction to TreeSet
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>(); // red-black tree
set.add(5);
set.add(1);
System.out.println(set);
}
}
Login to try C/C++/Java/PHP code in the editor
Automatic Sorting
Elements added to a TreeSet are sorted according to their natural ordering by default -- numbers sort numerically and strings sort alphabetically -- regardless of the order they happened to be inserted in.
Example: Automatic Sorting
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>();
set.add(3);
set.add(1);
set.add(2);
System.out.println(set); // sorted: 1, 2, 3
}
}
Login to try C/C++/Java/PHP code in the editor
Adding and Removing Elements
TreeSet supports the same add and remove operations as any other Set, but every insertion or removal also re-balances the underlying tree to keep the sorted order intact, and duplicate values are still rejected.
Example: Adding and Removing Elements
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>();
set.add(5);
set.add(5); // duplicate ignored
set.remove(5);
System.out.println(set);
}
}
Login to try C/C++/Java/PHP code in the editor
Navigation Methods
TreeSet adds navigation methods beyond a plain Set, like first, last, higher, and lower, which take advantage of the guaranteed sort order to efficiently find the smallest, largest, or nearest elements relative to a given value.
Example: Navigation Methods
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>();
set.add(1); set.add(5); set.add(10);
System.out.println(set.first());
System.out.println(set.last());
System.out.println(set.higher(5));
}
}
Login to try C/C++/Java/PHP code in the editor
Custom Sort Order with Comparator
Passing a Comparator to a TreeSet's constructor overrides the default natural ordering, letting elements be sorted by any custom rule, such as reverse order or by a computed property like string length instead of alphabetical order.
Example: Custom Sort Order with Comparator
import java.util.TreeSet;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>(Collections.reverseOrder());
set.add(1);
set.add(5);
System.out.println(set); // reverse sorted
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting