以下是中的哈希函數的源代碼java.util.HashMap。這些評論很好地解釋了它的成就。但是如何?-什么是^和>>>運營商在做什么?有人可以解釋的代碼實際上是如何做的評論有什么說的?/** * Applies a supplemental hash function to a given hashCode, which * defends against poor quality hash functions. This is critical * because HashMap uses power-of-two length hash tables, that * otherwise encounter collisions for hashCodes that do not differ * in lower bits. Note: Null keys always map to hash 0, thus index 0. */static int hash(int h) { // This function ensures that hashCodes that differ only by // constant multiples at each bit position have a bounded // number of collisions (approximately 8 at default load factor). h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4);}
3 回答

繁華開滿天機
TA貢獻1816條經驗 獲得超4個贊
這是一篇討論整數散列函數及其設計注意事項的文章。它不是很詳細,但是要點是:
操作必須使用一系列計算來實現雪崩。雪崩意味著輸入中的一位差異將使大約1/2的輸出位不同。
基本上,目標是使用補充哈希函數刪除輸入中的任何規則,因為這些規則可能導致哈希表退化。
添加回答
舉報
0/150
提交
取消