• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to implement hashCode()

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Screaming fools! It's nothing more than a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic