-7

Possible Duplicate:
Java: What is the difference between implementing Comparable and Comparator?
When to use Comparable vs Comparator

what's the difference between comparable and comparator interface ? can someone explain me by providing an corresponding example ?

Thank You

Community
  • 1
  • 1
Mihir
  • 2,480
  • 7
  • 38
  • 57
  • http://stackoverflow.com/q/1440134/839527 This previous question will be help you – JProgrammer Feb 25 '12 at 05:52
  • http://jagadeeshm.wordpress.com/2009/09/26/java-difference-between-comparable-and-comparator-interface/ http://www.javabeat.net/qna/633-difference-between-comparable-interface-and-c/ http://www.velocityreviews.com/forums/t141190-difference-between-comparator-and-comparable-interface.html ... http://apachejava.blogspot.com/2010/10/difference-between-comparable-and.html Take your pick! I have more :) – joe_coolish Feb 25 '12 at 05:53

3 Answers3

3

THis is what the rule of the thumb:

  1. implement Comparable interface to provide natural ordering.
  2. Use Comparator, to sort a collection of objects based on the criteria you wished for.

so you have

Note: The following code is just for illustration purpose, may not compile.

public class Student implements Comparable<Student>{

   public int age;
   public String name;

   //defined natural ordering is by name
   public int compareTo(Stundent that){
      return this.name.compareTo(that.name);
   }
}

public class AgeComparator implements Comparator<Student>{
   int compare(Student s, Student t){
      return s.age - t.age;
   }
}

Now, If you have a List of Student, say, studentList, you can use the following

Collections.sort(studentList); //sorts by natural ordering; by name
Collections.sort(studentList, new AgeComparator()); //sorts by age
Nishant
  • 54,584
  • 13
  • 112
  • 127
0

From the Java Documentation on Comparable:

This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.

In other words, a class that implements Comparable is capable of comparing itself to other objects of the same kind.

From the Java documentation on Comparator:

A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don't have a natural ordering.

A class that implements Comparator is capable of comparing other objects with each other.

kba
  • 19,333
  • 5
  • 62
  • 89
0

An object which has the type Comparable<T> is naturally ordered with respect to T. Given a T that, it determines the relation (<, =, or >) between this and that.

A Comparator<T> is an ordering of T. Given two Ts, it determines their relation (<, =, or >).

An example of a naturally ordered type is Integer. The natural order of integers is {-MAX_VALUE, ..., -1, 0, 1, ..., MAX_VALUE}. A type can only have one natural order, partly because the concept of a natural order entails that it is unique, and partly because a single class can't have multiple implementations of Comperable.compareTo.

On the other hand, there can be several meaningful Comparators for a single type. For example, String.CASE_INSENSITIVE_ORDER is a Comparator<String> which ignores case.

Daniel Lubarov
  • 7,796
  • 1
  • 37
  • 56