一、HashMap概述
在JDK1.8之前,HashMap采用數組+鏈表實現,即使用鏈表處理沖突,同一hash值的鏈表都存儲在一個鏈表裡。但是當位於一個桶中的元素較多,即hash值相等的元素較多時,通過key值依次查找的效率較低。而JDK1.8中,HashMap采用數組+鏈表+紅黑樹實現,當鏈表長度超過阈值(8)時,將鏈表轉換為紅黑樹,這樣大大減少了查找時間。
下圖中代表jdk1.8之前的hashmap結構,左邊部分即代表哈希表,也稱為哈希數組,數組的每個元素都是一個單鏈表的頭節點,鏈表是用來解決沖突的,如果不同的key映射到了數組的同一位置處,就將其放入單鏈表中。(此圖借用網上的圖)
圖一、jdk1.8之前hashmap結構圖
jdk1.8之前的hashmap都采用上圖的結構,都是基於一個數組和多個單鏈表,hash值沖突的時候,就將對應節點以鏈表的形式存儲。如果在一個鏈表中查找其中一個節點時,將會花費O(n)的查找時間,會有很大的性能損失。到了jdk1.8,當同一個hash值的節點數不小於8時,不再采用單鏈表形式存儲,而是采用紅黑樹,如下圖所示(此圖是借用的圖)
圖二、jdk1.8 hashmap結構圖
二、重要的field
//table就是存儲Node類的數組,就是對應上圖中左邊那一欄,
/**
* The table, initialized on first use, and resized as
* necessary. When allocated, length is always a power of two.
* (We also tolerate length zero in some operations to allow
* bootstrapping mechanics that are currently not needed.)
*/
transient Node<K,V>[] table;
/**
* The number of key-value mappings contained in this map.
* 記錄hashmap中存儲鍵-值對的數量
*/
transient int size;
/**
* hashmap結構被改變的次數,fail-fast機制
*/
transient int modCount;
/**
* The next size value at which to resize (capacity * load factor).
* 擴容的門限值,當size大於這個值時,table數組進行擴容
*/
int threshold;
/**
* The load factor for the hash table.
*
*/
float loadFactor;
/**
* The default initial capacity - MUST be a power of two.
* 默認初始化數組大小為16
*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
/**
* The maximum capacity, used if a higher value is implicitly specified
* by either of the constructors with arguments.
* MUST be a power of two <= 1<<30.
*/
static final int MAXIMUM_CAPACITY = 1 << 30;
/**
* The load factor used when none specified in constructor.
* 默認裝載因子,
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;
/**
* The bin count threshold for using a tree rather than list for a
* bin. Bins are converted to trees when adding an element to a
* bin with at least this many nodes. The value must be greater
* than 2 and should be at least 8 to mesh with assumptions in
* tree removal about conversion back to plain bins upon
* shrinkage.
* 這是鏈表的最大長度,當大於這個長度時,鏈表轉化為紅黑樹
*/
static final int TREEIFY_THRESHOLD = 8;
/**
* The bin count threshold for untreeifying a (split) bin during a
* resize operation. Should be less than TREEIFY_THRESHOLD, and at
* most 6 to mesh with shrinkage detection under removal.
*/
static final int UNTREEIFY_THRESHOLD = 6;
/**
* The smallest table capacity for which bins may be treeified.
* (Otherwise the table is resized if too many nodes in a bin.)
* Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts
* between resizing and treeification thresholds.
*/
static final int MIN_TREEIFY_CAPACITY = 64;
三、構造函數
//可以自己指定初始容量和裝載因子
public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
this.loadFactor = loadFactor;
//重新定義了擴容的門限
this.threshold = tableSizeFor(initialCapacity);
}
/**
* Returns a power of two size for the given target capacity.
*/
static final int tableSizeFor(int cap) {
int n = cap - 1;
//先移位再或運算,最終保證返回值是2的整數冪
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}
/**
* Constructs an empty <tt>HashMap</tt> with the specified initial
* capacity and the default load factor (0.75).
*
* @param initialCapacity the initial capacity.
* @throws IllegalArgumentException if the initial capacity is negative.
*/
//當知道所要構建的數據容量的大小時,最好直接指定大小,提高效率
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
/**
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
* (16) and the default load factor (0.75).
*/
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
//將map直接放入hashmap中
public HashMap(Map<? extends K, ? extends V> m) {
this.loadFactor = DEFAULT_LOAD_FACTOR;
putMapEntries(m, false);
}
final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
int s = m.size();
if (s > 0) {
if (table == null) { // pre-size
float ft = ((float)s / loadFactor) + 1.0F;
int t = ((ft < (float)MAXIMUM_CAPACITY) ?
(int)ft : MAXIMUM_CAPACITY);
if (t > threshold)
threshold = tableSizeFor(t);
}
else if (s > threshold)
resize();
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
K key = e.getKey();
V value = e.getValue();
putVal(hash(key), key, value, false, evict);
}
}
}
/**
* Basic hash bin node, used for most entries. (See below for
* TreeNode subclass, and in LinkedMyHashMap for its Entry subclass.)
*/
在hashMap的結構圖中,hash數組就是用Node型數組實現的,許多Node類通過next組成鏈表,key、value實際存儲在Node內部類中。
public static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
public final K getKey() { return key; }
public final V getValue() { return value; }
public final String toString() { return key + "=" + value; }
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
public final boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
if (Objects.equals(key, e.getKey()) &&
Objects.equals(value, e.getValue()))
return true;
}
return false;
}
}
四、重要的方法分析
1.put方法
/**
* Associates the specified value with the specified key in thismap.
* If the map previously contained a mapping for the key, the old
* value is replaced.
*
*/
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
static final int hash(Object key) {
int h;
//key的值為null時,hash值返回0,對應的table數組中的位置是0
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
/**
* Implements Map.put and related methods
*
* @param hash hash for key
* @param key the key
* @param value the value to put
* @param onlyIfAbsent if true, don't change existing value
* @param evict if false, the table is in creation mode.
* @return previous value, or null if none
*/
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//先將table賦給tab,判斷table是否為null或大小為0,若為真,就調用resize()初始化
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//通過i = (n - 1) & hash得到table中的index值,若為null,則直接添加一個newNode
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
//執行到這裡,說明發生碰撞,即tab[i]不為空,需要組成單鏈表或紅黑樹
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
//此時p指的是table[i]中存儲的那個Node,如果待插入的節點中hash值和key值在p中已經存在,則將p賦給e
e = p;
//如果table數組中node類的hash、key的值與將要插入的Node的hash、key不吻合,就需要在這個node節點鏈表或者樹節點中查找。
else if (p instanceof TreeNode)
//當p屬於紅黑樹結構時,則按照紅黑樹方式插入
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
//到這裡說明碰撞的節點以單鏈表形式存儲,for循環用來使單鏈表依次向後查找
for (int binCount = 0; ; ++binCount) {
//將p的下一個節點賦給e,如果為null,創建一個新節點賦給p的下一個節點
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
//如果沖突節點達到8個,調用treeifyBin(tab, hash),這個treeifyBin首先回去判斷當前hash表的長度,如果不足64的話,實際上就只進行resize,擴容table,如果已經達到64,那麼才會將沖突項存儲結構改為紅黑樹。
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//如果有相同的hash和key,則退出循環
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;//將p調整為下一個節點
}
}
//若e不為null,表示已經存在與待插入節點hash、key相同的節點,hashmap後插入的key值對應的value會覆蓋以前相同key值對應的value值,就是下面這塊代碼實現的
if (e != null) { // existing mapping for key
V oldValue = e.value;
//判斷是否修改已插入節點的value
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;//插入新節點後,hashmap的結構調整次數+1
if (++size > threshold)
resize();//HashMap中節點數+1,如果大於threshold,那麼要進行一次擴容
afterNodeInsertion(evict);
return null;
}
2.擴容函數resize()分析
/**
* Initializes or doubles table size. If null, allocates in
* accord with initial capacity target held in field threshold.
* Otherwise, because we are using power-of-two expansion, the
* elements from each bin must either stay at same index, or move
* with a power of two offset in the new table.
*
* @return the table
*/
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;//定義臨時Node數組型變量,作為hash table
//讀取hash table的長度
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;//讀取擴容門限
int newCap, newThr = 0;//初始化新的table長度和門限值
if (oldCap > 0) {
//執行到這裡,說明table已經初始化
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
//用構造器初始化了門限值,將門限值直接賦給新table容量
newCap = oldThr;
else {
// zero initial threshold signifies using defaults
//老的table容量和門限值都為0,初始化新容量,新門限值,在調用hashmap()方式構造容器時,就采用這種方式初始化
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {
//如果門限值為0,重新設置門限
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;//更新新門限值為threshold
@SuppressWarnings({"rawtypes","unchecked"})
//初始化新的table數組
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
//當原來的table不為null時,需要將table[i]中的節點遷移
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
//取出鏈表中第一個節點保存,若不為null,繼續下面操作
if ((e = oldTab[j]) != null) {
oldTab[j] = null;//主動釋放
if (e.next == null)
//鏈表中只有一個節點,沒有後續節點,則直接重新計算在新table中的index,並將此節點存儲到新table對應的index位置處
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode)
//若e是紅黑樹節點,則按紅黑樹移動
((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 {
//下面這段暫時沒有太明白,通過e.hash & oldCap將鏈表分為兩隊,參考知乎上的一段解釋
/**
* 把鏈表上的鍵值對按hash值分成lo和hi兩串,lo串的新索引位置與原先相同[原先位
* j],hi串的新索引位置為[原先位置j+oldCap];
* 鏈表的鍵值對加入lo還是hi串取決於 判斷條件if ((e.hash & oldCap) == 0),因為* capacity是2的冪,所以oldCap為10...0的二進制形式,若判斷條件為真,意味著
* oldCap為1的那位對應的hash位為0,對新索引的計算沒有影響(新索引
* =hash&(newCap-*1),newCap=oldCap<<2);若判斷條件為假,則 oldCap為1的那位* 對應的hash位為1,
* 即新索引=hash&( newCap-1 )= hash&( (oldCap<<2) - 1),相當於多了10...0,
* 即 oldCap
* 例子:
* 舊容量=16,二進制10000;新容量=32,二進制100000
* 舊索引的計算:
* hash = xxxx xxxx xxxy xxxx
* 舊容量-1 1111
* &運算 xxxx
* 新索引的計算:
* hash = xxxx xxxx xxxy xxxx
* 新容量-1 1 1111
* &運算 y xxxx
* 新索引 = 舊索引 + y0000,若判斷條件為真,則y=0(lo串索引不變),否則y=1(hi串
* 索引=舊索引+舊容量10000)
*/
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;
}
3.get方法
/**
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
*
* <p>More formally, if this map contains a mapping from a key
* {@code k} to a value {@code v} such that {@code (key==null ? k==null :
* key.equals(k))}, then this method returns {@code v}; otherwise
* it returns {@code null}. (There can be at most one such mapping.)
*
* <p>A return value of {@code null} does not <i>necessarily</i>
* indicate that the map contains no mapping for the key; it's also
* possible that the map explicitly maps the key to {@code null}.
* The {@link #containsKey containsKey} operation may be used to
* distinguish these two cases.
*
* @see #put(Object, Object)
*/
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
/**
* Implements Map.get and related methods
*
* @param hash hash for key
* @param key the key
* @return the node, or null if none
*/
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;
}
/**
* Returns <tt>true</tt> if this map contains a mapping for the
* specified key.
*
* @param key The key whose presence in this map is to be tested
* @return <tt>true</tt> if this map contains a mapping for the specified
* key.
*/
public boolean containsKey(Object key) {
return getNode(hash(key), key) != null;
}
4.紅黑樹
/**
* Entry for Tree bins. Extends LinkedMyHashMap.Entry (which in turn
* extends Node) so can be used as extension of either regular or
* linked node.
*/
static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {
TreeNode<K,V> parent; // red-black tree links
TreeNode<K,V> left;
TreeNode<K,V> right;
TreeNode<K,V> prev; // needed to unlink next upon deletion
boolean red;
TreeNode(int hash, K key, V val, Node<K,V> next) {
super(hash, key, val, next);
}
//紅黑樹暫時還沒有仔細研究,紅黑樹相關的增刪改查操作後期再認真分析。
五、總結
仔細分析hashmap源碼後,可以掌握很多常用的數據結構的用法。本次筆記只是記錄了hashmap幾個常用的方法,像紅黑樹、迭代器等還沒有仔細研究,後面有時間會認真分析。