Map的實現類HashMap的各種方法原始碼解析

2021-09-11 01:56:37 字數 3228 閱讀 2731

// map.put 方法 

public v put(k key, v value)

// putval 方法

final v putval(int hash, k key, v value, boolean onlyifabsent,

boolean evict)

// 判斷 e.hash 是否與 hash 相等 相等說明是 e 節點的put 跳出迴圈 需要執行

// 覆蓋方法

if (e.hash == hash &&

((k = e.key) == key || (key != null && key.equals(k))))

break;

p = e;// 條件都不成立 繼續迴圈}}

if (e != null)

}++modcount;// 操作++

if (++size > threshold)// size ++ 大於擴容閥值 執行擴容方法

resize();// 擴容方法

afternodeinsertion(evict); // 用於linkedhashmap 查其他地方的說明 待驗證

return null;

}// resize 方法 執行擴容

final node resize()

// 定義 newcap = oldcap * 2 並且判斷 newcap < maximum_capacity && oldcap >= 16

else if ((newcap = oldcap << 1) < maximum_capacity &&

oldcap >= default_initial_capacity)

newthr = oldthr << 1; // 成立 定義 newthr 為 oldthr * 2

}else if (oldthr > 0) // oldcap = 0 && oldthr > 0

newcap = oldthr;// 定義 newcap = oldthr

else

if (newthr == 0)

threshold = newthr;// 將new閥值 賦值為threshold

@suppresswarnings()

node newtab = (node)new node[newcap];//執行 newtab 的操作

table = newtab;// 從新定義 table

if (oldtab != null) while()迴圈

// 定義 nextnode

next = e.next;

if ((e.hash & oldcap) == 0)

else

} while ((e = next) != null);

// 找到末尾node 從末尾開始賦值newtab

if (lotail != null)

if (hitail != null) }}

}}

return newtab;// 返回newtab 沒有想到 hashmap.put 方法 如此複雜

}// 紅黑樹方法 實現

static final class treenodeextends linkedhashmap.entry

// 紅黑樹方法 put 實現方式

// ((treenode)p).puttreeval(this, tab, hash, key, value)

final treenodeputtreeval(hashmapmap, node tab,

int h, k k, v v)

// 最終比較key物件的預設hashcode()方法的返回值,因為

// system.identityhashcode(a)呼叫的是物件a預設的hashcode();

dir = tiebreakorder(k, pk);

}treenodexp = p;

if ((p = (dir <= 0) ? p.left : p.right) == null)

}}

// map.get 方法

public v get(object key)

// getnode 方法

final nodegetnode(int hash, object key) while()迴圈查詢

if (e.hash == hash &&

((k = e.key) == key || (key != null && key.equals(k))))

return e;// 一直迴圈下乙個 匹配返回

} while ((e = e.next) != null);}}

return null;// 無匹配資料 返回null

}

// map.remove 方法

public v remove(object key)

// removenode 方法

final noderemovenode(int hash, object key, object value,

boolean matchvalue, boolean movable) while()迴圈查詢

if (e.hash == hash &&

((k = e.key) == key ||

(key != null && key.equals(k))))

p = e;//繼續迴圈下乙個

} while ((e = e.next) != null);}}

// 判斷 node != null matchvalue:false 定義v (目前感覺只要走到這裡 這裡一定是成立

// 的)

if (node != null && (!matchvalue || (v = node.value) == value ||

(value != null && value.equals(v))))

}return null;//返回null

}

// map.clear 方法

public void clear()

}

// hash(key) 方法 比較特殊 需要慢慢理解

static final int hash(object key)

Map實現類的比較

map實現類的比較 hashmap的存入順序和輸出順序無關。key值物件 hashcode和equals方法 linkedhashmap則保留了鍵值對的存入順序,一般不會使用訪問順序。key值物件 hashcode和equals方法 linkedhashmap雖然增加了時間和空間上的開銷,但是通過維...

hash map和map的區別

這裡列幾個常見問題,應該對你理解和使用hash map比較有幫助。4.1 hash map和map的區別在 4.2 什麼時候需要用hash map,什麼時候需要用map?總 體來說,hash map 查詢速度會比map快,而且查詢速度基本和資料量大小無關,屬於常數級別 而map的查詢速度是log n...

map和hash map的比較

hash map和map的區別在 建構函式。hash map需要hash函式,等於函式 map只需要比較函式 小於函式 儲存結構。hash map採用hash表儲存,map一般採用紅黑樹 rb tree 實現。因此其記憶體資料結構是不一樣的。什麼時候需要用hash map,什麼時候需要用map?ha...