Sunday, 21 February 2016

Comparable And Comparator Interface

By
Comparable Interface
  • It is in java.lang package
  • It have only one method
    • public int compareTo(Object o);
  • How to call compareTo method
    • obj1.compareTo(obj2);
  • There are three options of comparision
    • Return +1 when obj1 > obj2
    • Return -1 when obj1 < obj2
    • Return 0 when obj1 == obj2
Lets take a simple example
class comapareToExample{
 public static void main(){
 System.out.prinlt("A".compareTO("B")); //-1 negative value
 System.out.prinlt("Z".compareTO("B")); //+1 positive value
 System.out.prinlt("A".compareTO("A")); //0
 System.out.prinlt("A".compareTO(null));// NullPointerException

 }
}

How comparable related to TreeSet ?
Lets take simple example
class comapareToExample{
 public static void main(){
 TreeSet ts = new TreeSet();
 ts.add("A");
 ts.add("Z");
 ts.add("C");
 ts.add("B");

 System.out.prinlt(ts);

 }
}

So here in this example whenever we try to add new element a compareTo(Object o); method is called  by JVM for comparison
call to a method will e like this
obj1.compareTo(obj2);
obj1 is the element which is passed in add and obj2 is element which is already inserted
class comapareToExample{
 public static void main(){
 TreeSet ts = new TreeSet();
 ts.add("A"); 
 ts.add("Z"); //"Z".compareTo("A") -ive
 ts.add("C"); //"C".compareTo("A") +ive then "C".compareTo("Z") +ive
 ts.add("B"); //"B".compareTo("A") -ive then "B".compareTo("Z")

 System.out.prinlt(ts); //{A,B,C,Z}

 }
}

This is the case when we want default natural sorting order
Note:
  • If we are not satisfied by default natural sorting order then we can define our own customized sorting order by sing Comparator
  • Comparable meant for default natural sorting order whereas Comparator for customized sorting order.
Comparator Interface

  • Used for customizing sorting order
  • It is in java.util package
  • It contain two method
    • public int compare(Object o,object o1);
    • public boolean equals(Object o);
  • obj1.compare(obj2) this will return:
    • -ve when obj1 has to come before obj2
    • +ve when obj1 has to come after obj2
    • 0 when obj1 is equal to obj2
There is compulsion that for an interface a class should provide implementation for all the method of interface but incase of Comparator there is a loop hole that we only give definition of compare() method not for equals() method it is optional why?

class myClass implements Comparator{
 compare(Object o1,Object o2){

 }
}
Note :
  • Whenever we implement Comparator interface then we must give implementation of compare() method
  • Implementation of equals method is optional because it is already present in every java class from Object class through inheritance
Implementation of Comparator example
Here we are trying to insert Integer object in descending order
class  MyComparatorClass 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;
    }
  }
}
so how to use it
class comapareExample{
 public static void main(){
 TreeSet ts = new TreeSet(new MyComparatorClass());
 ts.add(1); 
 ts.add(22); // compare(22,1) -ive
 ts.add(12); //compare(12,1) -ive then compare(12,22) +ive
 ts.add(0);//compare(0,1) +ive 
 System.out.prinlt(ts); //{22,12,1,0}
 }
}
 Balanced Tree which is generated is 



class  MyComparatorClass implements Comparator{
  public int compare(Object o1,Object o2){
  Integer i1 = (Integer) o1;
  Integer i2 = (Integer) o2;
   return i2.compareTo(i1); // code can be replace by compareTo   method
  }
}
then the output will be in descending order
Various other implementation of compare method if be return
  • return -i1.compareTo(i2);// Descending order [22,12,1,0]
  • return  i1.compareTo(i2);// Ascending order  [0,1,12,22]
  • return  i2.compareTo(i1);// Descending order [22,12,1,0]
  • return -i2.compareTo(i1);// Ascending order  [0,1,12,22] 
  • return +1;//insertion order [1,22,12,0]
  • return -1;//reverse of insertion order [0,12,22,1]
  • return 0;//only first element will be inserted [1]and all other elements are considered as duplicates
Comparision between Comparator and Comparable interface


S.no
Comparable
Comparator
1
It is in java.lang package
It is in java.util package
2
It is used for Default Natural sorting order
It is used for Customized sorting order
3
It has only one method
public int compareTo(Object o);
It has  two methods
public int compareTo(Object o);
public boolean equals(Object o);
4
All wrapper class and String class aready implement compareable
There is no implements class that are implementing Comparator interface

0 comments :

Post a Comment