1 容量總是2次冪
/**
* Returns a power of two size for the given target capacity.
*/
static final int tableSizeFor(int cap) {
int n = cap - 1;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}
2 散列函數的設計會同時使用hashcode的高位和低位特性
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16) ;
}
3 查找index位置 a%b=a&(b-1)
int index = hash(key) &(capacity-1)
4 衝突解決方法是:鏈表法。在同一列表中元素個數大於8 ,使用紅黑樹。