final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
HashMap的putVal 方法,為什么還要單獨新建個區域變數Node<K,V>[] tab ,tab = table.直接操作table不行嗎
uj5u.com熱心網友回復:
是發生了擴容就需要新建一個吧uj5u.com熱心網友回復:
table是成員變數,tab=table,resize()方法回傳的新的Node<K,V>[]也只是改變后的成員變數table.參考的地址都是一樣的.
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
就有點像是 e和p的問題, e和p只是不同的物件,e是p的淺拷貝,參考的地址是不變的.e改變,p也會跟著改變.為什么還要再新建個e.直接操作p不行了.繞的人頭暈.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/274267.html
標籤:Java相關
上一篇:Java相關知識,面試參考系列
下一篇:微服務里呼叫檔案上傳服務總是超時
