Comparable and Comparator

5 views
Skip to first unread message

Axat

unread,
Apr 23, 2011, 5:12:50 PM4/23/11
to UBC CPSC 210
The compareTo method in comparable takes one argument and the
compareTo method in comparator takes two arguments. Is it possible to
know when to use the comparator and when to use the comparable
interface?

Paul Carter

unread,
Apr 23, 2011, 8:37:28 PM4/23/11
to ubc-cp...@googlegroups.com
The Comparator interface defines what's known as the "natural ordering" for a class. However, it's often possible to define more than one ordering - in such cases, you would use the Comparator interface to define any additional orderings.

Paul

Axat

unread,
Apr 23, 2011, 10:22:04 PM4/23/11
to UBC CPSC 210
If I had to use more than one ordering, I would use the Comparable
interface?

On Apr 23, 5:37 pm, Paul Carter <pcar...@cs.ubc.ca> wrote:
> The Comparator interface defines what's known as the "natural ordering" for a class.  However, it's often possible to define more than one ordering - in such cases, you would use the Comparator interface to define any additional orderings.
>
> Paul
>

Paul Carter

unread,
Apr 24, 2011, 6:29:26 PM4/24/11
to ubc-cp...@googlegroups.com
If you have more than one ordering, you would normally designate one of them as the "natural ordering" and you would specify it by implementing the Comparable interface. Any additional orderings would then be specified by implementing the Comparator interface.

Paul

montse

unread,
Apr 24, 2011, 11:51:27 PM4/24/11
to UBC CPSC 210
In the documentation for Comparable, it is specified that it imposes a
total ordering(natural ordering) of the class that implements it. For
practicality, which would be the difference between imposing a natural
ordering and defining a natural ordering?

In terms of when to implement Comparator instead of Comparable, how
different would it be to implement comparable and override equals from
the class Object against implementing Comparator?

Paul Carter

unread,
Apr 25, 2011, 12:31:32 AM4/25/11
to ubc-cp...@googlegroups.com
There's no difference between imposing and defining a natural ordering.

I'm not too sure what you're getting at in your second question below (the two approaches that you've outlined are quite different) but the following might help. If you define a natural ordering for a class by implementing the Comparator interface, you can add instances of the class to a TreeSet or as the keys in a TreeMap and the natural ordering will be used to sort those items.

If a class does not implement Comparable, you cannot add instances of that class to TreeSet or TreeMap unless you also implement a Comparator which defines an ordering for instances of that class.

Paul

Jonatan Schroeder

unread,
Apr 25, 2011, 3:45:43 AM4/25/11
to ubc-cp...@googlegroups.com
> ... If you define a natural ordering for a class by implementing the Comparator interface...

Paul, wouldn't that be Comparable?

Anyway, giving my two cents, notice that when a class implements
Comparable, it is comparing itself against another object, while when
a class implements Comparator, it is comparing two other objects. One
example from the Java default library, the class String implements
Comparable, because you can call "abc".compareTo("def") and get the
natural ordering of the strings (in this case, lexicographical order).
Now take a look at Collator, which implements Comparator, and is used
to compare two strings (i.e. not itself against another Collator, but
two different objects), so you can use:
Collator.getInstance().compare("abc", "def") and you will get a
different order (in this case the alphabetical order, i.e. ignoring
case, so A < b < C).

Hope this helps,

Jonatan
--
Jonatan Schroeder, MSc.
Teaching Assistant, PhD Student
UBC - Computer Science
jon...@cs.ubc.ca - http://www.cs.ubc.ca/~jonatan

"He who asks is a fool for five minutes, but he who does not ask
remains a fool forever." -- Chinese proverb
"Computer science is no more about computers than astronomy is about
telescopes." -- Edsger W. Dijkstra

Paul Carter

unread,
Apr 25, 2011, 12:06:01 PM4/25/11
to ubc-cp...@googlegroups.com
Sorry folks, in my message below, the sentence "If you define a natural ordering for a class by implementing the Comparator interface..." should read "If you define a natural ordering for a class by implementing the Comparable interface..."

Paul

Reply all
Reply to author
Forward
0 new messages