wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Object hashCode ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object hashCode ?" Watch "Object hashCode ?" New topic
Author

Object hashCode ?

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
why we need hashCode() in java Object class ?

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

Thanks.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
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.


JDBCSupport - An easy to use, light-weight JDBC framework -
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


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

Joined: Dec 23, 2003
Posts: 798
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

Edward Chen wrote:then too much waste,


Waste? Of what?


[Jess in Action][AskingGoodQuestions]
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
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
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

Again, I ask, waste of what? Electrons?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
He probably thinks that having functionality that is only needed in special cases is unnecessary overhead. ..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
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

Joined: Sep 29, 2008
Posts: 12617

I'm taking up a collection of Free Electrons. Give generously.
Lucas Alberione
Greenhorn

Joined: Aug 15, 2009
Posts: 8
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.


Lucas
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object hashCode ?