• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object hashCode ?

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why we need hashCode() in java Object class ?

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

Thanks.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • 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.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21226
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, I ask, waste of what? Electrons?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He probably thinks that having functionality that is only needed in special cases is unnecessary overhead. ..
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • 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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm taking up a collection of Free Electrons. Give generously.
 
Lucas Alberione
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic