• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overriding Hash()  RSS feed

 
Chinna Karuppan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IF you are overriding the equals method in object and pushing that object into a hash map using put.
I am not able to get it back by creating a object with the same id I have used for comparision in equals method.
i read(red) you should also override hash function.why should i override the hash function when I have standard hash function which
is the address of the object to hash into the proper place.
I think get is a combination of hash function to get to the proper place and then comparing it with the equals method.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a HashMap/Set stores or retrieves an object, it first uses the hashCode() value of the key/object to locate approximately where the object should be stoerd. Once there, since hashCode() cannot provide a truly unique value for every object, the map/set must compare -- using equals() -- the objects that all share that hashCode() value.

Thus, you must ensure to override both hashCode() and equals(). The only time you can relax this requirement is if you can guarantee that object identity will equate with equality. In other words, if two object references point to different instances, they are unequal.

This would be the case if you provided a registry of singleton values of your objects. Instead of instantiating new objects to hold the desired values, you look up the single instance for your desired values. For example,Now you can be sure that the Color representing black, Color(0, 0, 0), has only one instance in the entire JVM, and thus any two Color references are equal if and only if they point to the same instance. Now you can skip overriding hashCode().

... and equals(), hah!
[ December 22, 2004: Message edited by: David Harkness ]
 
Chinna Karuppan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My contention was the standard hash() method of object would do the hashing and while using it would do that again.so my equals will then come into picture and so it should fine.but it is not.
WHY.
ChinnaKarupan
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

perhaps you want to have a look at the EqualsBuilder and HashCodeBuilder from jakartas common-lang package.


pascal
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This topic is also covered very nicely in Josh Bloch's "Effective Java," which is definitely worth getting.

I'm going to move this to the intermediate forum -- this isn't an advanced topic.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chinna Karuppan:
My contention was the standard hash() method of object would do the hashing and while using it would do that again.so my equals will then come into picture and so it should fine.but it is not.
WHY.
As I said, you must override hashCode() in this case. For example, say you're using the following class as the keys in the HashMap:Now watch what happens when we call hashCode() on Name instances.The output will beOverriding hashCode() as "return name.hashCode()" will fix this.

Note also that you want to make sure you don't do anything to an object already in a HashMap/Set that will change its hashCode() value because the Collection will not know to rehash and move it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!