TreeSet Tutorial Constructors Methods


TreeSet is derived from SortedSet interface. We know earlier, SortedSet is derived from Set and Set is derived from Collection. A complete hierarchy is available at "Java Collections Interfaces Hierarchy". TreeSet is very powerful as it can make use of all the methods of Collection (like iterator() and add()); stores only unique elements, a feature derived from Set interface; elements are implicitly sorted, a feature derived from SortedSet.

This TreeSet Tutorial discusses in detail Constructors and Methods with syntax and functionality.

Finally, TreeSet comes with the following features.

  1. Does not allow duplicates; stores unique elements only.
  2. Returns the elements in ascending order (known as natural order). That is, with TreeSet extra sorting algorithm is not required.
  3. Can make use of all the methods of Collection, Set and SortedSet interfaces.
  4. Ordering can be customized with Comparator interface.

Any data structure (subclass of Collection interface) elements can be passed to TreeSet constructor and obtained the elements in ascending order. This we have done in "Vector Play With". One more easy way to obtain the elements in ascending order is using sort() method of Collections class as we did in "List Reversing Sorting".

The order of elements can be customized (or changed) as per the code demands using Comparator interface.

The elements added to a SortedSet (its subclass TreeSet) must be able to get compared with Comparator interface (using compare() method); if unable to compare, the JVM throws ClassCastException.

TreeSet is node-based data structure. All the elements added are stored in the form of nodes in a tree. Node-based data structures are very fast in addition of elements at any position. TreeSet implementation is advantageous to get the element in sorted order and also for quick retrieval.

TreeSet methods are not synchrnozed. A synchronized set can be obtained as follows.

TreeSet ts1 = new TreeSet();
Set mySet = Collections.synchronziedSet(ts);

Following is the class signature

public class TreeSet extends AbstractSet implements SortedSet, Cloneable, Serializable

Following are the constructors

  1. TreeSet(): Creates a TreeSet object without any elements. Elements are implicitly sorted in natural order.
  2. TreeSet(Comparator comp): Creates a TreeSet object without any elements. Elements are sorted as per the order supplied with Comparator object comp.
  3. TreeSet(Collection col): Creates a TreeSet object with the elements of collection class col. Like this, elements of any collection class can be passed to TreeSet. Elements are implicitly sorted in natural order.
  4. TreeSet(SortedSet ss): Creates a TreeSet object with the elements of SortedSet ss. Like this, a tree set with the elements of the other can be created. Elements are implicitly sorted in natural order.

Apart the methods of Set and SortedSet, what TreeSet can enjoy, it adds one more of its own.

  • Comparator comparator(): Returns the Comparator object that sets the ordering. Returns null if the TreeSet is not set for comparator and follows its own natural order.

Two Programs exists on TreeSet Tutorial.

  1. TreeSet General: General operations like printing the elements in natural order (the default behavior of SortedSet, the super class of TreeSet), extracting a part of a set, creating generics TreeSet etc. are performed.
  2. TreeSet Special: Oerations like cloning the TreeSet, iterating elements with Iterator and the methods like retainAll(), addAll(), containsAll(), size(), remove(), clear() and equals() are performed. The operations are done on a generics TreeSet that stores strings.

Pass your comments on this TreeSet Tutorial.

1 thought on “TreeSet Tutorial Constructors Methods”

  1. Dear Sir,

    So as TreeSet has all the benefits of Set(No dupication) and SortedSet(Sorting), TreeSet should have been used frequently in programming but in reality,I see very rare usage of TreeSet.Could you pls let me know the reason behind it if any?

    Thanks
    Raji

Leave a Comment

Your email address will not be published.