JAVA TreeSet Tutorial

public classTreeSet<E>
extends AbstractSet<E>
implements NavigableSet<E>, Cloneable, Serializable
where E - the type of elements maintained by this set

  • Underlying data structure for TreeSet is Balanced Tree, that means when we create a
    TreeSet object internally a balanced tree will create for containing data items.

  • Duplicate objects are not allowed.

  • Insertion order is not preserved, but all data items are inserted according to some
    sorting order.

  • Heterogeneous objects are not allowed. If we are trying to insert heterogeneous
    objects then we will get runtime exception known as ClassCastException.

  • Null insertion is allowed, but only once.

  •  

Constructors:

1. TreeSet():Creates an empty TreeSet object where elements are inserted
according to default natural sorting order.

2. TreeSet(Comparator c) : Creates an empty TreeSet object where elements
are inserted according to customized sorting order.

3. TreeSet t=new TreeSet(SortedSet s):From a SortedSet object, we can create
an equivalent TreeSet object.

4. TreeSet t=new TreeSet(CollectionSet s) : From any Collection object, we can
create an equivalent TreeSet object.

Let's see a program on TreeSet

TreeSetExample.java:

package java8s;
import java.util.*;
    public class TreeSetExample
     {
       public static void main(String[] args)
        {
          TreeSet t=new TreeSet();
          t.add(50);
          t.add(35);
          t.add(100);
          //t.add(null); NullPointerException
           //t.add("java"); ClassCastException
          System.out.println(t);
        }
     }

Output

[35, 50, 100]

Null Acceptance:

  • For the empty TreeSet as the first element null insertion is possible. But after
    inserting that null, if we are trying to insert any other element, we will get
    NullPointerException.

  • For non-empty TreeSet, if we are trying to insert null then we will get
    NullPointerException.

Let's see another program

Tree Set Demo1.java:

package java8s;
import java.util.*;
    public class TreeSetExample1
     {
       public static void main(String[] args)
        {
          TreeSet t=new TreeSet();
          t.add(new StringBuffer("Sashi"));
          t.add(new StringBuffer("Sneha"
          t.add(new StringBuffer("Sradha"));
          t.add(new StringBuffer("Sanghamitra"));
          System.out.println(t);
        }
     }

Output

Exception in thread "main" java.lang.ClassCastException:
java.base/java.lang.StringBuffer cannot be cast to
java.base/java.lang.Comparable

Note :

  • If we are depending on the default natural sorting order then objects should be homogeneous and Comparable. Otherwise we will get run time exception known as Class Cast Exception.
  • An object is said to be Comparable if and only if the corresponding class implements java.lang.Comparable interface.
  • String class and all wrapper classes implements Comparable interface, but StringBuffer does not implement Comparable interface.
  • Hence in the above program we got ClassCastException.
Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment