• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why CompareTo exists when we already have equals method

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why java String class has got CompareTo method when it has already got equals method?
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you notice that String is implementing Comparable ?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

See the String java source code.

 
Anuj Singhal
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic