aspose file tools*
The moose likes Java in General and the fly likes Comparator Vs Comparable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparator Vs Comparable" Watch "Comparator Vs Comparable" New topic
Author

Comparator Vs Comparable

Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Hi,

I am going through comparator and comparable, I have done lot of googling also, can you guys please explain in simple,very simple words that whats the use of both of them In which real life situation we should choose comparable and in which situation we should go for comparator..!! please if possible explain it with a small example..!!
Haina Minawa
Ranch Hand

Joined: Oct 13, 2011
Posts: 119
Saral Saxena wrote:Hi,

I am going through comparator and comparable, I have done lot of googling also, can you guys please explain in simple,very simple words that whats the use of both of them In which real life situation we should choose comparable and in which situation we should go for comparator..!! please if possible explain it with a small example..!!


Well, things are simple:

- Comparator: A comparator compares two object together by the method: compare(Object1, Object2). The objects being compared do not have to aware about the comparison logic. This is suitable for the situation in which you don't want to modify the compared object class.

- Comparable: A comparable object must implement the compareTo(Object) method, the compared object is responsible for the comparison logic. In this way you have to write additional code for the compared objects.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
Here is my standard example showing the usage of both:

Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

take a look at this explanation and see if it helps: Comparable & Comparator


Keith Rainey
OCPJP6
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

How about the Object Ordering tutorial?


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
Implementing Comparable on your class lets you sort objects of that class in one way, by sticking on a compareTo(Object o) method. Note that you have to modify the class to do this, so it won't work if the class is final. And it's no help if you need to sort multiple ways.

Implementing Comparator means you leave your own class alone and instead create at least one new class. This class implements Comparator. Each Comparator sorts your objects its own way. So you can have a MonkeysByCuriosityAscending class and a MonkeysByCuriosityDescending class. Use the one that sorts the way you want.

Note the following detail of Rob's example. He used a generic type on his Comparable declaration: public class Person implements Comparable<Person>

That lets him do this:



If he had not used a generic he would have had to say public int compareTo(Object o) and then have done some ugly casts inside the method to check if it's really a Person he's dealing with.


Costs matter. Justice lies in processes not outcomes. Crime is caused by criminals.
 
 
subject: Comparator Vs Comparable