每當我嘗試生成一個索引值時,我在一個索引處得到許多衝突。在一個指標300和第二個最高的碰撞是10。我認爲的問題是,如果單詞的長度爲1,2,3。他們通常導致較小的權力,因此產生一個較小的數字。導致它被添加到數量較少的桶中。有沒有多功能這不會發生?或者你能幫我解決這個問題嗎?多項式哈希函數java?
public int GetHashCompress(String str){
int ply=0;
double mathIt=0;
int size = str.length();
for(int j = 0 ; j< size ; j++){
double x0 = (double) str.charAt(j);
double firstStep = (int) Math.pow(31, (size-j))*x0;
mathIt = mathIt + firstStep ; // hash function +1 to increance the range to keep 33 to the power >1
}
//arrayOfRawHash.add(mathIt); // this is for testing it later
ply =(int) mathIt %numBucket;// this is where it is compressed
return ply;
}
'String#hashCode()'怎麼樣?或者如果你想要更好的散列,MD5,SHA1,..? – zapl