HashMap实现原理:容量、负载因子、hash与定位都搞定了吗?( 四 )
hashCode方法
注意key.hashCode()的多态用法 。 重点是hash方法 。
hash方法的实现和定位
前面已经说过 , 在get和put计算下标时 , 先对hashCode进行hash操作 , 然后再通过hash值进一步计算下标 , 如下图所示:
回顾hash方法的源码可知 , hash方法大概的作用就是:高16bit不变 , 低16bit和高16bit做了一个异或 。
javadoc这样说:
Computes key.hashCode() and spreads (XORs) higher bits of hash to lower. Because the table uses power-of-two masking sets of hashes that vary only in bits above the current mask will always collide. (Among known examples are sets of Float keys holding consecutive whole numbers in small tables.) So we apply a transform that spreads the impact of higher bits downward. There is a tradeoff between speed utility and quality of bit-spreading. Because many common sets of hashes are already reasonably distributed (so don’t benefit from spreading) and because we use trees to handle large sets of collisions in bins we just XOR some shifted bits in the cheapest possible way to reduce systematic lossage as well as to incorporate impact of the highest bits that would otherwise never be used in index calculations because of table bounds.
推荐阅读
- 左慈|三国杀:技能缝接,超强发挥,用左慈就能实现
- 商人|游戏商人的月入百万是真的吗?揭秘真实的剑三游戏商人真实现状
- fly|从电竞迈向艺术:Fly选手实现人生跨越,成历史第一人!
- 赤色要塞|玩经典游戏《赤色要塞》魔改版:吉普车也能用激光炮,童年梦想实现了
- 手机游戏|梦幻西游手游:封系统治擂台?浅谈经脉学问,方寸也能实现双封!
- 魂斗罗|“为什么魂斗罗内存这么少,却可以实现那么长的剧情?”
- rw战队|KPL前瞻:Hero对阵RW侠首发公布,黑鲸的Flag真有可能实现
- 单机|《仙剑奇侠传七》逆风翻盘!蒙尘的老牌IP,终于实现了自我救赎
- LGD|Ti10:LGD实现逆天翻盘锁定总决赛,虎牙Sccc激动到泪目
- 美好世界|“我们的工作只是实现人类的愿望,而不是创造一个我们认为的《美好世界》”