my dog learned polymorphism
The moose likes Java in General and the fly likes compund key algorithm for hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "compund key algorithm for hashmap" Watch "compund key algorithm for hashmap" New topic

compund key algorithm for hashmap

Jagdeep Sharma
Ranch Hand

Joined: May 24, 2010
Posts: 121


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

Joined: Aug 06, 2009
Posts: 33
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.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45263
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:
subject: compund key algorithm for hashmap
jQuery in Action, 3rd edition