Link to home
Start Free TrialLog in
Avatar of Manikandan Thiagarajan
Manikandan ThiagarajanFlag for India

asked on

what is the difference between comparable and comparator interface

what is the difference between comparable and comparator interface
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Manikandan Thiagarajan

ASKER

what is the difference
In order to sort Collection<E> its elemenst need to implemnet Comparable

public static <T extends Comparable<? super T>> void sort(List<T> list)

Bt you can aslo sort your own custom objects if you use
another method whaich takes type implementing Comaprator as arguments:

public static <T> void sort(List<T> list,
                            Comparator<? super T> c)
Loook above they implemnent methods with different signatures
Comparable takes one parameter and Comaparator takes two parameters

14 down vote accepted
      

The text below comes from http://lkamal.blogspot.com/2008/07/java-sorting-comparator-vs-comparable.html

Comparable

A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances.

Comparator

A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.

This is also nice explanation:

Comparable lets a class implement its own comparison:

    it's in the same class (it is often an advantage)
    there can be only one implementation (so you can't use that if you want two different cases)

By comparison, Comparator is an external comparison:

    it is typically in a unique instance (either in the same class or in another place)
    you name each implementation with the way you want to sort things
    you can provide comparators for classes that you do not control
    the implementation is usable even if the first object is null
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thankyou for the analysis
I've requested that this question be closed as follows:

Accepted answer: 0 points for stmani2005's comment http:/Q_27384931.html#37010218

for the following reason:

helpful
so why did you request to close this without allcatiing points?
I believe this question was answered. Perhaps author just pressed the wrong button ?

I'd suggest that the solution should be split between posting http:#36928865 which explains
formal difference beween Comparable and Comparator in terms of method signatures
and http:#36928886 which shows the difference between them in terms of most popular
use cases.
Avatar of South Mod
All,
 
Following an 'Objection' by for_yan (at https://www.experts-exchange.com/questions/27409984/26-Oct-11-15-Automated-Request-for-Review-Objection-to-Accept-Q-27384931.html) to the intended closure of this question, it has been reviewed by at least one Moderator and is being closed as recommended by the Expert.
 
At this point I am going to re-start the auto-close procedure.
 
Thank you,
 
SouthMod
Community Support Moderator