< Prev
Next >



Comparator Interface





As we know that TreeSet and PriorityQueue class sort their elements in an ascending order by default. But if you want to order the elements in TreeSet/PriorityQueue/TreeMap in a different order than their default ascending order then we will have to implement Comparator interface.




Comparator interface defines two methods -







Signature of compare() method


int compare(T obj1, T obj2)

This method compares the two similar type objects(obj1 and obj2) with each other. This method defines conditions for ascending ordering of elements -:




Note :


if we want to give a descending ordering than we must implement this method in such a way that it reverses the outcome of a comparison between both objects.




Sorting a collection with Integer object -:


By default, TreeSet stores its elements in an ascending order. We are implementing Comparator interface to give a descending order to the Integer objects in TreeSet.
import java.util.*;

class Comp implements Comparator<Integer> 
{
public int compare(Integer obj1, Integer obj2)  //Implementing Comparator's compare() method to give a descending ordering
{
obj2.compareTo(obj1); //Integer's compareTo() method compares two Integer objects
}

 

public static void main(String... ar)
{
TreeSet<Integer> pq= new TreeSet<Integer>(new Comp()); //Passing an object of implementer of Comparator Interface
pq.add(17);
pq.add(40);
pq.add(72);
pq.add(39);  
pq.add(234);
pq.add(24);
pq.add(22);

System.out.println("TreeSet with descending ordering" + pq);
System.out.println("Size of TreeSet = "+ pq.size());
}

}


Output :


TreeSet with descending ordering : 234 72 40 39 24 22 17
Size of TreeSet = 7


Program Analysis







Sorting a collection with String objects.


Here in the next example, we are implementing Comparator interface to give a descending order to the String objects in a TreeSet.
import java.util.*;

class Comp implements Comparator
{

public int compare(String obj1, String obj2) //overriding Comparator's compare() method to give a descending ordering
{
return(obj2.compareTo(obj1)); //String's compareTo() method compares two String objects.
}


public static void main(String... ar)
{
TreeSet<String> tree= new TreeSet<String>(new Comp2()); //passing an object of implementer of Comparator Interface

tree.add("H");
tree.add("E");
tree.add("M"); 
tree.add("A");   
tree.add("N");

System.out.println("Content of TreeSet with descending ordering" + tree);
System.out.println("Size of TreeSet = "+ tree.size());
}
}


Output is - :


Content of TreeSet with descending ordering[N, M, H, E, A]
Size of TreeSet = 5


Program Analysis


Here, we have called String's compareTo() method which compares two String objects. However, obj2(and not obj1)invokes compareTo(). This will reverse the outcome of comparison between obj1 and obj2 and TreeSet String objects will be sorted in an descending order.



Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< TreeSet Class
PriorityQueue Class >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2