How TreeSet Works Internally in Java
TreeSet is not the most used Java collection class. But in a few cases, it is preferred over other collection classes. It is crucial to know in which cases the TreeSet is preferred over other Collection classes and how it is implemented. It is also important for the interview practice; sometimes, the interviewee may ask you about the internal working of the TreeSet class.
The TreeSet is much similar to the HashSet class. The major difference between both classes is used data structure between them. The HashSet uses a Hashtable, and the TreeSet uses a self-balancing tree.
What is TreeSet
The TreeSet is a class of the Java Collection framework used to store the tree data structure. It uses a tree data structure to store and maintain the objects. The TreeSet class is the implementing class of the Set interface.
The TreeSet stores the objects in the ascending order, which is a natural ordering of a tree. We can also specify a comparator to sort the elements based on it during the creation of the TreeSet. It implements the SortedSet and NavigableSet interface to maintain and navigate the order of the elements.
It is a useful class for finding the comparison between the elements, such as greater than, less than, etc., between the available elements of the tree.
Some key features of the TreeSet class are as follows:
The TreeSet class can be declared as follows:
Consider the below example to understand the behaviour of the TreeSet:
[Hibernate, Java, Spring]
From the above example, we can see duplicate elements are not allowed in TreeSet, and the elements get sorted in ascending order. Let's understand the internal working of the TreeSet.
Internal Working of the TreeSet
The data structure for the TreeSet is TreeMap; it contains a SortedSet & NavigableSet interface to keep the elements sorted in ascending order and navigated through the tree.
As soon as we create a TreeSet, the JVM creates a TreeMap to store the elements in it and performs all the operations on it. It's working is similar to HashSet.
When we implement a TreeSet the following code will be executed by the Java Compiler:
It instantiate a TreeMap and executes the following code to implement the interfaces:
It automatically assigns elements to the corresponding position using the NavigableMap. The TreeMap can not have a duplicate value. So, when we execute the add() method, it performs a put operation and adds a value to the tree.
By default, It sorts values in ascending order, but we can specify the comparator if we want to sort the elements in any specific order. It uses a comparator or comparable variable to compare the elements.
Let's understand how the sorting is performed internally in TreeSet:
How the Sorting is Performed Internally in TreeSet
When we implement a TreeSet, it creates a TreeMap to store the elements. It sorts the elements either naturally or using the user define comparator.
When the object of a TreeSet is created, it automatically invokes the default constructor and creates an object of TreeMap and assigns comparator as null. Below code is executed by the Java compiler:
When we add an element to TreeMap, it compares each element with other elements and put it in TreeMap. It uses comparable to compare the objects. The put() method checks if the comparator object is null.