Tuesday 18 June 2013

Comparable vs Comparator in Java

Comparator vs Comparable in Java

Example 

Comparable
public class Fruit implements Comparable<Fruit>{
 
 private String fruitName;
 private String fruitDesc;
 private int quantity;
 
 public Fruit(String fruitName, String fruitDesc, int quantity) {
  super();
  this.fruitName = fruitName;
  this.fruitDesc = fruitDesc;
  this.quantity = quantity;
 }
 
 public String getFruitName() {
  return fruitName;
 }
 public void setFruitName(String fruitName) {
  this.fruitName = fruitName;
 }
 public String getFruitDesc() {
  return fruitDesc;
 }
 public void setFruitDesc(String fruitDesc) {
  this.fruitDesc = fruitDesc;
 }
 public int getQuantity() {
  return quantity;
 }
 public void setQuantity(int quantity) {
  this.quantity = quantity;
 }
 
 public int compareTo(Fruit compareFruit) {
 
  int compareQuantity = ((Fruit) compareFruit).getQuantity(); 
 
  //ascending order
  return this.quantity - compareQuantity;
 
  //descending order
  //return compareQuantity - this.quantity;
 
 } 
}
 
Comparable used for Natural Ordering
Arrays.sort(fruits)
 
Comparator 
 
import java.util.Comparator; 
  
 
 public class Fruit implements Comparable<Fruit>{
 
 private String fruitName;
 private String fruitDesc;
 private int quantity;
 
 public Fruit(String fruitName, String fruitDesc, int quantity) {
  super();
  this.fruitName = fruitName;
  this.fruitDesc = fruitDesc;
  this.quantity = quantity;
 }
 
 public String getFruitName() {
  return fruitName;
 }
 public void setFruitName(String fruitName) {
  this.fruitName = fruitName;
 }
 public String getFruitDesc() {
  return fruitDesc;
 }
 public void setFruitDesc(String fruitDesc) {
  this.fruitDesc = fruitDesc;
 }
 public int getQuantity() {
  return quantity;
 }
 public void setQuantity(int quantity) {
  this.quantity = quantity;
 }
 
 public int compareTo(Fruit compareFruit) {
 
  int compareQuantity = ((Fruit) compareFruit).getQuantity(); 
 
  //ascending order
  return this.quantity - compareQuantity;
 
  //descending order
  //return compareQuantity - this.quantity;
 
 }
 
 public static Comparator<Fruit> FruitNameComparator 
                          = new Comparator<Fruit>() {
 
     public int compare(Fruit fruit1, Fruit fruit2) {
 
       String fruitName1 = fruit1.getFruitName().toUpperCase();
       String fruitName2 = fruit2.getFruitName().toUpperCase();
 
       //ascending order
       return fruitName1.compareTo(fruitName2);
 
       //descending order
       //return fruitName2.compareTo(fruitName1);
     }
 
 };
}
 
 How to use Comparator
Arrays.sort(fruits, Fruit.FruitNameComparator);
 
 
Here are some of the common differences, which is worth remembering to answer this question if asked during a telephonic or face to face interview:

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.

1 comment: