• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compund key algorithm for hashmap

 
Jagdeep Sharma
Ranch Hand
Posts: 121
IBM DB2 Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

I need a compound key algorithm so that i can use it in hashmap. This compound key is to be constructed from three values INT,INT,STRING.

INT can have any value from 1 to infinity
STRING will have only two values DOMESTIC or OVERSEAS.


Can you people suggest me some algorithm to generate unique hashcode.


Thanks in advance
 
Amit ChaudhariC
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remember reading in Effective Java about generating a unique hashcode.
i can vaguely recall it using 17 and 31 as some numbers since they are prime and then using your instance field values,it computes a unique code.

Regards,
Amit
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Domestic or Overseas sounds to me more like an enum than a String. You would have to create a class which encapsulates those three values (if you really want to go to ∞ you can't use an int; try BigInteger instead), and calculate its hash code. There is no such thing as a unique hash code; the possible range of values in a 7-letter String containing only the 26 letters used in English is greater than 2 to the 32nd.
Remember each of those objects returns a hash code; you can use their hashCode methods. You can download an old edition of Thinking in Java™ here. There are details about hash codes in chapter 13.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic