HashMap中的indexFor方法分析
分析java7的HashMap源码的时候,其用indexFor来计算entry对象在table数组中的索引值
static int indexFor(int h, int length) {
// assert Integer.bitCount(length) == 1 : "length must be a non-zero power of 2";
return h & (length-1);
}
为什么不是对hash表的长度取余,而使用&进行位运算呢?
/**
* The table, resized as necessary. Length MUST Always be a power of two.
*/
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
/**
* The default