![]() |
VOOZH | about |
A TreeSet is a collection class that stores unique elements in a sorted order. It is part of java.util package that implements the SortedSet interface, and internally uses a Red-Black tree to maintain sorting.
Syntax
TreeSet<DataType> ts = new TreeSet<>();
This statement creates a TreeSet object in Java that stores elements in sorted order automatically
[5, 10, 20]
TreeSet implements NavigableSet, which extends SortedSet, which extends Set.
Note:
- A class must implement Comparable or provide a Comparator to avoid ClassCastException at runtime.
- Built-in classes like String and wrapper classes already implement Comparable.
In order to create a TreeSet, we need to create an object of the TreeSet class. The TreeSet class consists of various constructors which allow the possible creation of the TreeSet. The following are the constructors available in this class:
Creates an empty TreeSet that sorts elements in their natural order
TreeSet<> ts = new TreeSet<>();
This constructor is used to build an empty TreeSet object in which elements will need an external specification of the sorting order.
TreeSet <> ts = new TreeSet<>(comp);
Creates a TreeSet containing all elements from the given collection, stored in their natural sorted order. It is useful for converting a collection to a TreeSet.
TreeSet <> t = new TreeSet<>(col);
Creates a TreeSet containing the same elements and order as the specified SortedSet.
TreeSet <> t = new TreeSet<>(s);
Here we will be performing various operations over the TreeSet object to get familiar with the methods and concepts of TreeSet in java. Letβs see how to perform a few frequently used operations on the TreeSet.
To add elements to a TreeSet, use the add() method. Elements are stored in ascending order, duplicates are ignored, and null values are not allowed.
[For, Geek, Geeks]
After adding the elements, if we wish to access the elements, we can use inbuilt methods like contains(), first(), last(), etc.
Tree Set is [For, Geek, Geeks] Contains Geeks true First Value For Last Value Geeks Higher Geeks Lower For
The values can be removed from the TreeSet using the remove() method. There are various other methods that are used to remove the first value or the last value.
Initial TreeSet [A, B, For, Geek, Geeks, Z] After removing element [A, For, Geek, Geeks, Z] After removing first [For, Geek, Geeks, Z] After removing last [For, Geek, Geeks]
There are various ways to iterate through the TreeSet. The most famous one is to use the enhanced for loop.
A, B, For, Geek, Geeks, Z,
If a class does not implement Comparable (e.g., StringBuffer), provide a Comparator.
[1, A, B, L, O, Z]
Method of TreeSet Class are depicted below in tabular format which later on we will be implementing to showcase in the implementation part.
| Method | Description |
|---|---|
| add(Object o) | Adds element in sorted order; ignores duplicates. |
| addAll(Collection c) | Adds all elements from a collection; ignores duplicates.. |
| ceiling(E e) | This method returns the least element in this set greater than or equal to the given element, or null if there is no such element. |
| clear() | This method will remove all the elements. |
| clone() | The method is used to return a shallow copy of the set, which is just a simple copied set. |
| Comparator comparator() | This method will return the Comparator used to sort elements in TreeSet or it will return null if the default natural sorting order is used. |
| contains(Object o) | This method will return true if a given element is present in TreeSet else it will return false. |
| descendingIterator() | This method returns an iterator over the elements in this set in descending order. |
| descendingSet() | This method returns a reverse order view of the elements contained in this set. |
| first() | This method will return the first element in TreeSet if TreeSet is not null else it will throw NoSuchElementException. |
| floor(E e) | This method returns the greatest element in this set less than or equal to the given element, or null if there is no such element. |
| headSet(Object toElement) | This method will return elements of TreeSet which are less than the specified element. |
| higher(E e) | This method returns the least element in this set strictly greater than the given element, or null if there is no such element. |
| isEmpty() | This method is used to return true if this set contains no elements or is empty and false for the opposite case. |
| Iterator iterator() | Returns an iterator for iterating over the elements of the set. |
| last() | This method will return the last element in TreeSet if TreeSet is not null else it will throw NoSuchElementException. |
| lower(E e) | This method returns the greatest element in this set strictly less than the given element, or null if there is no such element. |
| pollFirst() | This method retrieves and removes the first (lowest) element, or returns null if this set is empty. |
| pollLast() | This method retrieves and removes the last (highest) element, or returns null if this set is empty. |
| remove(Object o) | It returns true/false whether the element was removed |
| size() | This method is used to return the size of the set or the number of elements present in the set. |
| spliterator() | This method creates a late-binding and fail-fast Spliterator over the elements in this set. |
| subSet(Object fromElement, Object toElement) | This method will return elements ranging from fromElement to toElement. fromElement is inclusive and toElement is exclusive. |
| tailSet(Object fromElement) | This method will return elements of TreeSet which are greater than or equal to the specified element. |