• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Comparable and Comparator Interface use

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

Please suggest in which real life project senario we should go with Comparable and Comparator interface?

Thanks!
Sumanta Panda
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We would use them when we want to compare objects with one another. So any time you have more than one object of the same type you need to compare.
 
sumanta panda
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Paul Sturrock,

Thanks for your response.Could you please tell me in which senario we should go with comparable and which senario we should go with comparator interface.

Thanks!
Sumanta Panda
 
Marshal
Posts: 27540
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first link returned by Google for keywords "java comparable tutorial":

Object Ordering (The Java™ Tutorials)
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Implementing Comparable lets object compare itself to other objects of the same class, it provides natural ordering fo objects. If you implement Comparator you create small class which you can use to compare objects of other classes, you are injecting ability to order from outside.
Comparable provides just one compareTo() method which means that you will have only one way of comparing objects. In most cases its enough, but sometimes you will want to compare objects of your class in many ways (for example: 'normal' way of comparing Person objects could use names, but in specific case you could need comparing by age or by height) - in such cases Comparator is better, you can have separate class for each criteria.
Sometimes you have to compare objects of class which doesn't implement Comparable and cannot be chanched - in such cases the only way is using Comparator.

To sum up: Use Comparable in simple cases where you can control comparable class. If you cannot modify class or you need more than one way of comparing - use Comparator.
 
reply
    Bookmark Topic Watch Topic
  • New Topic