Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why CompareTo exists when we already have equals method

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why java String class has got CompareTo method when it has already got equals method?
 
fred Joly
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you notice that String is implementing Comparable ?
 
Anuj Singhal
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

See the String java source code.

 
Anuj Singhal
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic