一、数据结构区别
HashMap 1.7 使用数组+链表
HashMap 1.8 使用Node数组+链表+红黑树(当链表长度>8才会转)
二、扩容区别
HashMap 1.7 扩容:
// 在扩容前会进行判断元素个数是否>=阈值,同时table[bucketIndex]不能为空
if ((size >= threshold) && (null != table[bucketIndex])) {
resize(2 * table.length); //扩容为旧数组的2倍
hash = (null != key) ? hash(key) : 0;
bucketIndex = indexFor(hash, table.length);
}
扩容方法:
void resize(int newCapacity) {
//旧数组
Entry[] oldTable = table;
// 旧数组长度
int oldCapacity = oldTable.length;
//达到最大阈值后直接返回
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return;
}
//扩容后的新数组
Entry[] newTable = new Entry[newCapacity];
//进行元素的转移
transfer(newTable, initHashSeedAsNeeded(newCapacity));
table = newTable;
// 重新计算阈值
threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}
元素转移(此方法在多线程扩容时,会产生循环链表):
void transfer(Entry[] newTable, boolean rehash) {
// 新数组大小
int newCapacity = newTable.length;
// 遍历整个旧数组
for (Entry<K,V> e : table) {
// 遍历数组中具体的某一个元素
while(null != e) {
Entry<K,V> next = e.next;
if (rehash) {
e.hash = null == e.key ? 0 : hash(e.key);
}
// 获取下标
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next; // 使用头插法
}
}
}
HashMap 1.8 扩容:
在1.8扩容时,只要判断元素个数是否>= 阈值
final Node<K,V>[] resize() {
// 旧数组
Node<K,V>[] oldTab = table;
// 旧数组长度
int oldCap = (oldTab == null) ? 0 : oldTab.length;
// 旧阈值
int oldThr = threshold;
// 新数组长度、新数组阈值
int newCap, newThr = 0;
// 旧数组大小>0
if (oldCap > 0) {
// 旧数组大小达到最大容量后就给阈值赋予最大值
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
} // 扩容后的长度不大于最大的阈值并且旧数组的长度大于等于默认容量
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold // 新阈值也扩大两倍
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;// 初始化新数组大小,这个是用户指定了阈值,但是没有给数组长度
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY; // 初始化操作
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {// 计算新的阈值
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
if (oldTab != null) { // 旧数组不为空,开始转移元素
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
if (e.next == null) // 当前下标只有一个元素,直接转移到新数组
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode) // 当前元素是树结构,就执行树形扩容
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) { // 将链表拆分为高低两个链表,然后再去转移
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);// 遍历整个链表
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead; // 低位链表转移
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead; // 高位链表转移
}
}
}
}
}
return newTab;
}
三、get()区别
1.8:
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k)))) // 总是检查第一个元素是否符合
return first;
if ((e = first.next) != null) { // 遍历整个链表
if (first instanceof TreeNode) // 如果是树形,就遍历整个树
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e; // 找到元素,返回出去
} while ((e = e.next) != null);
}
}
return null;
}
1.7:
final Entry<K,V> getEntry(Object key) {
if (size == 0) {
return null;
}
// key==null hash=0
int hash = (key == null) ? 0 : hash(key);
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e; // 遍历链表进行取值
}
return null;
}