I'm implementing a hashmap to contain all words in a word file (e.g dictionary.txt
, bible.txt
) and I am having a collision problem. I know that there are many good hash functions out there but when I try compressing the hash code using this compression function, the number of collisions raises significantly (I'm using dbj2 for my hash function).
My hashmap basically converts a key to its hash value and compresses that hash value to the index of the entry in the internal hash table, which is an array. It resizes itself to 2 * capacity - 1
if the load factor
of 0.5
is reached. When collisions happen, my hashmap generates new indexes using quadratic probing.
This is what my current compress function looks like:
private int compress(int hashCode) {
return Math.abs(hashCode) % capacity;
}
Is there any (efficient) way I can do to avoid collisions? Changing the structure of the hashmap itself is also accepted.