Let us take some programming examples based on String objects in TreeSet
concept :
Example1 : Write a JAVA Program to insert String Objects into a TreeSet where all elements should be inserted according to reverse of alphabetical order.
import java.util.*; class myComp implements Comparator // Our custom comparator class { public int compare(Object obj1, Object obj2) { String str1 = (String)obj1; // Converting object obj1 to a String String str2 = obj2.toString(); // Converting object obj2 to a String return str2.compareTo(str1); /* If we have done ‘return str1.compareTo(str2) then the sorting would have been done according to alphabetical order, but here we changed the order of the objects so that the sorting technique becomes reverse of alphabetical order. */ } } class TreeSetDemo { public static void main(String[] args) { /* TreeSet t = new TreeSet(); If we will declare the TreeSet object in this way, the sorting technique in the TreeSet object will be according to alphabetical order. But to do the sorting in reverse of alphabetical order we will pass our comparator object in the TreeSet constructor as an argument */ TreeSet t = new TreeSet(new myComp()); t.add(“Rajesh”); t.add(“Sinha”); t.add(“Padhle”); t.add(“Internship”); System.out.println(t); // Output : [Sinha, Rajesh, Padhle, Internship] } }
Now if we compile and run this code we will get the following output :
[Sinha, Rajesh, Padhle, Internship]
Note : that objects are sorted in the reverse of alphabetical order as per the requirement.
Example 2 . Write a program to insert StringBuffer objects into the TreeSet where sorting order is alphabetical order.
import java.util.*; class myComp implements Comparator { public int compare(Object obj1, Object obj2) { String str1 = obj1.toString(); // Converting object obj1 to String String str2 = obj2.toString(); // Converting object obj2 to String return str1.compareTo(str2); /* Here we are not modifying the return statement as we want elements to be sorted in alphabetical order. */ } } class TreeSetDemo { public static void main(String[] args) { TreeSet t = new TreeSet(new myComp); /* If we don’t pass any comparator object as an argument in the TreeSet constructor then we will get ClassCast Exception as StringBuffer Objects are not comparable. */ t.add(new StringBuffer(“Rajesh”)); t.add(new StringBuffer(“Sinha”)); t.add(new StringBuffer(“Padhle”)); t.add(new StringBuffer(“Internship”)); System.out.println(t); // Output : [Internship, Padhle, Rajesh, Sinha] } }
Now if we compile and run this code we will get the following output :
[Internship, Padhle, Rajesh, Sinha]
Note : The elements are sorted in alphabetical order here.
Note : If we are depending on default comparator then the objects must be homogeneous and comparable. Otherwise we will get ClassCast Exception as RuntimeException.
If we are defining our own sorting by comparator then objects need not be comparable and homogeneous i.e. we can add heterogeneous non comparable objects also.
Example 3 : Write a program to insert String and StringBuffer objects into TreeSet where sorting order is increasing length order. If two objects having same length, then consider their alphabetical order.
import java.util.*; class myComp implements Comparator { public int compare(Object obj1, Object obj2) { String str1 = obj1.toString(); String str2 = obj2.toString(); int len1 = str1.length(); int len2 = str2.length(); if ( len1<len2) return -1; else if(len1>len2) return 1; else return str1.compareTo(str2); } } class TreeSetDemo { public static void main(String[] args) { TreeSet t = new TreeSet(new myComp); t.add(“Rajesh”); t.add(“Sinha”); t.add(new StringBuffer(“ATM”)); t.add(new StringBuffer(“Padhle”)); t.add(new StringBuffer(“Internship”)); System.out.println(t); // Output : [ATM, Sinha, Padhle, Rajesh, Internship] } }
Now if we compile and run this code we will get the following output :
[ATM, Sinha, Padhle, Rajesh, Internship]
Hope you have clear understanding of Treeset in java and java interview questions are covered in this tutorial.