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

comparator and comparable interfaces

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference(or relation) between comparator and comparable interfaces?
As far as I know comparable i/f has compareTo method and this method implements natural order of objects, where as comparator contains compare method and comparing of objects can be dictated.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Points which you mentioned is absolutely correct.

In addition,

Comparable interface is defined in lang package.

Comparator is defined in util package.

The sort method defined in Collections utility class is overloaded.

1. Collections.sort(reference of collection which you want to sort);

In this case, the object which collection hold must be comparable.

2. Collections.sort(Collection c, Comparator instance)

------------------------------------------------------------------------

The advantage which Comparator give sover Comparable is you can sort the same collection in n no. of ways by passing different comparators in each case.


Naseem
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See if this helps...

Comparable is an adjective. When something is comparable, that means it has the ability to compare itself ("this") to some other object using its compareTo(Object other) method.

Comparator is a noun. When something is a comparator, that means it can compare two other objects using a compare(Object other1, Object other2) method.
 
Pramila Chinguru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The elements in TreeSet/TreeMap should implement comparable i/f by default.

Should they implement either comparable or comparator interface?
 
Pramila Chinguru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Naseem Khan:

The advantage which Comparator give sover Comparable is you can sort the same collection in n no. of ways by passing different comparators in each case.


Naseem



Can we implement comparable interface and override compareTo method to provide different ways of comparing objects other than natural ordering? then wouldn't it be the same kind of service provided by comparator?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pramila ch:
The elements in TreeSet/TreeMap should implement comparable i/f by default.

Should they implement either comparable or comparator interface?


If you check the API for TreeSet and TreeMap, you will see that either can be used, depending on the constructor used (specifically, whether you provide a Comparator for the Set/Map to use on their elements, or whether the elements themselves are Comparable).
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pramila ch:
...Can we implement comparable interface and override compareTo method to provide different ways of comparing objects other than natural ordering? ...


The way you implement compareTo defines what "natural ordering" means for the type.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by pramila ch:

Can we implement comparable interface and override compareTo method to provide different ways of comparing objects other than natural ordering? then wouldn't it be the same kind of service provided by comparator?



I think you did not understand my post.

The benefit which Comparator gives over Comparable is you can sort instances of same class in n number of ways by passing different types of comparator.

Suppose you want to sort the instances of Element class by id (code below), one can pass a comparator for sorting instances based on id say IDComparator.

Collections.sort(c, new IDComparator());





If you want to sort the same collection based on name. One can write NameComparator for this case and pass its reference to sort method.

Collections.sort(c, new NameComparator());




However with Comparable, if you are changing your comparion criterion as above, then you have to modify compareTo() method.


Regards


Naseem
 
Pramila Chinguru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class NameComparator implements Comparator{ // rest code}


Here also we need to modify(override/implement) compare method of comparator interface.
My doubt is instead of implementing comparator interface, can we implement comparable interface itself and modify(override) compareTo method for doing the above functionality.

just like
class NameComparator implements Comparable{

int compareTo(Object obj){
//applicable comparison.
}
}

which would be called by default for TreeSet and TreeMap without having to implement comparator interface.

and second one, since we are able to do comparison by changing compareTo method itself, what is the additional advantage of comparator interface over comparable interface?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can implement the Comparable interface if you have access to the source code of the class definition.

One advantage of a Comparator is that you can create a Comparator class definition independently of the class definitions being compared.

This is very useful if you don't have access to the source code of the classes to be compared or don't want to modify the source code.
 
Pramila Chinguru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we want to sort objects in different ways, for eg:
1. sort the string objects in TreeSet using alphabetical order
2. sort the string objects in the same TreeSet using case insensitive alphabetical order
then objects in the same TreeSet can't have different implementation to same compareTo method. hence we do use comparator for second way of sorting.

The point which is bugging me is why can't the TreeSet/Map take comparable interface itself as the parameter in constructor ? what's the need to have comparator interface separately?
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pramila ch:
If we want to sort objects in different ways, for eg:
1. sort the string objects in TreeSet using alphabetical order
2. sort the string objects in the same TreeSet using case insensitive alphabetical order
then objects in the same TreeSet can't have different implementation to same compareTo method. hence we do use comparator for second way of sorting.

The point which is bugging me is why can't the TreeSet/Map take comparable interface itself as the parameter in constructor ? what's the need to have comparator interface separately?



Comparable really wouldn't make sense as a parameter to TreeSet. As Marc said, Comparable is an adjective that describes a class definition. It is not stand alone.
[ July 03, 2006: Message edited by: Keith Lynn ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The objects stored in Treeset are itself comparable objects. Then why you
need to pass a second comparable argument to same constructor?

Whereas, the elements stored in TreeSet are not comparator, so if you want to compare it by come comparator, pass reference of that comparator in its constructor. Then that comparator will sort objects by its own way.

Naseem
 
Ranch Hand
Posts: 2023
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic