HashMap解析
HashMap解析
HashMap的兩個版本
HashMap在JDK7之后發生了一些改變,所以有兩個版本:
- JDK7下的HashMap(數組+鏈表)
- JDK8下的HashMap(數組+鏈表+紅黑樹)
HashMap(java7版本)
結構解析
Java7的版本下的HashMap如下:
HashMap 里面是一個數組,然后數組中每個元素是一個單向鏈表。并且HashMap 的數組是運行擴容的,也就是說當數組不夠用時,可以通過擴容來擴大數組的容量。HashMap數組中存儲的是Entry 的實例。
Entry實例有一個key值,一個value值,一個hsah值和一個指針(引用)。Entry實例就是我們平時的k-V對了。
構造參數
capacity:當前數組容量,始終保持 2^n。
loadFactor:負載因子,默認為 0.75。
threshold:擴容的閾值,等于 capacity * loadFactor
put過程
源代碼
public V put(K key, V value) {// 當插入第一個元素的時候,需要先初始化數組if (table == EMPTY_TABLE) {inflateTable(threshold);}// 如果 key 為 null,將entry放到table[0]中if (key == null)return putForNullKey(value);// 1. 對 key 的 hash 值int hash = hash(key);// 2. 通過key的hash值映射成數組的下標int i = indexFor(hash, table.length);// 3. 遍歷一下對應下標處的鏈表,看是否有重復的 key 已經存在,for (Entry<K,V> e = table[i]; e != null; e = e.next) {Object k;// 有,則覆蓋,并put 方法返回舊值if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {V oldValue = e.value;e.value = value;e.recordAccess(this);return oldValue;}}modCount++;// 4. 不存在重復的 key,將此 entry 添加到鏈表中addEntry(hash, key, value, i);return null; }get過程
通過key計算hash值
通過值找到數組下標
遍歷數組下標下的鏈表
public V get(Object key) {// key為null遍歷table[0] 處的鏈表if (key == null)return getForNullKey();// 根據key那entry對象Entry<K,V> entry = getEntry(key);return null == entry ? null : entry.getValue(); }HashMap(java8版本)
ava8 對 HashMap 進行了一些修改,所以其由 數組+鏈表+紅黑樹 組成。
結構解析
HashMap (JDK1.8):底層實現是數組+鏈表+紅黑樹,當鏈表中的元素超過了 8 個以后,會將鏈表轉換為紅黑樹。
put過程
public V put(K key, V value) {//put方法調用的是putValreturn putVal(hash(key), key, value, false, true); }//hash:key的hash,key:鍵 value:值 //onlyIfAbsent:放置模式 如果是 true,那么只有在不存在該 key 時才會進行 put 操作 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);//有東西的情況else {Node<K,V> e; K k;//判斷是否重復if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k))))e = p;//再判斷是否為紅黑樹節點,是則通過紅黑樹的方式插入else if (p instanceof TreeNode)e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);//否則跟JDK1.7的鏈表一樣插入else {for (int binCount = 0; ; ++binCount) {if ((e = p.next) == null) {p.next = newNode(hash, key, value, null);//插入新節點后判斷長度是否超過8,超過就將鏈表轉化為紅黑樹if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1sttreeifyBin(tab, hash);break;}if (e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))))break;p = e;}}if (e != null) {V oldValue = e.value;//根據前面的onlyIfAbsent參數覆蓋掉舊值if (!onlyIfAbsent || oldValue == null)e.value = value;afterNodeAccess(e);return oldValue;}}++modCount;if (++size > threshold)resize();afterNodeInsertion(evict);return null; }get過程
public V get(Object key) {Node<K,V> e;return (e = getNode(hash(key), key)) == null ? null : e.value; } 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; }總結