TreeSet in Java

Java TreeSet with Example

Java TreeSet implements the Navigable Set interface. It stores the objects in ascending order. It contains unique elements. Access and retrieval time is fast. It does not include the null element. The TreeSet class is synchronized. It doesn’t contain the duplicate elements because it extends Sorted Set. It does not maintain the insertion of order, but elements are sorted.  It doesn’t allow to insert heterogeneous objects if you want to add heterogeneous elements it gives classCastException at runtime. TreeSet uses the data structure which is Red-Black Tree. The complexity time for insert, delete and search operation is O(log n). The complexity time for an operation like printing N elements is O (n) time.

Synchronized TreeSet

TreeSet is not synchronized because if multiple thread access the tree set immediately, Then, at least one thread modifies the set and synchronized externally. It accomplished by synchronizing on some objects that naturally encapsulated the set. The set should be wrapped by using Collections.synchronizedSortedSet method when no such objects exist.

To prevent the unsynchronized access to the TreeSet, we have to synchronize at creation time:

There are two things which remember at the creation and insertion time:

  1. At the insertion time, null not be inserted. Elements get compared to each other and null cannot be compared to any value. If you insert null, then it throws NullPointerException.
  2. If you want default sorting order, then the object should be homogeneous and comparable. Otherwise, it throws a Runtime Exception.

Example:

Output:

Constructors of TreeSet

  1. TreeSet t= new TreeSet (): It is the default constructor. It will create the empty TreeSet object.
  2. TreeSet t= new TreeSet (Collection c): It is used when any conversion is needed of any collection object to TreeSet objects.
  3. TreeSet t= new TreeSet (Comparator comp): It is used to create the empty TreeSet that will sort according to their comparator.
  4. TreeSet t= new TreeSet (SortedSet s): This is used to convert the SortedSet object to the TreeSet object.

Methods in TreeSet

  1. add (): It is used to add elements according to their sorting order and unique values.

Example:

Output:

TreeSet:

  1. addAll (Collection c): This is used to add all elements of the specified collection in the set.

Example:

Output:

  1. clear (): It is used to clear the elements from the set.

Example:

Output:

  1. contains (object o): It is used to check the element present in the set or not. If element present, it returns true otherwise return false.

Example:

Output:

  1. first (): This method is used to return the first element of the TreeSet.

Example:

Output:

  1. last (): This method is used to return the last element of the TreeSet.

Example:

Output:

  1. headSet (Object to Element): This method is used to return the elements of the TreeSet which is less than the specified element.

Example:

Output:

  1. tailSet (Object to Element): This method is used to return the element which is greater than or equal to the Specified element of the TreeSet.

Example:

Output:

  1. subset (object fromElement, object toElement): This method is used to return the element in the range of fromElement to toElement of the TreeSet.

Example:

import java.io.*;

Output:

  1. isEmpty (): It returns true if the set is empty otherwise false.

Example:

Output:

  1. clone (): It is used to make the same copy of the set.

Example:

Output:

  1. size (): This method is used to return the size of the set.

Example:

import java.io.*;

Output:

  1. remove (Object o): It is used to eliminate the particular element from the set.

Example:

Output:

  1. iterator (): Returns the iterator over the elements of the TreeSet.

Example:

Output:

  1. comparator (): It returns comparator used to sort elements in the TreeSet.

Example:

Output:

  1. descendingSet (): It returns the element in reverse order.

Example: