• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparator Vs Comparable

 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my standard example showing the usage of both:

 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
take a look at this explanation and see if it helps: Comparable & Comparator
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about the Object Ordering tutorial?
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic