This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

How to implement hashCode()

 
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
 
author
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()".
 
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
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
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!