I am looking for a hash function for an integer array containing about 17 integers each. There are about 1000 items in the HashMap and I want the computation to be as fast as possible.
I am now kind of confused by so many hash functions to choose and I notice that most of them are designed for strings with different characters. So is there a hash function designed for strings with only numbers and quick to run?
Thanks for your patience!
You did not specify any requirements (except speed of calculation), but take a look at java.util.Arrays#hashCode. It should be fast, too, just iterating once over the array and combining the elements in an
int
calculation.Actually, no!
You could technically use
int[]
as a key in aHashMap
in Java (you can use any kind ofObject
), but that won't work well, as arrays don't define a usefulhashCode
method (or a usefulequals
method). So the key will use object identity. Two arrays with identical content will be considered to be different from each-other.You could use
List<Integer>
, which does implementhashCode
andequals
. But keep in mind that you must not mutate the list after setting it as a key. That would break the hashtable.