What is TreeSet in Java?
- In Java, a TreeSet is a collection that executes the Set interface and is based on a NavigableMap (ordinarily a Red-Black tree).
- It is portion of the java.util package and is utilized to store a special set of components in a sorted order.
- The TreeSet gives a way to preserve components in their characteristic arrange (or a custom arrange in case a Comparator is provided).
Key Highlights of TreeSet in Java:

1.Unique Elements:
- Like several other execution of the Set interface, a TreeSet does not permit duplicate components.
2.Sorted Order:
- By default, the components are requested concurring to their common order (ascending order for numbers, alphabetical order for strings).
- You’ll be able too give a custom sorting order using a Comparator.
3.No Null Elements:
- A TreeSet does not permit invalid components, not at all like a few other Set executions.
4.Logarithmic Time Complexity:
- Most operations like add(), remove(), and contains() take O(log n) time due to the fundamental Red-Black tree structure.
5.NavigableSet Interface:
- Since TreeSet executes the NavigableSet interface, it gives extra methods for navigation like first(), last(), lower(), higher(), etc.
Types of TreeSet in Java
There are basically two types of TreeSet in Java, based on how the components are compared and ordered:
1.TreeSet with Natural Ordering:
- This is often the default type of TreeSet.
- The components are ordered in their normal order (i.e., ascending order for comparable types such as numbers or strings).
2.TreeSet with Custom Ordering:
- In this type, you’ll give a custom Comparator to characterize how components ought to be ordered within the set.
- The comparator can permit for descending order, case-insensitive order, or any custom logic.
Examples of TreeSet in Java
Let’s explore a few practical examples to superior get it how a TreeSet works.
Example 1: TreeSet with Natural Ordering
import java.util.*;
public class TreeSetExample {
public static void main(String[] args) {
// Creating a TreeSet with integers
TreeSet<Integer> treeSet = new TreeSet<>();
// Adding elements to the TreeSet
treeSet.add(10);
treeSet.add(20);
treeSet.add(15);
treeSet.add(30);
// Displaying the elements of the TreeSet
System.out.println(“TreeSet (Natural Order): ” + treeSet);
// TreeSet will automatically sort the elements
// Output: [10, 15, 20, 30]
}
}
In this example, the integers are automatically ordered in ascending order, as TreeSet uses the common ordering of the Integer class.
Example 2: TreeSet with Custom Ordering Utilizing Comparator
import java.util.*;
public class CustomTreeSetExample {
public static void main(String[] args) {
// Creating a TreeSet with a custom comparator for descending order
TreeSet<Integer> treeSet = new TreeSet<>(Collections.reverseOrder());
// Adding elements to the TreeSet
treeSet.add(10);
treeSet.add(20);
treeSet.add(15);
treeSet.add(30);
// Displaying the elements of the TreeSet
System.out.println(“TreeSet (Custom Order – Descending): ” + treeSet);
// Output: [30, 20, 15, 10]
}
}
Here, a custom comparator (Collections.reverseOrder()) is utilized to order the components in descending order. The TreeSet stores the components in reverse order rather than the default ascending arrange.
Example 3: Exploring through TreeSet
- The TreeSet gives a few valuable methods for exploring through the set, such as first(), last(), lower(), and higher().
import java.util.*;
public class TreeSetNavigationExample {
public static void main(String[] args) {
// Creating a TreeSet with integers
TreeSet<Integer> treeSet = new TreeSet<>();
// Adding elements to the TreeSet
treeSet.add(10);
treeSet.add(20);
treeSet.add(15);
treeSet.add(30);
// Displaying the first and last elements
System.out.println(“First element: ” + treeSet.first());
System.out.println(“Last element: ” + treeSet.last());
// Using ‘lower’ and ‘higher’ methods
System.out.println(“Element lower than 20: ” + treeSet.lower(20));
System.out.println(“Element higher than 15: ” + treeSet.higher(15));
// Output:
// First element: 10
// Last element: 30
// Element lower than 20: 15
// Element higher than 15: 20
}
}
Example 4: TreeSet with Strings
import java.util.*;
public class StringTreeSetExample {
public static void main(String[] args) {
// Creating a TreeSet with strings
TreeSet<String> treeSet = new TreeSet<>();
// Adding elements to the TreeSet
treeSet.add(“Apple”);
treeSet.add(“Banana”);
treeSet.add(“Cherry”);
treeSet.add(“Date”);
// Displaying the elements of the TreeSet
System.out.println(“TreeSet (String elements): ” + treeSet);
// Output: [Apple, Banana, Cherry, Date]
}
}
Common Operations on TreeSet in Java
1.Adding Elements: Utilize add(E e) to insert an component. In case the component is as of now display, it won’t be added (no duplicates).
2.Removing Elements: Utilize remove(Object o) to remove an component from the set.
3.Size: You’ll be able get the size of the set using size().
4.Contains: Utilize contains(Object o) to check if a specific component exists within the set.
5.Clear: Utilize clear() to remove all components from the set.