To those of you who do not about Comparable and Comparator these are interfaces provided to sort objects of a given class.
Here are some of the common differences,
1) Comparator in Java is defined in java.util package while Comparable interface in Java is defined in java.lang package, which very much says that Comparator should be used as an utility to sort objects which Comparable should be provided by default.
2) Comparator interface in Java has method public int compare (Object o1, Object o2) which returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. While Comparable interface has method public int compareTo(Object o) which returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
3) If you see then logical difference between these two is Comparator in Java compare two objects provided to him, while Comparable interface compares "this" reference with the object specified. I have shared lot of tips on how to override compareTo() method and avoid some common mistakes programmer makes while implementing Comparable interface.
4) Comparable in Java is used to implement natural ordering of object. In Java API String, Date and wrapper classes implements Comparable interface.Its always good practice to override compareTo() for value objects.
5) If any class implement Comparable interface in Java then collection of that object either List or Array can be sorted automatically by using Collections.sort() or Arrays.sort() method and object will be sorted based on there natural order defined by CompareTo method.
6)Objects which implement Comparable in Java can be used as keys in a SortedMap like TreeMap or elements in a SortedSet for example TreeSet, without specifying any Comparator.
That depends on the problem you are dealing with.If you have a class whose instances store information about employee.And if you want to sort these objects on the basis of employee id than Comparable would be enough but if we look at other side of this problem you might ask that hey can't we sort them with salary or any other basis and also provide user with the option so that he can provide the sort parameter.This is where Comparator comes into picture .With Comparator implemented on a class user can pass parameter saying okay now i want to sort these objects by salary or retirement year.
Example using Comparator
import java.util.*;
public class sort{
static ArrayList<DvdInfo> list=new ArrayList<DvdInfo>();
sort()
{
DvdInfo a=new DvdInfo("Batman Begins "," Christian Bale");
DvdInfo b=new DvdInfo("The Dark Knight "," Aaron Eckhart");
list.add(a);
list.add(b);
}
public static void main(String[] args)
{
sort dummy=new sort();
title t=new title();
Collections.sort(list,t);
System.out.println(list.toString());
actor a=new actor();
Collections.sort(list,a);
System.out.println(list.toString());
}
}
class DvdInfo{
private String title;
private String actor;
DvdInfo(String a,String b)
{
title=a;
actor=b;
}
String getTitle()
{
return title;
}
String getActor()
{
return actor;
}
public String toString(){
return title+""+actor;
}
}
class title implements Comparator<DvdInfo>{
public int compare(DvdInfo a,DvdInfo b)
{
return a.getTitle().compareTo(b.getTitle());
}
}
class actor implements Comparator<DvdInfo>{
public int compare(DvdInfo a,DvdInfo b)
{
return a.getActor().compareTo(b.getActor());
}
}
Output is
[Batman Begins Christian Bale, The Dark Knight Aaron Eckhart]
[The Dark Knight Aaron Eckhart, Batman Begins Christian Bale]
Example using Comparable
import java.util.*;
public class sam{
public static void main(String[] args)
{
Test a=new Test(); //Object a
Test b=new Test(); //Object b
a.c=-50; //Assigning variable c of object a to -50
b.c=55; //Assigning variable c of object b to 55
int x=b.compareTo(a); //Comparing a and b
System.out.println(""+x);
}
}
class Test implements Comparable{
Integer c;
public int compareTo(Object t)
{
Integer v=10;
return c.compareTo(v); //this is a test condition
}
}
Output will be
1
Comparator vs Comparable in Java
Here are some of the common differences,
1) Comparator in Java is defined in java.util package while Comparable interface in Java is defined in java.lang package, which very much says that Comparator should be used as an utility to sort objects which Comparable should be provided by default.
2) Comparator interface in Java has method public int compare (Object o1, Object o2) which returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. While Comparable interface has method public int compareTo(Object o) which returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
3) If you see then logical difference between these two is Comparator in Java compare two objects provided to him, while Comparable interface compares "this" reference with the object specified. I have shared lot of tips on how to override compareTo() method and avoid some common mistakes programmer makes while implementing Comparable interface.
4) Comparable in Java is used to implement natural ordering of object. In Java API String, Date and wrapper classes implements Comparable interface.Its always good practice to override compareTo() for value objects.
5) If any class implement Comparable interface in Java then collection of that object either List or Array can be sorted automatically by using Collections.sort() or Arrays.sort() method and object will be sorted based on there natural order defined by CompareTo method.
6)Objects which implement Comparable in Java can be used as keys in a SortedMap like TreeMap or elements in a SortedSet for example TreeSet, without specifying any Comparator.
So which one we should use?
That depends on the problem you are dealing with.If you have a class whose instances store information about employee.And if you want to sort these objects on the basis of employee id than Comparable would be enough but if we look at other side of this problem you might ask that hey can't we sort them with salary or any other basis and also provide user with the option so that he can provide the sort parameter.This is where Comparator comes into picture .With Comparator implemented on a class user can pass parameter saying okay now i want to sort these objects by salary or retirement year.
Example using Comparator
import java.util.*;
public class sort{
static ArrayList<DvdInfo> list=new ArrayList<DvdInfo>();
sort()
{
DvdInfo a=new DvdInfo("Batman Begins "," Christian Bale");
DvdInfo b=new DvdInfo("The Dark Knight "," Aaron Eckhart");
list.add(a);
list.add(b);
}
public static void main(String[] args)
{
sort dummy=new sort();
title t=new title();
Collections.sort(list,t);
System.out.println(list.toString());
actor a=new actor();
Collections.sort(list,a);
System.out.println(list.toString());
}
}
class DvdInfo{
private String title;
private String actor;
DvdInfo(String a,String b)
{
title=a;
actor=b;
}
String getTitle()
{
return title;
}
String getActor()
{
return actor;
}
public String toString(){
return title+""+actor;
}
}
class title implements Comparator<DvdInfo>{
public int compare(DvdInfo a,DvdInfo b)
{
return a.getTitle().compareTo(b.getTitle());
}
}
class actor implements Comparator<DvdInfo>{
public int compare(DvdInfo a,DvdInfo b)
{
return a.getActor().compareTo(b.getActor());
}
}
Output is
[Batman Begins Christian Bale, The Dark Knight Aaron Eckhart]
[The Dark Knight Aaron Eckhart, Batman Begins Christian Bale]
Example using Comparable
import java.util.*;
public class sam{
public static void main(String[] args)
{
Test a=new Test(); //Object a
Test b=new Test(); //Object b
a.c=-50; //Assigning variable c of object a to -50
b.c=55; //Assigning variable c of object b to 55
int x=b.compareTo(a); //Comparing a and b
System.out.println(""+x);
}
}
class Test implements Comparable{
Integer c;
public int compareTo(Object t)
{
Integer v=10;
return c.compareTo(v); //this is a test condition
}
}
Output will be
1
No comments:
Post a Comment