【发布时间】:2014-09-24 19:47:01
【问题描述】:
我关心的是检查 Java HashMap 如何为键获取相同的索引。即使它的大小从默认的 16 扩展到更高的值,因为我们不断添加条目。
我尝试复现HashMap的索引算法。
int length=1<<5;
int v=6546546;
int h = new Integer(v).hashCode();
h =h^( (h >>> 20) ^ (h >>> 12));
h=h ^ h >>> 7 ^ h >>> 4;
System.out.println("index :: " + (h & (length-1)));
我针对不同的“长度”值运行了我的代码。 因此,对于相同的键,我得到不同的索引,因为 HashMap 的长度发生了变化。我在这里想念什么?
我的结果:
length=1<<5;
index :: 10
length=1<<15;
index :: 7082
length=1<<30;
index :: 6626218
【问题讨论】: