• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparable and Comparator Interface

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I'm new to this forum, also preparing ocpjp6. I'm finding difficulty with Comparable and Comparator Interface ! . Please help me with this concept.

Thanks.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Comparator and Comparable are quit important from point of view , so also pay importance towards there method signature and valid return type. Both are use to compare objects.
Comparable is used to define natural ordering for the objects whereas comparator is used to provide whatever ordering you want for objects.
For e.g. let say you have created a class MyOddNumber and you as the author of the class want that objects of class MyOddNumber will be be compared in ascending order so for e.g. 1,3,5,7,9, etc .So in order to achieve this you will implement Comparable.
But now let say some other user A want to compare objects of class MyOddNumber but in descending order so for e.g. 9,7,5,3,1 , then he can use Comparator. Another user B wants that objects of class MyOddNumber will be compared in such a way that odd numbers should be in ascending order as well as multiple of 3 [/bf]or e.g. 3,9,15,21,etc.
As you can see thanks to Comparator we can change the natural ordering of the class without modifying the original class and define non-natural ordering
 
D pisces
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sir,

Thanks for the reply. And you mean to say the "natural ordering" of the "Comparator" interface will order the objects only in ascending order? and also from k&b 6 book in the difference between comparable and comparator section . it states like for comparable interface "you must modify the class whose instances you want to sort" and for comparator "you build a class separate from the class whose instances you want to sort" . Please explain me about these differences sir.

Thank you.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

D pisces wrote:it states like for comparable interface "you must modify the class whose instances you want to sort" and for comparator "you build a class separate from the class whose instances you want to sort" . Please explain me about these differences sir.


Suppose you have a class called Number. It looks like this:
To add natural ordering to this class, you need to be able to modify its source code thus you must modify the class whose instances you want to sort.

So you go like this:
What if you can't modify the source code of Number but you need to provide some ordering? Or you want to provide different ordering that natural ordering of Number?

You write a comparator. And this comparator is a separate class.
Like this:Thus you build a class separate from the class whose instances you want to sort.
 
D pisces
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the clear example sir. So the "Comparable" interface will always sort in ascending order and "Comparator" interface can be sorted in many ways like ascending or descending. am i right sir? please correct me if am wrong.

Thank you once again.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

D pisces wrote:Thank you for the clear example sir. So the "Comparable" interface will always sort in ascending order and "Comparator" interface can be sorted in many ways like ascending or descending. am i right sir? please correct me if am wrong.



The Comparable interface will sort in the "natural" order. This is the order determined by the developer of the class. Whether this order is "ascending" or in any other order is determined by the designer/developer of the class.

Henry
 
D pisces
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you pawal ,henry and hamada for the replies. Now i'm clear with this.

Thank you once again.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic