Comparator interface in java is used to sort the elements of collection in some specific orders.
Features of Comparator interface
1) Comparator is present in java.util package.
2) Comparator has two methods – compare() and equals() method.
Comparator Interface Methods
There are two methods defined by the comparator interace
1) Public int compare( object obj1 ,object obj2)
Description :
Return type is “int” and it also give the result same as compareTo() method.
It returns ” – negative ” when obj1 comes before obj2. example – obj1 = 1 , obj2 = 2 and obj1 < obj2
It returns “+ positive ” when obj1 comes after obj2. example – obj1 = 2 , obj2 = 1 and obj1 > obj2
It returns “0“+positive when obj1 equals obj2. example – obj1 = 2 , obj2 = 2 and obj1 = obj2
2) Public boolean equals(object obj )
Description :
Its return type is boolean and returns “True” if Obj1 equals Obj2 and returns “False” if Obj1 not equal Obj2.
example : Obj1.equals(Obj2)
How to implement Comparator Interface ?
Lets have an example of Java program to implement the comparator interface. To implement any interface we have to define all its method but in comparators we have two methods only , hence we have to define compare() method and equal() method but in comparator we do not define equals() method because object class is parent class for all classes in Java and it has the definition of equals() method in it, so we define only compare method of comparator interface.
Note : Class that implement Comparator Interface needs only to define compare() method and class is not required to define equals() method because Equals() method is already inherited to the class from object class through inheritance.
example :
Class padhle implements comparator
{
public int compare(String a, String b)
{
// define compare method here
}
// No need to define equals() method as equals method is already
// defined in object class which is inherited by every class.
// hence this class is also inheriting the object class.
}
Java Program using default ascending sorting order using compareTo() method internally.
By default collection use compareTo() method for sorting the elements
import java.util.*; public class comparator_sorting { public static void main(String[] args) { // TreeSet with default sorting TreeSet ts = new TreeSet(); ts.add(9); // add 1st element ts.add(5); // add 2nd element ts.add(7); // add 3rd element ts.add(3); // add 4th element ts.add(15); // add 5th element ts.add(7); // add 6th element System.out.println(ts); // default sorting order - ascending order using comparaTo() method // Output -> [3, 5, 7, 9, 15] } } //
Java Program using compare() method to sort in descending order using comparator.
import java.util.*; import java.util.*; public class comparator_sorting { public static void main(String[] args) { // TreeSet with customize sorting - descending order // no default sorting is used here // descending_sort parameter is passed in TreeSet constructor TreeSet ts = new TreeSet(new descending_sort()); ts.add(9); // add 1st element ts.add(5); // add 2nd element ts.add(7); // add 3rd element ts.add(13); // add 4th element ts.add(15); // add 5th element ts.add(7); // add 6th element System.out.println(ts); // default sorting order - ascending order using comparaTo() method // Output -> [15, 13, 9, 7, 5] } } // comparator defined for descending order // descending_sort class is implementing Comparator // and defining compare() method with customize requirement // no need to define equals() method class descending_sort implements Comparator { public int compare(Object o1, Object o2) { Integer i1 = (Integer)o1; Integer i2 = (Integer)o2; if(i1<i2) return +1; else if(i1>i2) return -1; else return 0; } } //
Here descending_sort is the comparator class defined for the sorting for descending order. Inside the class we have defined the method compare to sort the elements in descending order. This program insert the elements in TreeSet in descending order.
Note : Positive return type are inserted at right side of tree and negative return type inserted at left side of tree
How above program works lets understand
1) Add 1st element -> 9 , it is inserted at root node
2) Add 2nd element -> 5 , it is compared with 9 -> method called compare(5,9) , 5 <9 returns positive and inserted at right side of tree.
3) Add 3rd element -> 7 , it is compared with 9 -> method called compare(7,9) , 5 <9 returns positive , it goes to right side and again compared with 5 , now compare(7,5) , 7>5 returns negative so it will insert at left side of 5.
4) Add 4th element -> 13 , it is compared with 9 -> method called compare(13,9) , 13 > 9 returns negative , it goes to left side of 9.
5)Add 5th element ->15 , it is compared with 9 -> method called compare(15,9) , 15 >9 returns negative , it goes to left side of 9 and again compared with 13, now compare(15,13) , 15>13 returns negative so it will insert at left side of 13.
6) Add 6th element -> 7 , it also compared in same manner at last it get compared with 7 again , compare(7,7) returns 0 and it will discard due to duplicate elements are not allowed.
Diagram shows the TreeSet insertion order
Note : By default JVM called compareTo() method for default ascending sorting order. Compare() method used for customize sorting.
Various possible combination of comparable methods and their output.
Here consider the input of the above program -> [ 9 , 5 , 7 , 3 , 15 , 7 ] and compareTo() method is used for sorting the elements inside compare() method.
//comparator defined for ascending order class comparator_1 implements Comparator { public int compare(Object o1, Object o2) { // converting object o1 int type to integer type( comparable class ) // only comparable class can use comparTo() method Integer int1 = (Integer)o1; Integer int2 = (Integer)o2; return int1.compareTo(int2); //output -> [5, 7, 9, 13, 15] } } //comparator defined for descending order class comparator_2 implements Comparator { public int compare(Object o1, Object o2) { Integer int1 = (Integer)o1; Integer int2 = (Integer)o2; return -int1.compareTo(int2); // output -> [15, 13, 9, 7, 5] } } //comparator defined for descending order class comparator_3 implements Comparator { public int compare(Object o1, Object o2) { Integer int1 = (Integer)o1; Integer int2 = (Integer)o2; return int2.compareTo(int1); // output -> [15, 13, 9, 7, 5] } } //comparator defined for ascending order class comparator_4 implements Comparator { public int compare(Object o1, Object o2) { Integer int1 = (Integer)o1; Integer int2 = (Integer)o2; return -int2.compareTo(int1); // output -> [5, 7, 9, 13, 15] } } //comparator returns always 1 and insert in same order as inserted // duplicates are also inserted class comparator_5 implements Comparator { public int compare(Object o1, Object o2) { // TODO Auto-generated method stub return 1; // output -> [9, 5, 7, 13, 15, 7] } } //comparator returns always 1 and insert in reverse order as inserted // duplicates are also inserted class comparator_6 implements Comparator { public int compare(Object o1, Object o2) { return -1; //output -> [7, 15, 13, 7, 5, 9] } } //comparator returns always 0 hence only first element is inserted class comparator_7 implements Comparator { public int compare(Object o1, Object o2) { return 0; // output -> [9] } } //
Hope you like this article on Comparator Interface in JAVA.