Loading

TreeSet

  • TreeSet is an implementation class of SortedSet. It was introduced from JDK1.2 
  • The elements in the TreeSet are sorted by default in ascending order.
  • We can add only the objects that are comparable type to TreeSet otherwise TreeSet throws ClassCastException.
  • The TreeSet doesn't support heterogeneous collection.
  • Null cannot be inserted in TreeSet.
  • The Elements of the TreeSet compare themselves based on the comparable interface. Such comparison is known as mutual comparison.
  • The TreeSet works on the Binary data structure.
  • When you add an object into TreeSet internally calls compareTo() method. 

Constructor of TreeSet.

No-argument constructor: 

  • TreeSet s1= new TreeSet();
  • Creates an empty TreeSet which arranges the element in natural sorting order.
  • The TreeSet allows only mutually comparable objects otherwise it throws ClassCastException.

comparable type-argument constructor: 

  • TreeSet s1= new TreeSet(c1);
  • Let's say c1 is the object of comparable type.
  • Creates an empty TreeSet where the elements are not arranged in a natural sorting order but they arranged as per the implementation of comparator type.  

Sort Set type-argument constructor: 

  • TreeSet s1= new TreeSet(x);
  • Let's say x is an object of type sorted set.
  • Creates an element of a specified sorted set.

Collection  type-argument constructor: 

  • TreeSet s1= new TreeSet(s1);
  • Let's say s1 is the collection of n elements.
  • Creates a TreeSet with elements of collection type the elements must be mutually comparable type otherwise it throws ClassCastException. 

We will see how the TreeSet works with an example. 

package quipoin;

import java.util.TreeSet;

public class Demo {
	public static void main(String[] args) {
		TreeSet<Integer> treeSet = new TreeSet<>();
		treeSet.add(30);
		treeSet.add(10);
		treeSet.add(50);
		treeSet.add(20);
		treeSet.add(60);
		treeSet.add(40);

		System.out.println("The elemens of TreeSet are:");
		for (int num : treeSet) { // foreach loop
			System.out.println(num);
		}
	}
}

Output:

The elemens of TreeSet are:
10
20
30
40
50
60
  • We can observe that elements are randomly inserted into TreeSet but the output is in order wise. TreeSet internally calls the compareTo() method.
  • compareTo () method returns ‘-1’ when the value is lesser than, ‘+1’ when the value is greater than and returns ‘0’ when the value is equal.

Example 2: String type objects

package quipoin;

import java.util.Iterator;
import java.util.TreeSet;

public class Sample {
	public static void main(String[] args) {

		TreeSet<String> ts = new TreeSet<>();
		ts.add("Banana");
		ts.add("Cat");
		ts.add("Apple");

		System.out.println("Elements of TreeSet are:");
		Iterator<String> t = (Iterator<String>) ts.iterator();
		// traversing elements using iterator() method;
		while (t.hasNext()) {
			System.out.println(t.next());
		}
	}
}

Output:

Elements of TreeSet are:
Apple
Banana
Cat
  • In the above example, String types of objects are sorted based on the ASCII values.
  • All the wrapper class implements comparable interface and the compareTo() method is overridden by those respective class.