Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

difference between comparator and comparable interface

Thread view: 
gaurav - 20 Feb 2005 12:12 GMT
what's the difference between comparable and comparator interface.
Andrew McDonagh - 20 Feb 2005 12:18 GMT
> what's the difference between comparable and comparator interface.

Comparable interface shows that the class knows how to compare ITSELF
against another class.

Comparator interface allows a single class know how to compare two
classes which (typically) are not the same type as the comparator.

Its about moving the responsibility for doing the comparison from the
class that is being compared, to another class who's sole responsibility
is to be the comparator.
Michiel Konstapel - 20 Feb 2005 12:39 GMT
> what's the difference between comparable and comparator interface.

Something that's Comparable knows how to compare itself to others  
(presumably of the same class). How it does so is built into the class.  
For example, Strings compare themselves alphabetically (well, by character  
value, anyway). A Comparator can compare two other objects by whatever  
criterion the Comparator chooses. For example, it might compare Strings by  
their length.
HTH,
Michiel
Oscar kind - 20 Feb 2005 14:24 GMT
In comp.lang.java.help Michiel Konstapel <usenet@konstapel.nl> wrote:

>> what's the difference between comparable and comparator interface.
>
[quoted text clipped - 4 lines]
> criterion the Comparator chooses. For example, it might compare Strings by  
> their length.

The Comparable interface is (IMO) best used to implement a natural order
for lists of elements of the class. For example the low-to-high order of
numbers represented by the Integer class. This ties in nicely with the
order used by the variants of TreeSet, TreeMap, Arrays.sort() and
Collections.sort() where no Compataror is specified.

The Comparator interface is then used to create exotic orders, where
exotic is anything not implemented by the Comparable interface. This also
means the most obvious order for elements if a class doesn't implement
Comparable, though a subclass may sometimes be a better choice then.

Signature

Oscar Kind                                    http://home.hccnet.nl/okind/
Software Developer                    for contact information, see website

PGP Key fingerprint:    91F3 6C72 F465 5E98 C246  61D9 2C32 8E24 097B B4E2



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.