这是一篇关于HashMap的概述和底层原理的介绍。算是网上很多帖子的综合和我自己的一点想法。
HashMap在jdk1.8以前是数组+链表。
在jdk1.8以后是数组+链表+红黑树。一点点分析数据结构。
1. Map中的entry对象:
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; } }
2.load-factor负载因子和capacity容量
简单说一下存储就是node中key计算的hash值决定存储在数组中的位置的bucket(桶)。
如果hash值一样,数组中该位置的bucket(桶)里就会变成链表。在jdk1.8,链表的长度如果>8,就会变成红黑树。
与HashMap实例相关的参数常用的有两个,load-factor负载因子和capacity容量。
简单解释一下两个参数:
loadFactor 就是创建hashMap什么时间扩容。举个例子来说:
默认new一个HashMap的capacity:16,loadFactor:0.75。
/** * 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 } /** * The load factor used when none specified in constructor. */ static final float DEFAULT_LOAD_FACTOR = 0.75f;16*0.75 = 12;
也就是当Map的大小达到12的时候,开始扩容。
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; 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; // double threshold } 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) { Node<K,V> e; if ((e = oldTab[j]) != null) { oldTab[j] = null; if (e.next == null) newTab[e.hash & (newCap - 1)] = e; else if (e instanceof TreeNode) ((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 { 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; }
看这句:
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && oldCap >= DEFAULT_INITIAL_CAPACITY) newThr = oldThr << 1; // double threshold就知道,每次扩容都是之前的2倍。第一次的大小是16,扩容后就变成32。
这里注意一下1.7和1.8的变化;
先说一下1.7的源码:
转自:http://blog.csdn.net/yimi099/article/details/62043566
3. 1.7源码
void resize(int newCapacity) { //传入新的容量 Entry[] oldTable = table; //引用扩容前的Entry数组 int oldCapacity = oldTable.length; if (oldCapacity == MAXIMUM_CAPACITY) { //扩容前的数组大小如果已经达到最大(2^30)了 threshold = Integer.MAX_VALUE; //修改阈值为int的最大值(2^31-1),这样以后就不会扩容了 return; } Entry[] newTable = new Entry[newCapacity]; //初始化一个新的Entry数组 transfer(newTable); //!!将数据转移到新的Entry数组里 table = newTable; //HashMap的table属性引用新的Entry数组 threshold = (int) (newCapacity * loadFactor);//修改阈值 } void transfer(Entry[] newTable) { Entry[] src = table; //src引用了旧的Entry数组 int newCapacity = newTable.length; for (int j = 0; j < src.length; j++) { //遍历旧的Entry数组 Entry<K, V> e = src[j]; //取得旧Entry数组的每个元素 if (e != null) { src[j] = null;//释放旧Entry数组的对象引用(for循环后,旧的Entry数组不再引用任何对象) do { Entry<K, V> next = e.next; int i = indexFor(e.hash, newCapacity); //!!重新计算每个元素在数组中的位置 e.next = newTable[i]; //标记[1] newTable[i] = e; //将元素放在数组上 e = next; //访问下一个Entry链上的元素 } while (e != null); } } }
1.7里是每次扩容都去计算元素的hash值,从而改变该元素在数组中的位置。capacity变了,位置自然就要改变。
1.8做了优化:
do { 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; }
解释一下什么意思。
转自:http://blog.csdn.net/brycegao321/article/details/52527236/
(e.hash & oldCap) == 0写的很赞!!! 它将原来的链表数据散列到2个下标位置, 概率是当前位置50%,高位位置50%。 你可能有点懵比, 下面举例说明。 上边图中第0个下标有496和896, 假设它俩的hashcode(int型,占4个字节)是
resize前:
496的hashcode: 00000000 00000000 00000000 00000000
896的hashcode: 01010000 01100000 10000000 00100000
oldCap是16: 00000000 00000000 00000000 00010000
496和896对应的e.hash & oldCap的值为0, 即下标都是第0个。
resize后:496的hashcode: 00000000 00000000 00000000 00000000
896的hashcode: 01010000 01100000 10000000 00100000
oldCap是32: 00000000 00000000 00000000 00100000
496和896对应的e.hash & oldCap的值为0和1, 即下标都是第0个和第16个。
因为hashcode的第n位是0/1的概率相同, 理论上链表的数据会均匀分布到当前下标或高位数组对应下标。
再说一下其他参数:
bucket桶:
数组中每一个位置上都放有一个桶,每个桶里就是装一个链表,链表中可以有很多个元素(entry),这就是桶的意思。也就相当于把元素都放在桶中。
size:
HashMap的实例中实际存储的元素的个数。
4. threshold:
threshold = capacity * loadFactor,当Size>=threshold的时候,那么就要考虑对数组的扩增了,也就是说,这个的意思就是衡量数组是否需要扩增的一个标准。
/** * 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;对于一个桶(容器)来说,桶的统计临界值比起list集合更用于树。
当向有多很节点的桶添加一个元素的时候,桶转换成树。这个很多节点就是8。
再说下扩容的过程:
是否扩容主要看:threshold这个参数,threshold = capacity*loadFactor,初始值是threshold = 16*0.75=12,第一次扩容capacity=capacity*2=32,threshold =threshold *2=24。
5. 1.8源码的put方法
再说一下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; //初始化桶,默认16个元素 if ((p = tab[i = (n - 1) & hash]) == null) //如果第i个桶为空,创建Node实例 tab[i] = newNode(hash, key, value, null); else { //哈希碰撞的情况, 即(n-1)&hash相等 Node<K,V> e; K k; if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; //key相同,后面会覆盖value else if (p instanceof TreeNode) e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); //红黑树添加当前node else { for (int binCount = 0; ; ++binCount) { if ((e = p.next) == null) { p.next = newNode(hash, key, value, null); //链表添加当前元素 if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st treeifyBin(tab, hash); //当链表个数大于等于7时,将链表改造为红黑树 break; } if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } if (e != null) { // existing mapping for key V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; //覆盖key相同的value并return, 即不会执行++size } } ++modCount; if (++size > threshold) //key不相同时,每次插入一条数据自增1. 当size大于threshold时resize resize(); afterNodeInsertion(evict); return null; }提一个新的名词,哈希碰撞。
如果hashMap的key和key的hashCode找到数组中同一个位置,就是哈希碰撞。
哈希碰撞是产生链表的原因。
最后!!!!
1,HashMap的初始容量是16个, 而且容量只能是2的幂。 每次扩容时都是变成原来的2倍。
2,默认的负载因子是0.75f,threshold:16*0.75=12。即默认的HashMap实例在插入第13个数据时,会扩容为32。
3,JDK1.8对HashMap的优化, 哈希碰撞后的链表上达到8个节点时要将链表重构为红黑树, 查询的时间复杂度变为O(logN)。
4,通常hashMap查询的时间复杂度是O(N),1.8以后红黑树的查询的时间复杂度是O(logN)。极少数情况不会出现哈希碰撞,那是数组,查询的时间复杂度是O(1)。
5,初始化数组或者扩容为2倍,初值为空时,则根据初始容量开辟空间来创建数组。否则, 因为我们使用2的幂定义数组大小,数据要么待在原来的下标, 或者移动到新数组的高位下标。