• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Jason Bloch's hashCode formula

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the book Effective Java, Jason Bloch proposed this hashCode formula (here shown for strings). What is the significance of the magic numbers 17 and 37 ? Is some number theory math wizardry involved ?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
It's Joshua Bloch!
Other than their being prime numbers, there's no special significance to them; they're just values that empirically work well.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi! just wondering what
this.value and this.value.hashCode() is...
 
Gavin Bong
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
value and code are the two member variables in my class, from which this code extract was taken from.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I try to use the formula below to implement the hashCode() of my class, say ClassA.
There are 6 String fields to be compared in the equals() and hence my hashCode() will include these 6 fields.
On using this formula, the calculated hash code is very large, sometimes it is negative, say -1342574843. I guess it is because the value is bigger than a int can contain. My question is: is there any harm about this? How come there is a negative sign? Thx.
public int hashCode(){
int result = 17;
result = 37*result + ((this.value != null) ? this.value.hashCode() : 0);
result = 37*result + ((this.code != null) ? this.code.hashCode() : 0);
return result;
}
 
lowercase baba
Posts: 13056
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as i understand it, you are basically creating a bit pattern as your hash key. for some strings, the top bit gets set to 1. this makes it negative. it shouldn't hurt anything.
i think...
reply
    Bookmark Topic Watch Topic
  • New Topic