• 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

Object hashCode ?

 
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why we need hashCode() in java Object class ?

Why we need to override equals() when overriding hashCode() ?

Thanks.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Comes down to the algorithm of collections.
HashCode determines where objects are located (two identical hashcodes do not necessarily mean 2 equal objects), and equals determines equality between those objects.
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why we need to override equals() when overriding hashCode() ?



Not always. this is not part of Contract. Though its advised to do so.

The contract is "Always override hashCode when you override equals"
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sebastian Janisch wrote:Comes down to the algorithm of collections.
HashCode determines where objects are located (two identical hashcodes do not necessarily mean 2 equal objects), and equals determines equality between those objects.



Thanks for your reply.

as you said, "two identical hashcodes do not necessarily mean 2 equal objects". then could we say, two different hashcodes definitely mean two not-equal object ?

Do you mean, hashcode in Object is ONLY for algorithm of collection ? then too much waste, because we NOT always use collections.

Thanks.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Chen wrote:then too much waste,



Waste? Of what?
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:

Edward Chen wrote:then too much waste,



Waste? Of what?


My meaning is , hashCode is defined in Object, the top level class, so it is a waste. If hashcode is only use for collection, then we need to implement hashcode when we need collection.

Thanks.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If hashcode is only use for collection, then we need to implement hashcode when we need collection.



As a writer of the data class, sometimes you can't control whether your class will be used in a collection. Some people do put instances of Object in a hashing collection.

Henry
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

If hashcode is only use for collection, then we need to implement hashcode when we need collection.



As a writer of the data class, sometimes you can't control whether your class will be used in a collection. Some people do put instances of Object in a hashing collection.

Henry



Yes, even if you do not design your classes for collection or do not plan for it, someone might do it, and if object had no hashCode and equals, evil things would happen.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, I ask, waste of what? Electrons?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He probably thinks that having functionality that is only needed in special cases is unnecessary overhead. ..
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because many collections store data assigned to the type "Object" and require hashCode and equals methods to deal with those data and Objects, the hashCode and equals methods must be declared in the Object class.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm taking up a collection of Free Electrons. Give generously.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In item 9 of his book "Effective Java" Joshua Bloch states that failure to override hashCode in a class that overrides equals will prevent that class from working correctly when it is used in hash-based collections like HashMap, HashSet and Hashtable.

He also indicates that when you fail to override hashCode you are breaking the following Object contract: equal objects must have equal hash codes. A consequence of this the inability of Map<K,V>.get(Object key) to work properly because, under certain conditions, the get method will not check for object equality for objects that have different hashCodes.

For a thorough treatment I highly recommend reading Item 9 "Always override hashCode when you override equals" of Effective Java.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic