• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Comparator Vs Comparable

 
Ranch Hand
Posts: 203
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..!!
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my standard example showing the usage of both:

 
Ranch Hand
Posts: 66
Android IntelliJ IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
take a look at this explanation and see if it helps: Comparable & Comparator
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about the Object Ordering tutorial?
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic