Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Comparable & Comparator interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparable & Comparator interfaces" Watch "Comparable & Comparator interfaces" New topic
Author

Comparable & Comparator interfaces

Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107
Can you please tell me the difference in the implementation of the two Comparable & Comparator interfaces. Preferable if you could give examples.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Comaparable is an interface which indicates that an object may be compared with another Comparable object. Comparator is an interface which indicates that an object may be used to compare other objects. They are quite different concepts.
The API javadoc for Comparable is here and the API javadoc for Comparator is here.
What is it that you are unclear about ?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107
The examples given in the API documentation for Comparable and Comparator interfaces, give an impression that both do the same job; except that
1. the compareTo method of Comparable interface is invoked by an object of a class, that implements the interface and another object which needs to be compared with is passed as an argument to this method.
2. the compare method of the Comparator interface receives two arguments which are objects to be compared.
Is it that, the objects compared by the compareTo method belong to the same class, and the objects compared using compare method are objects to different subclasses of the same parent class?
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Not really. It's more general than that.
A comparator is a "third-party" tool used by a container to order its contents. The container will pass two objects to the supplied comparator each time it needs to compare them. The comparator is responsible for returning an indication of the ordering of the two objects but has no relationship with the objects other than that. The comparator object does not need to be an instance of any class related to the objects being compared, it just needs to know how to compare them.
A Comparable object, on the other hand, knows how to compare itself with another supplied object. If no Comparator is specified then the container must compare its contents by asking each one how it relates to another.
When deciding which of the two appraches to use, you need to decide where you wish to put the comparison code. In some cases it makes sense to build an external Comparator - particularly where the container may contain a wide variety of object types and/or the comparison rules are complex. If the comparison rules are simple and all the objects in the collection are of a single type, it's often easier to make them Comparable and let the container do the comparing.
Is this making any more sense?
Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107
The difference between Comparable and Comparator interfaces, does make sense to me now.
Thankyou Frank.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparable & Comparator interfaces