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

Use of comparator and comparable interface

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

Could anybody please specify when we should use the comparable interface to sort the collection and when to use comparator interface? Since both of them are use for sort any collection. Please specify in details...

Thanks,
Sanjib
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be able to use the Comparable interface, the class of the objects in the collection has to implement that interface. You might have for example a List of objects from a class that you don't control yourself - for example, a class that comes from some third-party library. You can't make that class implement Comparable easily (it would mean you'd have to change the source code of the third-party library, which is normally not a good idea).

To be able to sort the List anyway, you can implement your own Comparator and pass that to for example Collections.sort(list, comparator);. Your Comparator object can be separate from the objects that are in the List.

Another example is when you might want to sort the list in different ways. Suppose you have a Person class:

Suppose that you have a List of Person objects, and that you at one time want to sort the List by name, but at another time sort the List by age. You could make class Person implement Comparable, but you can implement the compareTo() method in only one way.

By using Comparator, you could instead make different comparators, one that compares Person objects by name, and another one that compares them by age. If you want to sort by name, you use the first Comparator object, and if you want to sort by age you use the other Comparator object.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Comparable should be used for the natural ordering - an ordering that just seems logical. For numbers that numerical ordering, for Strings its lexicographical ordering, for a Person class it could be the last name first, first name second. Basically, an ordering you want by default.

Comparator should be used if you don't want a default natural ordering, you can't make your class Comparable (perhaps because you get it from a library) or you want a different way of ordering (e.g. on first name first, last name second for the Person class).
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper Young wrote:
By using Comparator, you could instead make different comparators, one that compares Person objects by name, and another one that compares them by age. If you want to sort by name, you use the first Comparator object, and if you want to sort by age you use the other Comparator object.



by this way, you can move the compare *logic* to a seperate class , which looks good .
 
Sanjib Pal
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Thanks everybody. Those replays cleared my concepts.

Sanjib
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Comparable interface is having compareTo() method and which is normally used for natural ordering.
Comparator interface is having compare() method which takes two arguments and it can be used whenever you want to sort objects based of more then one parameter.

Example:

Comparable Interface:



Comparator Interface:

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks it helps to clear the concept of comparable and comparator
 
Marshal
Posts: 79974
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You will find more details here, here and here, as well as old threads on this forum.
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic