Java HashMap的hash算法

HashMap 中hash table 定位算法: 
Java代码   收藏代码
  1. int hash = hash(key.hashCode());  
  2. int i = indexFor(hash, table.length);  

其中indexFor和hash源码如下: 
Java代码   收藏代码
  1. /** 
  2.  * Applies a supplemental hash function to a given hashCode, which 
  3.  * defends against poor quality hash functions.  This is critical 
  4.  * because HashMap uses power-of-two length hash tables, that 
  5.  * otherwise encounter collisions for hashCodes that do not differ 
  6.  * in lower bits. Note: Null keys always map to hash 0, thus index 0. 
  7.  */  
  8. static int hash(int h) {  
  9.     // This function ensures that hashCodes that differ only by  
  10.     // constant multiples at each bit position have a bounded  
  11.     // number of collisions (approximately 8 at default load factor).  
  12.     h ^= (h >>> 20) ^ (h >>> 12);  
  13.     return h ^ (h >>> 7) ^ (h >>> 4);  
  14. }  
  15.   
  16. /** 
  17.  * Returns index for hash code h. 
  18.  */  
  19. static int indexFor(int h, int length) {  
  20.     return h & (length-1);  
  21. }  

indexFor这个方法论坛中已有人分析过,这里就不再分析。 
现在分析一下hash算法: 
Java代码   收藏代码
  1. h ^= (h >>> 20) ^ (h >>> 12);  
  2. return h ^ (h >>> 7) ^ (h >>> 4);  

假设key.hashCode()的值为:0x7FFFFFFF,table.length为默认值16。 
上面算法执行如下: 

 

得到i=15 

其中h^(h>>>7)^(h>>>4) 结果中的位运行标识是把h>>>7 换成 h>>>8来看。 

即最后h^(h>>>8)^(h>>>4) 运算后hashCode值每位数值如下: 
8=8 
7=7^8 
6=6^7^8 
5=5^8^7^6 
4=4^7^6^5^8 
3=3^8^6^5^8^4^7 
2=2^7^5^4^7^3^8^6 
1=1^6^4^3^8^6^2^7^5 
结果中的1、2、3三位出现重复位^运算 
3=3^8^6^5^8^4^7     ->   3^6^5^4^7 
2=2^7^5^4^7^3^8^6   ->   2^5^4^3^8^6 
1=1^6^4^3^8^6^2^7^5 ->   1^4^3^8^2^7^5 

算法中是采用(h>>>7)而不是(h>>>8)的算法,应该是考虑1、2、3三位出现重复位^运算的情况。使得最低位上原hashCode的8位都参与了^运算,所以在table.length为默认值16的情况下面,hashCode任意位的变化基本都能反应到最终hash table 定位算法中,这种情况下只有原hashCode第3位高1位变化不会反应到结果中,即:0x7FFFF7FF的i=15。 


以上看起来比较晦涩难懂,期待高手来用通俗语言解答。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值