difference between comparator and comparable interface

A

Andrew McDonagh

gaurav said:
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.
 
M

Michiel Konstapel

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
 
O

Oscar kind

In comp.lang.java.help Michiel Konstapel said:
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.

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.
 
Joined
Apr 14, 2009
Messages
1
Reaction score
0
Java.util.comparatable And Comparator

In compareto(object 1,obect 2) and compare() method the object ar comapared and returned -ve,0 and +ve values based on the comparision --- if object1 is greater than object 2 return 0,my question in what sense the object ar compared as greater or smaller?is it the no. of characters counted and compared?

ex-obj1 let be "fun"
and obj2 let be "hello" then if we compare this two object by using overriding of
compare(obj1,obj2)
{
return obj1.compareTo(obj2);
}
how i can preddict wether it is -1,1 or zero?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top