TreeSet Tutorial In Java With Example

TreeSet is a type of Collection, that implements Set Interface. It is mostly similar to HashSet class that is it do not allow duplicate elements to be stored.

In TreeSet all the values are stored in there natural order, like all integer values are stored in ascending order and strings are stored according to Dictionary values. Apart from adding this functionality of maintaining natural ordering, Tree Set do not allow null values.

TreeSet is best choice for storing large amount of data, as its retrieval and access time is very fast, which makes data to be found in no time.

Important Note: Hash set does not maintains any order, that is when we retrieve values from it we do not get that values in the same order we have entered in it.


Hierarchy of TreeSet class:

TreeSet implements NavigableSet Interface which further extends SortedSet, Set, Collection, and Iterable Interfaces, as shown in figure 1.

Hierarchy of TreeSet class


 Example of TreeSet class: 

Let us discuss TreeSet with the help of program. Following program has been divided into 3 Steps that we will discuss one by one:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

  //Step 1

  TreeSet <Integer>treeSetObject1 = new TreeSet<Integer>();

  treeSetObject1.add(3); 
  
  //Step 2

  treeSetObject1.add(5);

  treeSetObject1.add(4);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  treeSetObject1.add(8);

  treeSetObject1.add(9);

  treeSetObject1.add(10);

  treeSetObject1.add(9);

  treeSetObject1.add(12);

   //Step 3

  System.out.println("Values in tree Set Object 1 :" +treeSetObject1);    

  }

}

Output:

Values in tree Set Object 1:[1, 2, 3, 4, 5, 8, 9, 10, 12]

Description:

  • In Step 1, we have created an object of TreeSet collection, we have defined element to be stored of Integer type.
  • In Step 2, we have used add method to add elements of Integer type in the TreeSet object that we have created in Step 1.
  • In Step 3, we have printed all the values of TreeSet object.

TreeSet Methods:

1. boolean add(Object o):

This methods adds element in the object, which will automatically stores all the elements in increasing order, as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

  TreeSet <Integer>treeSetObject1 = new TreeSet<Integer>();

  TreeSet <String>treeSetObject2 = new TreeSet<String>();

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(5);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  treeSetObject2.add("B");

  treeSetObject2.add("C");

  treeSetObject2.add("A");

  treeSetObject2.add("D");

  treeSetObject2.add("A");

  System.out.println("Values in TreeSet object 1: " +treeSetObject1);

  System.out.println("Values in TreeSet object 2:  " +treeSetObject2);

  }

}

Output:

Values in TreeSet object 1: [1, 2, 3, 5]

Values in TreeSet object 2: [A, B, C, D]

2. boolean addAll(Collection c):

This method adds all the elements of one object to another , as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

  TreeSet <Integer>treeSetObject1 = new TreeSet<Integer>();

  TreeSet <Integer>treeSetObject2 = new TreeSet<Integer>();

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(5);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  treeSetObject2.add(9);

  treeSetObject2.add(11);

  treeSetObject2.add(1);

  treeSetObject2.add(8);

  treeSetObject2.add(7);

  treeSetObject1.addAll(treeSetObject2);

  System.out.println("Values in TreeSet object 1: " +treeSetObject1);

  }

}

Output:

Values in TreeSet object 1: [1, 2, 3, 5, 7, 8, 9, 11]

3. Object ceiling(Object o):

This method retrieves the least element which is greater than or equal to the given element in the argument list, or null if there is no such element in the set as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

  TreeSet <Integer>treeSetObject1 = new TreeSet<Integer>();

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(5);

  treeSetObject1.add(1);

System.out.println("Ceiling value of '5' is:" +treeSetObject1.ceiling(5));

System.out.println("Ceiling value of '2' is:" +treeSetObject1.ceiling(2));

 }

}

Output:

Ceiling value of '5' is:5

Ceiling value of '2' is:3

4. void clear():

This method removes all of the elements from this object, as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

  TreeSet <Integer>treeSetObject1 = new TreeSet<Integer>();

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(5);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  System.out.println("Values in TreeSet object 1:" +treeSetObject1);

  treeSetObject1.clear();

  System.out.println("Values in TreeSet object 1:" +treeSetObject1);

  }

}

Output:

Values in TreeSet object 1:[1, 2, 3, 5]

Values in TreeSet object 1:[]

Read All TreeSet Methods In JAVA With Example


 TreeSet Important Points To Remember:

  • TreeSet only stores unique values, that is duplicate values are not allowed.
  • TreeSet stores values in natural ordering, which means it returns the elements in the increasing order.
  • TreeSet does not allows null values.
  • As its Access and Retrieval time is fast as compared to HashSet, it is best choice for storing large amount of data.

 Quick Revision Points About TreeSet:

  • Stores Values in ascending order.
  • No duplicate values are allowed.
  • No null value is allowed.
  • Best for large amount of data.
  • Faster Access and Retrieval time as compared to HashSet.

Leave a Reply

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