HashMap的知识点能够说在面试中常常被问到,是Java中比较常见的一种数据结构。因此这一篇就经过源码来深刻理解下HashMap。java
/** HashMap继承AbstractMap,而AbstractMap又实现了Map的接口 */ public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable
从上面源码能够看出HashMap支持序列化和反序列化,并且实现了cloneable接口,能支持clone()方法复制一个对象。node
//最小容量为16,且必定是2的幂次 static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 //最大容量为2的30次方 static final int MAXIMUM_CAPACITY = 1 << 30; // 默认加载因子 static final float DEFAULT_LOAD_FACTOR = 0.75f; //当某节点的链表长度大于8而且hash数组的容量达到64时,链表将会转换成红黑树 static final int TREEIFY_THRESHOLD = 8; //当链表长度小于6时,红黑树将转换成链表 static final int UNTREEIFY_THRESHOLD = 6; //链表变成红黑树的最小容量 static final int MIN_TREEIFY_CAPACITY = 64;
从上面的源码能够看出,JDK1.8的HashMap其实是由数组+链表+红黑树组成,在必定条件下链表会转换成红黑树。这里要谈一下默认加载因子为何为0.75(3/4),加载因子也叫扩容因子,用来判断HashMap何时进行扩容。选择0.75的缘由是为了平衡容量与查找性能:扩容因子越大,形成hash冲突的概率就越大,查找性能就会越低,反之扩容因子越小,所占容量就会越大。于此同时,负载因子为3/4的话,和capacity的乘积结果就能够是一个整数。
面试
下面再看看hash数组中的元素数组
hash数组通常称为哈希桶(bucket),结点在JDK1.7中叫Entry,在JDK1.8中叫Node。安全
//1.8中Node实现entry的接口 static class Node<K,V> implements Map.Entry<K,V> { //每一个节点都会包含四个字段:hash、key、value、next 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; } //hash值是由key和value的hashcode异或获得 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; //判断o对象是否为Map.Entry的实例 if (o instanceof Map.Entry) { Map.Entry<?,?> e = (Map.Entry<?,?>)o; //再判断二者的key和value值是否相同 if (Objects.equals(key, e.getKey()) && Objects.equals(value, e.getValue())) return true; } return false; } } //这个是扰动函数,减小hash碰撞 static final int hash(Object key) { int h; //将key的高16位与低16位异或(int是2个字节,32位) return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); }
public V get(Object key) { Node<K,V> e; //将key值扰动后传入getNode函数查询节点 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) { //从第一个节点开始查询,若是hash值和key值相等,则查询成功,返回该节点 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; }
向哈希表中插入一个节点数据结构
public V put(K key, V value) { //将扰动的hash值传入,调用putVal函数 return putVal(hash(key), key, value, false, true); } //当参数onlyIfAbsent为true时,不会覆盖相同key的值value;当evict是false时,表示是在初始化时调用 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; //若是与该节点的hash值和key值都相等,将节点引用赋给e if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; //若是p是树节点的实例,调用红黑树方法新增一个树节点e else if (p instanceof TreeNode) e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); //若该节点后是链表 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 1st //将链表转换成红黑树 treeifyBin(tab, hash); break; } //遍历过程当中发现了key和hash值相同的节点,用e覆盖该节点 if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } //对e节点进行处理 if (e != null) { V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; } } //节点插入成功,修改modCount值 ++modCount; //若是达到扩容条件,直接扩容 if (++size > threshold) resize(); afterNodeInsertion(evict); return null; }
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); } //数组为空,且新的阈值为0 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 //定义现有数组的位置low,扩容后的位置high;high = low + oldCap Node<K,V> loHead = null, loTail = null; Node<K,V> hiHead = null, hiTail = null; Node<K,V> next; do { next = e.next; /*经过(e.hash & oldCap)来肯定元素是否须要移动, e.hash & oldCap大于0,说明位置须要做相应的调整。 反之等于0时说明在该容量范围内,下标位置不变。 */ 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; } //处于高位位置要改变为j + oldCap if (hiTail != null) { hiTail.next = null; newTab[j + oldCap] = hiHead; } } } } } return newTab; }
HashMap其实是线程不安全的,在JDK1.7中,链表的插入方式为头插法,在多线程下插入可能会致使死循环。所以在JDK1.8中替换成尾插法(其实想要线程安全大可用ConcurrentHashMap、Hashtable)多线程
//JDK1.7源码 void transfer(Entry[] newTable boolean rehash) { int newCapacity = newTable.length; for (Entry<K,V> e : table) { while(null != e) { //多线程在这里会致使指向成环 Entry<K,V> next = e.next; if(rehash) { e.hash = null == e.key ? 0 : hash(e.key); } int i = indexFor(e.hash, new Capacity); e.next = newTable[i]; newTable[i] = e; e = next; } } }
假如HashMap的容量为2,其中在数组中有一个元素a(此时已经到达扩容的临界点)。建立两个线程t一、t2分别插入b、c,由于没有锁,两个线程都进行到扩容这一步,那么其中有节点位子由于扩容必然会发生变化(之前的容量不够),这个时候假设t1线程成功运行,插入成功。可是因为t2线程的合并,加上节点位置的挪动,就会形成链表成环。最后读取失败函数
//经过key值删除该节点,并返回value public V remove(Object key) { Node<K,V> e; return (e = removeNode(hash(key), key, null, false, true)) == null ? null : e.value; } //删除某个节点 //若matchValue为true时,须要key和value都要相等才能删除;若movable为false时,删除节点时不移动其余节点 final Node<K,V> removeNode(int hash, Object key, Object value,boolean matchValue, boolean movable) { Node<K,V>[] tab; Node<K,V> p; int n, index; //若数组非空 if ((tab = table) != null && (n = tab.length) > 0 && (p = tab[index = (n - 1) & hash]) != null) { //设node为删除点 Node<K,V> node = null, e; K k; V v; //查到头节点为所要删除的点,直接赋于node if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) node = p; //不然遍历 else if ((e = p.next) != null) { //当节点为树节点 if (p instanceof TreeNode) node = ((TreeNode<K,V>)p).getTreeNode(hash, key); //节点为链表时 else { do { if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) { node = e; break; } p = e; } while ((e = e.next) != null); } } //对取回的node节点进行处理,当matchValue为false,或者value相等时 if (node != null && (!matchValue || (v = node.value) == value || (value != null && value.equals(v)))) { if (node instanceof TreeNode) //为树节点 ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable); else if (node == p) //为链表头结点 tab[index] = node.next; else //为链表中部节点 p.next = node.next; //修改modCount和size ++modCount; --size; afterNodeRemoval(node); return node; } } return null; }
新元素下标方面,1.8经过高位运算(e.hash & oldCap) == 0
分类处理表中的元素:低位不变,高位原下标+原数组长度;而不是像1.7中计算每个元素下标。工具
在resize()函数中,1.8将1.7中的头插逆序变成尾插顺序。可是仍然建议在多线程下不要用HashMap。性能
参考博文: