wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Author

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?


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
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é
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Did you notice that String is implementing Comparable ?


[My Blog]
All roads lead to JavaRanch
Anuj Singhal
Greenhorn

Joined: Oct 17, 2003
Posts: 23
Hi,
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?

Thanks,
Anuj
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

Hello,

See the String java source code.

Anuj Singhal
Greenhorn

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

check this code:-





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

Thanks,
Anuj
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why CompareTo exists when we already have equals method
 
Similar Threads
Difference between Comparable and Comparator
Problem with Date Object
help with loop
Overriding the equals ( ) method
hashcode() and equals()