你真的懂大厂面试题:HashMap吗?
jdk1.8版本HashMap
以HashMap的put
方法和get
方法为出发点,从源码角度,阐述面试的知识点
-
HashMap中<key,value>元素被封装成Node对象
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; } }
-
put方法
HashMap的put方法会调用
putVal
方法//hashmap的put方法 public V put(K key, V value) { return putVal(hash(key), key, value, false, true); }
hash函数
hash过程(即hash函数)是HashMap的一个核心知识点。hash映射的计算过程是
hashValue&(n-1)
.计算公式中的hashValue就是hash函数返回的结果。即key.hashCode与其高16位相异或的结果。
为什么是key.hashCode与其高16位相异或的结果?
n是2的幂次方,所以n-1,一定是前部全为0,尾部全为1。类似
00001111
,假设hashValue==01010111
其与
00001111
相与,由于0与[0或1]相与一定为0,所以相与过程中,只利用到了hashVaule的0111
后四位,所以为了增加多样性,使散列均匀,让hashValue与其前四位相异或,在后四位的基础上,
融入前四位的差异
static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); }
putVal函数
首先判断map是否为空,为空调用resize函数进行初始化,初始数组大小为
16
(数组大小一定是2的幂次方),初始加载因子0.75
,之后进行hash映射计算(n-1)&hash
,得到hash位置(数组中的位置),如果没有hash冲突,就直接插入。之后分两种情况讨论,红黑树结构,按照红黑树的方式进行插入或覆盖。链表结构,使用尾插法进行插入或替换,并且如果插入后,超过规定的阈值,链表结构会转换成红黑树结构。最后如果插入元素后,数组中包含的元素超过加载阈值,会调用
resize
函数进行扩容操作final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; //判断map是否为空,为空调用resize函数初始化 if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; //如果没有hash冲突,就直接插入 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); //链表结构,使用尾插法进行插入 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); break; } if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } //存在相同的key,则考虑值覆盖 if (e != null) { // existing mapping for key V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; } } ++modCount; //如果元素大于阈值,则进行扩容操作 if (++size > threshold) resize(); afterNodeInsertion(evict); return null; }
resize函数
hashMap的扩容操作,其实也是一个核心知识点。
如果2倍的原数组大小小于一个最大数量阈值,则可以进行扩容操作,原数组大小扩容成两倍,加载阈值也扩大为两倍。为什么一定要扩容为其两倍?因为数组大小一定必须为2的幂次方,数组大小n是2的幂次方的话, n − 1 n-1 n−1一定是前部均为0尾部均为1的形式, h a s h & ( n − 1 ) hash\&(n-1) hash&(n−1)范围一定是 [ 0 , n − 1 ] [0,n-1] [0,n−1].
由于数组扩容了,原数组必须会有一个rehash过程。
分为两种情况,一红黑树结构,是通过分裂实现rehash过程
二是链表结构
这里的 n n n是oldcap ,即原数组大小
如果 n & h a s h = = 0 n\ \&\ hash == 0 n & hash==0则 ( 2 n − 1 ) & h a s h = = ( n − 1 ) & h a s h (2n-1)\&hash ==(n-1) \& hash (2n−1)&hash==(n−1)&hash
如果 n & h a s h ! = 0 n\ \&\ hash\ !=\ 0 n & hash != 0则 ( 2 n − 1 ) & h a s h = = ( n − 1 ) & h a s h + n (2n-1)\&hash == (n-1)\&hash + n (2n−1)&hash==(n−1)&hash+n
上面结论,自己可以在草稿纸上推一推
根据上面结论,可以依据 h a s h V a l u e & n hashValue\ \&\ n hashValue & n的结果,将链表中的元素分为两个链表,一个依旧放在原位置
一个放在原位置+n处
final Node<K,V>[] resize() { Node<K,V>[] oldTab = table; int oldCap = (oldTab == null) ? 0 : oldTab.length; int oldThr = threshold; int newCap, newThr = 0; //原数组大小不为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; //rehash过程 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; }