Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to implement hashCode()

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to implement hashCode?
You should implement „hashCode“ so that EQUAL objects return the same hashCode.
Therefore I should take the objects instance variable which is compared for equality in equals method
and call the hashCode() to return an int number which is the same for equal objects.
In this example I calculate the hashCode from the string instance variable which is turning the balance
for equality of two objects.
My Question is:
- Is this implementation ok?
- I assume „empNum.hashCode( )“ is neccessary to convert a string in an int which is then returned
as „int hashCode() must return an int value.
 Would it be neccessary to say „empNum.hashCode( )“ if „empNum“ would already be an int instead
of a String?
- How would I calculate a hashCode if equals() method would compare two instance variables „empNum“ and „name“ instead of one?


C:\Java\EigeneJavaProgramme>java Employee_Test
false
74113823
74224158
true
74113823
74113823

Appreciate your answers.
Thomas
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Markl:
- Is this implementation ok?

Yes.

Would it be neccessary to say „empNum.hashCode( )“ if „empNum“ would already be an int instead of a String?

No, it would not be necessary.

- How would I calculate a hashCode if equals() method would compare two instance variables „empNum“ and „name“ instead of one?

You would somehow combine them to get a single int value. A simple solution would be "empNum + name.hashCode()".
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If empNum is an int, you can't say empNum.hashCode(). You can only call hashCode() on objects, not primitives.
 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ilja and Ron,
I am not quite clear about how to override the hashCode() method if „equals()“ compares
two instance variables. Therefore I gave it a try in the code above and overrode hashCode()
this way:

You can see that equals compares two instance variables and hence hashCode()
combines the two instance Varialbes and retrieves an int from the combined strings
by using hashCode().
Did I override hashCode correctly in the code above.
I checked the overridden hashCode() in the sample code (see below) and I think it
Works because it returns the same hashCode for equal objects e1 and e3.

C:\Java\EigeneJavaProgramme>java Employee_Test2
false
74113823
74224158
true
74113823
74113823
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, your implementation is correct.
Notice though, that in this case

would be true, leading to a "collision" in the HashMap/-Set, reducing its performance.
If that is likely to happen, you might want to use a more complicated algorithm, for example
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic