GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes comparator and comparable interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "comparator and comparable interfaces" Watch "comparator and comparable interfaces" New topic
Author

comparator and comparable interfaces

Pramila Chinguru
Ranch Hand

Joined: May 05, 2006
Posts: 54
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.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
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


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Pramila Chinguru
Ranch Hand

Joined: May 05, 2006
Posts: 54
The elements in TreeSet/TreeMap should implement comparable i/f by default.

Should they implement either comparable or comparator interface?
Pramila Chinguru
Ranch Hand

Joined: May 05, 2006
Posts: 54
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Apr 25, 2005
Posts: 809

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

Joined: May 05, 2006
Posts: 54
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?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: May 05, 2006
Posts: 54
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Apr 25, 2005
Posts: 809
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
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Few search results from JavaRanch:

Comparable & Comparator interfaces
Comparable & Comparator
What is the diff between Comparator and Comparable
Comparable vs Comparator
Comparable & Comparator
Diff. Between Comparable & Comparator Interface
 
GeeCON Prague 2014
 
subject: comparator and comparable interfaces