• 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

hashCode with long

 
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I have the following properties of an Object:



My question is with reference to hashCode. I would like to know what is the best why to create a hashCode for this class. Obviously, the ID is what makes the empl unique but it's type is long, so.... would this make a good hash:



???
thanks for any thoughts
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joshua Bloch, in Item 8 of his book Effective Java Programming Language Guide, suggests an algorithm for calculating a hashcode.

For a class with a single long field for a key, the resulting code would look like this:



where f is your long value, c1 is an arbitrary prime number and c2 is a non-zero number.

He's XOR'ing the upper 32 bits with the lower 32 bits.

If you haven't already, you should read Joshua's book. It's full of good advice.
[ December 06, 2006: Message edited by: Scott Johnson ]
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cool! I'll use it.

thanks Scott
reply
    Bookmark Topic Watch Topic
  • New Topic