TreeSet in Java

TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equals if it is to correctly implement the Set interface. It can also be ordered by a Comparator provided at set creation time, depending on which constructor is used. The TreeSet implements a NavigableSet interface by inheriting AbstractSet class.
Few important features of TreeSet are as follows:

  1. TreeSet implements the SortedSet interface so duplicate values are not allowed.
  2. Objects in a TreeSet are stored in a sorted and ascending order.
  3. TreeSet does not preserve the insertion order of elements but elements are sorted by keys.
  4. TreeSet does not allow to insert Heterogeneous objects. It will throw classCastException at Runtime if trying to add hetrogeneous objects.
  5. TreeSet serves as an excellent choice for storing large amounts of sorted information which are supposed to be accessed quickly because of its faster access and retrieval time.
  6. TreeSet is basically implementation of a self-balancing binary search tree like Red-Black Tree. Therefore operations like add, remove and search take O(Log n) time. And operations like printing n elements in sorted order takes O(n) time.

Constructors of TreeSet class:

  1. TreeSet t = new TreeSet();
    This will create empty TreeSet object in which elements will get stored in default natural sorting order.
  2. TreeSet t = new TreeSet(Comparator comp);
    This constructor is used when external specification of sorting order of elements is needed.
  3. TreeSet t = new TreeSet(Collection col);
    This constructor is used when any conversion is needed from any Collection object to TreeSet object.


  4. TreeSet t = new TreeSet(SortedSet s);
    This constructor is used to convert SortedSet object to TreeSet Object.

Synchronized TreeSet:
The implementation in a TreeSet is not synchronized in a sense that if multiple threads access a tree set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be “wrapped” using the Collections.synchronizedSortedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the set:

TreeSet ts = new TreeSet();
Set syncSet = Collections.synchronziedSet(ts); 

Below program illustrates the basic opearation of a TreeSet:

// Java program to demonstrate insertions in TreeSet
import java.util.*;

class TreeSetDemo
{
    public static void main (String[] args)
    {
        TreeSet<String> ts1= new TreeSet<String>();

        // Elements are added using add() method
        ts1.add("A");
        ts1.add("B");
        ts1.add("C");

        // Duplicates will not get insert
        ts1.add("C");

        // Elements get stored in default natural
        // Sorting Order(Ascending)
        System.out.println(ts1); 
    }
}
Output:

[A, B, C]

Two things must be kept in mind while creating and adding elements into a TreeSet:

  • Firstly, insertion of null into a TreeSet throws NullPointerException because while insertion of null, it gets compared to the existing elements and null cannot be compared to any value.
  • Secondly, if insertion of an object that cannot be compared with the elements existing in the set a ClassCastException is thrown.

NOTE: For an empty tree-set, when trying to insert null as first value, one will get NPE from JDK 7.From 1.7 onwards null is not at all accepted by TreeSet. However upto JDK 6, null will be accepted as first value, but any if insertion of any more values in the TreeSet, will also throw NullPointerException.
Hence it was considered as bug and thus removed in JDK 7.

Methods of TreeSet class:
TreeSet implements SortedSet so it has availability of all methods in Collection, Set and SortedSet interfaces. Following are the methods in Treeset interface.

  1. void add(Object o): This method will add specified element according to some sorting order in TreeSet. Duplicate entires will not get added.
  2. boolean addAll(Collection c): This method will add all elements of specified Collection to the set. Elements in Collection should be homogeneous otherwise ClassCastException will be thrown. Duplicate Entries of Collection will not be added to TreeSet.
  3. void clear(): This method will remove all the elements.
  4. boolean contains(Object o): This method will return true if given element is present in TreeSet else it will return false.
  5. Object first(): This method will return first element in TreeSet if TreeSet is not null else it will throw NoSuchElementException.
  6. Object last(): This method will return last element in TreeSet if TreeSet is not null else it will throw NoSuchElementException.
  7. SortedSet headSet(Object toElement): This method will return elements of TreeSet which are less than the specified element.
  8. SortedSet tailSet(Object fromElement): This method will return elements of TreeSet which are greater than or equal to the specified element.
  9. SortedSet subSet(Object fromElement, Object toElement): This method will return elements ranging from fromElement to toElement. fromElement is inclusive and toElement is exclusive.
  10. boolean isEmpty(): This method is used to return true if this set contains no elements or is empty and false for the opposite case.
  11. Object clone(): The method is used to return a shallow copy of the set, which is just a simple copied set.
  12. int size(): This method is used to return the size of the set or the number of elements present in the set.
  13. boolean remove(Object o): This method is used to return a specific element from the set.
  14. Iterator iterator(): Returns an iterator for iterating over the elements of the set.
  15. Comparator comparator(): This method will return Comparator used to sort elements in TreeSet or it will return null if default natural sorting order is used.

This article is contributed by Dharmesh Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Chinmoy Lenka




Practice Tags :

Recommended Posts:



2.6 Average Difficulty : 2.6/5.0
Based on 15 vote(s)






User Actions