File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes What is the diff between Comparator and Comparable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is the diff between Comparator and Comparable" Watch "What is the diff between Comparator and Comparable" New topic

What is the diff between Comparator and Comparable

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
Can anyone tell me java.lang.Comparable and java.util.Comparator

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

A Comparator is an object that compares other objects, but has no interesting value itself. You could use a single Comparator to sort an array of Person objects.
A Comparable is an object that can be compared to other objects using a compareTo() method. Some API classes like Integer and String implement this interface. You can sort an array of these without supplying a Comparator, sinc e they know how to compare themselves to each other.

[Jess in Action][AskingGoodQuestions]
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
Can you give an example Mr.Hill. Thanks for the reply as well.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

A Comparable would be a String -- it comes with a default sort order (A, B, C, D, E, ...)
A comperable would be a class that you write to implement sorting on non-Comparable objects, or to provide sorting other than the default of a Comparable object.
For example, say that I decided that I wanted to "alphabetize" a collection of string not be the standard Roman alphabet but by the "QWERTY" keyboard alphabet. I would write a Comperable object to do this.
(This is an example that comes to mind because my three-year-old son now sings, to the tune of "Twinkle, Twinkle, Little Star"/"The Alphabet Song" :
"Q W E R T Y,
U I O P A S D.
F G H J K L Z.
X C V B N and M.
Now I know ny keyboard keys,
Next time won't you sing with me...."
of course, I made sure that he knows that alphabet both ways... )

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
It's an instance of "inheritance vs. delegation".
Using the Comparable interface, you use inheritance to put the compareTo method directly into the object you want to compare.
Using the Comparator interface, you delegate to an outside object implementing the compare strategy (see Strategy pattern).
The latter is both more flexible and more complex.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: What is the diff between Comparator and Comparable
jQuery in Action, 3rd edition