compare and compareTo In JAVA Programming

What is the difference between compare() and compareTo()?





compareTo() is from the Comparable interface.
compare() is from the Comparator interface.
Both methods do the same thing, but each interface is used in a slightly different context.
The Comparable interface is used to impose a natural ordering on the objects of the implementing class. The compareTo() method is called the natural comparison method. The Comparator interface is used to impose a total ordering on the objects of the implementing class. For more information, see the links for exactly when to use each interface.




java.lang.Comparable
1. To implements comparable interface, class must implement a single method compareTo()
int a.compareTo(b)
2. You must modify the class whose instance you want to sort. So that only one sort sequence can be created per class.
java.util.Comparator
1. To implements comparator interface, class must implement a single method compare()
int compare (a,b)
2. You build a class separate from class whose instance you want to sort. So that multiple sort sequence can be created per class.


Example 



Comparable
import java.util.*;
class A implements Comparable
{
          int no;
          String name;
          A(int a,String n)
          {
                   this.no=a;
                   this.name=n;
          }
          int getVal()
          {
                   return this.no;
          }
          String getName()
          {
                   return this.name;
          } 
          public String toString()
          {
                   return this.no+" - "+this.name;
          }

          public int compareTo(Object a1)
          {
                   //return this.no-((A)a1).no;   -- Sort Number
                    return (((A)a1).name).compareTo(this.name);  // Sort Name
          }
          public static void main(String []s)
          {
                    A a[] = new A[5];
                   a[0] = new A(3,"Vijay");
                   a[1] = new A(4,"Ajay");
                   a[2] = new A(1,"Sanjay");
                   a[3] = new A(2,"Jay");
                   a[4] = new A(5,"Parajay");
                            
                   Arrays.sort(a);

                   for(int i=0;i<5;i++)
                   {
                             System.out.println(a[i]);                   
                   }
          }
}                         

// Comparator


import java.util.*;
class Ano implements Comparator
{
          public int compare(Object o1,Object o2)
          {
                   return ((A)o1).no-((A)o2).no;  
          }                 
}
class Aname implements Comparator
{
          public int compare(Object o1,Object o2)
          {
                   return (((A)o1).name).compareTo(((A)o2).name);
          }                 
}
class A
{
          int no;
          String name;
          A(int a,String n)
          {
                   this.no=a;
                   this.name=n;
          }
           
          public String toString()
          {
                   return this.no+" - "+this.name;
          }
         
          public static void main(String []s)
          {
                   ArrayList a = new ArrayList();
                   a.add(new A(3,"Vijay"));
                   a.add(new A(4,"Ajay"));
                   a.add(new A(1,"Sanjay"));
                   a.add(new A(2,"Jay"));
                   a.add(new A(5,"Parajay"));
                  
                   Collections.sort(a,new Aname());

                   Iterator ir = a.iterator(); 
                   A a1;
                   while(ir.hasNext())
                   {
                             a1 = (A)ir.next();
                             System.out.println(a1); 
                   }
          }
}


EmoticonEmoticon