我正在尝试使用双散列将字符串键散列到散列表中。我做了类似的事情:
protected int getIndex(String key) {
int itr = 0,
size = this.values.length,
index1,
index2,
index = 0;
do {
// do double hashing to get index for curr [itr] (iteration)
index1 = Math.abs(key.hashCode()) % size;
index2 = size - ((key + key + "#!@").hashCode() % size); # trying very hard to eliminate clash, but still fails ... TA and AT gets index 2 when size = 5
index = (index1 + (itr * index2)) % size;
// if itr > set threshold, exit
itr++;
if (itr > 200) {
index = -1;
break;
}
// once index found, exit loop
} while (index > 0 && this.keys[index] != null && !this.keys[index].equals(key));
return index;
}
主要部分是 . 之后的第 1 3 行do
。我可以说如果我使用Double Hashing,它应该消除碰撞的可能性吗?size
是我的哈希表的唯一键的总可能值