1. 程式人生 > >JDK原始碼-HashMap-resize擴容方法(JDK7和JDK8)

JDK原始碼-HashMap-resize擴容方法(JDK7和JDK8)

擴容操作可以認為是HashMap原始碼中最複雜的
resize方法的不是public的,不能通過client觸發,所以只能直接進入原始碼檢視

JDK 7

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]
; boolean oldAltHashing = useAltHashing; useAltHashing |= sun.misc.VM.isBooted() && (newCapacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD); boolean rehash = oldAltHashing ^ useAltHashing;//判斷是否需要對原node重新hash定位table的index transfer(newTable, rehash); //擴容核心方法 table = newTable; threshold =
(int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1); }

進入 transfer 方法

void transfer(Entry[] newTable, boolean rehash) {
	int newCapacity = newTable.length;
	for (Entry<K,V> e : table) {//直接遍歷table變數
		//連結串列跟table[i]斷裂遍歷,頭部往後遍歷插入到newTable中
		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; } } }

擴容過程總結:
建立一個新的陣列newTable,容量是oldTable的一倍
遍歷oldTable,拿到每個連結串列
遍歷連結串列,頭插法插入newTable

JDK 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;
	//一系列校驗
	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; //擴容閾值也翻倍
	}
	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) {//開始遍歷oldTable
			Node<K,V> e;
			if ((e = oldTab[j]) != null) {
				oldTab[j] = null;
				if (e.next == null) //table[i]沒值
					newTab[e.hash & (newCap - 1)] = e;
				else if (e instanceof TreeNode) //table[i]是紅黑樹的結構
					((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
				else { //table[i]是連結串列的結構,擴容後還是保持順序的
					Node<K,V> loHead = null, loTail = null; //“低連結串列”的頭和尾,即擴容後的在new table的index和old index是一樣的
					Node<K,V> hiHead = null, hiTail = null; //“高連結串列”的頭和尾,即擴容後的在new table的index是old index的2倍
					Node<K,V> next;
					do {
						next = e.next;
						if ((e.hash & oldCap) == 0) {//newTable index ==  oldTable index
							if (loTail == null)
								loHead = e;//頭只當初始時才賦值
							else
								loTail.next = e;//新插入的都到新增到tail中
							loTail = e;
						}
						else {//newTable index ==  oldTable index * 2
							if (hiTail == null)
								hiHead = e;//頭只當初始時才賦值
							else
								hiTail.next = e;//新插入的都到新增到tail中
							hiTail = e;
						}
					} while ((e = next) != null);
					if (loTail != null) { //將“低連結串列”新增到newTable中
						loTail.next = null;
						newTab[j] = loHead;
					}
					if (hiTail != null) {//將“高連結串列”新增到newTable中
						hiTail.next = null;
						newTab[j + oldCap] = hiHead;
					}
				}
			}
		}
	}
	return newTab;
}

總結(先針對連結串列操作的總結,紅黑樹的後續文章會寫):
如果是連結串列處理(紅黑樹的後續文章討論),那麼就將oldTable[i]分成高低連結串列,生成後插入到newTable中

對比

JDK7是每拿到一個Node就直接插入到newTable,而JDK8是先插入到高低連結串列中,然後再一次性插入到newTable。
所以連結串列的擴容過程JDK7會出現死迴圈問題,而JDK8避免了這個問題。
JDK8跟原先的連結串列對比Node之間順序是一致的,而JDK7是是反過來的。