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 why CompareTo exists when we already have equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "why CompareTo exists when we already have equals method" Watch "why CompareTo exists when we already have equals method" New topic

why CompareTo exists when we already have equals method

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
why java String class has got CompareTo method when it has already got equals method?

SCJP 1.4
fred Joly
Ranch Hand

Joined: Jan 19, 2006
Posts: 55
It would be impossible to sort String with just an "equals" method.
You need some method to tell you "bigger" or "smaller". (before or after)
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Did you notice that String is implementing Comparable ?

[My Blog]
All roads lead to JavaRanch
Anuj Singhal

Joined: Oct 17, 2003
Posts: 25
As per the contract of compareTo method, it is a "natural comparison method".
This means that this method should be used only to compare objects for order, and not to check equality of two objects.

Then, why do Set use compareTo method, instead of equals method, to eliminate duplicate items.
It expects compareTo() to be consistent with equals.
Isn't a breach of contract of compareTo method?

Simon Baker
Ranch Hand

Joined: Sep 09, 2004
Posts: 57
I'm not sure that it does use compareTo() for this purpose, although that will surely depend on what implementation of the Set interface you use. I can't imagine why a non-sorted collection (such as Set) would need to use the compareTo() method of it's objects. Indeed, it's contents will not necessarily implement the Comparable interface, hance the ClassCastException if you attempt to insert two objects that are not mutually comparable into a SortedSet, for example.

If you use a hash-based implementation, e.g. HashSet, I think (but have not checked / tested) that it probably uses equals() on the hashed bucket found from using hashCode(). If you have overridden equals() but not hashCode() there is a problem that the lookup method will be checking in the wrong hash bucket for your value. This is the reason for the Object.hashCode() contract that you must always override hashCode() when you override equals().

I think that the reason for using the hash code in these implementations is performance, and that you could logically use only the equals() method for a Set implementation - probably functionally OK but may have performance problems (of the same sort as if you choose a dumb implementation of hashCode() - e.g. always returns the same result for all objects of a given type which satisfies the contract but defeats the point).
[ September 25, 2006: Message edited by: Simon Baker ]
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257


See the String java source code.

Anuj Singhal

Joined: Oct 17, 2003
Posts: 25
TreeSet and TreeMap uses compareTo to check duplicate elements.
They do not use equals and hashcode method.

check this code:-

compareTo called
compareTo called
compareTo called
Set: [1, 3]

I agree. Here's the link:
subject: why CompareTo exists when we already have equals method
It's not a secret anymore!