TreeSet in java

TreeSet(Since Java 1.2)

  • Underlying data structure is balanced tree.
  • Duplicates are not allowed.
  • Insertion order is not preserved since objects will be inserted based on some sorting technique.
  • Null insertion is not possible in a non-empty TreeSet. We will get NullPointerException if we add.
  • Heterogeneous objects are not allowed.  We will get ClassCastException if we add.
  • Homogenous and Comparable: If you need default sorted order, the objects which you are adding in a TreeSet should be Homogenous and Comparable. Otherwise we will get ClassCaseException.

Constructors in TreeSet :

1)      TreeSet ts  = new TreeSet();

It creates empty TreeSet object in which the sorting order is default natural sorting order.

2)      TreeSet ts =  new TreesSet(Comparator comparator)

It creates empty TreeSet object in which the sorting order is customized sorting order specified by comparator

3)      TreeSet ts = new TreeSet(Collection c)

It creates empty TreeSet object with the specified collection

4)      TreeSet ts= new TreeSet(SortedSet s)

It creates empty TreeSet object with the specified sorted set

Example:

 Output:

It sorted based on natural order. Internally it does comparison as below:

ts.add(“C”);

ts.add(“B”); à “B”.compareTo(“C”); -VE

ts.add(“A”); à “A”.compareTo(“B”); -VE

ts.add(“D”); à “D”.compareTo(“A”); +ve

compareTo (T object): Here sorting is happened with natural sorting order. Hence JVM internally calls the compareTo() method  of Comparable interface.  Based on return type value JVM identifies the location of an object in sorting order.

Object1.compareTo(ObjectTo) :

compareTo returns  negative value, If object1 has to come before object2.

compareTo returns positive value, If object1 has to come after object2.

compareTo returns 0(zero) value if object1 and object2 are equal.

Null acceptance in TreeSet with Examples :

  • NullPointerException will come, if you add elements in a non-empty TreeSet.
  • It’s possible to add null as first element in the empty TreeSet.  NullPointerException will come if you add any other element after inserting null.
  • So the thing you can add only one null element in a TreeSet and you will get NPE if you try to add either null or element after insertion of null.

Example-1 :  We will get NPE, if you add null in a non-empty tree set.

 Output:

Reason It compares with the first element, once you add second element as Null in a treeset.

ts.add(“sree”);

ts.add(null); // null.compareTo(“sree”) which throws NullPointerException.

Example-2 :We will get NPE, if you add any element after inserting null as first element.

Output:

Example-3 : We will get NPE, if you add null after inserting null as first element.

Output:

Example-4 : We can add only one null element in a TreeSet and you will get NPE if you try to add either null or element after insertion of null.

 Output:

Heterogeneous objects are not allowed: 

We will get ClassCastException if we try to add Hetrogenous objects in a TreeSet.

Example:

Output:

Comparator Interface:

If you don’t want natural sorted order and you want your own customized sorting order then use Comparator interface (java.util.Comparator).

Comparator interface has below 2 methods.

1)      int compare(T o1, T o2);

  • It returns negative value, if object1 has to come before object2.
  • It returns positive value, if object1 has to come after object2.
  • It returns 0(zero) value if object1 and object2 are equal.

2)      boolean equals(Object obj);

We must override compare (o1, o2) method if you are implementing Comparator Interface.

Its optional to implement equls(o) method if you are implementing Comparator Interface, since default implementation is already there in java.lang.Object class which is super class for all java classes.

Example on Comparator with TreeSet:

 Output:

If you need default sorted order, the objects which you are adding in a TreeSet should be Homogenous and Comparable. Otherwise we will get ClassCaseException. If you are using your own customized sorting order using Comparator interface, the objects which you are adding in a TreeSet need not to be Comparable and Homogeneous.

Example on TreeSet, Comparable, Comparator with Customer Object.

Output:

 

Posted in collections and tagged , , .

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">