JavaTreeSet
Java TreeSet
ATreeSet is a collection that stores unique elementsin sorted order.
It is part of thejava.util package and implements theSet interface.
Tip: UnlikeHashSet, which has no order,TreeSet keeps its elements sorted automatically.
Create a TreeSet
Example
Create aTreeSet object calledcars that will store strings:
import java.util.TreeSet; // Import the TreeSet classTreeSet<String> cars = new TreeSet<>();Now you can use methods likeadd(),contains(), andremove() to manage your sorted set of elements.
Add Elements
To add elements to aTreeSet, use theadd() method:
Example
import java.util.TreeSet;public class Main { public static void main(String[] args) { TreeSet<String> cars = new TreeSet<>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("BMW"); // Duplicate cars.add("Mazda"); System.out.println(cars); }}Output: The elements will be sorted automatically (e.g., [BMW, Ford, Mazda, Volvo]).
Note: Duplicates like "BMW" will only appear once.
Check if an Element Exists
Usecontains() to check if an element exists:
Remove an Element
Useremove() to remove an element:
Remove All Elements
Useclear() to remove all elements:
TreeSet Size
Usesize() to count how many unique elements are in the set:
Note: Duplicate values are not counted - only unique elements are included in the size.
Loop Through a TreeSet
Loop through the elements of aTreeSet with afor-each loop:
Example
TreeSet<String> cars = new TreeSet<>();// add elements...for (String i : cars) { System.out.println(i);}Using TreeSet with Numbers
TreeSet also works with numbers and sorts them from smallest to largest:
Example
import java.util.TreeSet;public class Main { public static void main(String[] args) { TreeSet<Integer> numbers = new TreeSet<>(); numbers.add(40); numbers.add(10); numbers.add(30); numbers.add(20); for (int n : numbers) { System.out.println(n); } }}Output: The numbers will be printed in sorted order (10, 20, 30, 40).
HashSet vs TreeSet
| Feature | HashSet | TreeSet |
|---|---|---|
| Order | No guaranteed order | Sorted (natural order) |
| Duplicates | Not allowed | Not allowed |
| Performance | Faster (no sorting) | Slower (due to sorting) |
Tip: UseHashSet when you care about speed, andTreeSet when you need sorted elements.
The var Keyword
From Java 10, you can use thevar keyword to declare aTreeSet variable without writing the type twice. The compiler figures out the type from the value you assign.
This makes code shorter,but many developers still use the full type for clarity. Sincevar is valid Java, you may see it in other code, so it's good to know that it exists:
Example
// Without varTreeSet<String> cars = new TreeSet<String>();// With varvar cars = new TreeSet<String>();The Set Interface
Note: Sometimes you will see bothSet andTreeSet in Java code, like this:
import java.util.Set;import java.util.TreeSet;Set<String> cars = new TreeSet<>();This means the variable (cars) is declared as aSet (the interface), but it stores aTreeSet object (the actual set). SinceTreeSet implements theSet interface, this is possible.
It works the same way, but some developers prefer this style because it gives them more flexibility to change the type later.

