上一次我们已经了解了JDK1,7的HashMap的实现原理和红黑树,接下来我们看看JDK1.8的HashMap的实现原理
红黑树简介
jdk1.7HashMap的实现原理
在JDK1,8中,HashMap 数据结构为 数组+链表+红黑树.
因为当链表的长度特别长的时候,查询效率将直线下降,查询的时间复杂度为 O(n)。因此,JDK1.8 把它设计为达到一个特定的阈值之后,就将链表转化为红黑树。
在这里我们就讲一下jdk1.7和1.8的HashMap的区别吧:
hash()方法:首先就是hash()方法,1.7版本的hash()方法的散列程度比较大,但是由于1.8引入了红黑树,所以1.8版本的hash()的散列程度相对于1.7来说比较小.
jdk1.8:
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
jdk1.7:
final int hash(Object k) {
int h = hashSeed;//默认为0
if (0 != h && k instanceof String) {
return sun.misc.Hashing.stringHash32((String) k);
}
h ^= k.hashCode();
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
从上面可以看到JDk1.8中的hash方法实现简单得多.
put()方法:我们再来看看put方法();
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node[] tab; Node p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0) //如果数组为空,那么就初始化这个数组;
n = (tab = resize()).length; //resize()对数组进行初始化
if ((p = tab[i = (n - 1) & hash]) == null) //如果这个元素所在数组对应的索引上没有元素的话,那么就直接new一个Node对象并赋值;
tab[i] = newNode(hash, key, value, null);
else { //如果这个元素所在数组对应的索引上已经有元素;
Node e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k)))) //如果数组该索引的第一个元素的key与要插入的key相同,那么直接将e对象等于p;
e = p;
else if (p instanceof TreeNode) //如果p节点是树节点,即红黑树的情况时:
e = ((TreeNode)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); //遍历结束后,将新的node节点插入到最后一个节点的后面(尾插法)
if (binCount >= TREEIFY_THRESHOLD - 1) //TREEIFY_THRESHOLD 值为8;当bincount >= 7.即当第九个元素加入链表的时候,链表的长度大于8的时候,将链表转化为红黑树.
treeifyBin(tab, hash); //将链表转化为红黑树.
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k)))) //如果在链表中找到key相同的元素,直接退出循环
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;
}
}
//记录修改次数
++modCount;
//判断是否需要扩容.
if (++size > threshold) //threshold(阈值) = capacity(table.length(数组长度)) * 0.75(加载因子,默认是0.75)
resize(); //扩容
afterNodeInsertion(evict);
return null;
}
如果存在key节点,返回旧值,如果不存在则返回null.
我们可以看到jdk1.7的时候使用的链表插入是头插法(并发情况下可能会出现环形链表),1.8使用了尾插法;
基本上这里我们就已经将put方法的大致流程走完了,接下来我们看看如何将链表转化为红黑树:
treeifyBin()方法
final void treeifyBin(Node[] tab, int hash) { int n, index; Node e; if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY) //MIN_TREEIFY_CAPACITY值为64;即如果数组为空或者数组的长度小于64的话不会将链表转化为红黑树,而是将数组扩容 resize(); else if ((e = tab[index = (n - 1) & hash]) != null) { //将链表转为红黑树 TreeNode hd = null, tl = null; do { TreeNode p = replacementTreeNode(e, null); //将Node对象转化为TreeNode对象 if (tl == null) //然后将单链表转化为双向链表,hd是头结点; hd = p; else { p.prev = tl; tl.next = p; } tl = p; } while ((e = e.next) != null); if ((tab[index] = hd) != null) hd.treeify(tab); //将双向链表转化为红黑树; } }
replacementTreeNode()方法,将Node对象转化为TreeNode对象;
TreeNodereplacementTreeNode(Node p, Node next) { return new TreeNode<>(p.hash, p.key, p.value, next); }
TreeNode对象的属性及有参构造:
static final class TreeNodeextends LinkedHashMap.Entry { TreeNode parent; // red-black tree links TreeNode left; TreeNode right; TreeNode prev; // 指向前一个节点 boolean red; TreeNode(int hash, K key, V val, Node next) { super(hash, key, val, next); } }
treeify()方法:将双向链表转化为红黑树
final void treeify(Node[] tab) { TreeNode root = null; for (TreeNode x = this, next; x != null; x = next) { //遍历链表 next = (TreeNode )x.next; x.left = x.right = null; //先将节点的左右节点都设置为null; if (root == null) { //根节点 x.parent = null; x.red = false; root = x; } else { K k = x.key; int h = x.hash; Class> kc = null; for (TreeNode p = root;;) { //从根节点开始遍历红黑树; int dir, ph; K pk = p.key; if ((ph = p.hash) > h) //不是直接比较的是值,比较的是哈希值 dir = -1; //代表往左边走 else if (ph < h) dir = 1; //代表往右边走 else if ((kc == null && (kc = comparableClassFor(k)) == null) || (dir = compareComparables(kc, k, pk)) == 0) //查看key对象是否实现了Comparable 接口,如果实现了就用compareto()方法进行比较; dir = tieBreakOrder(k, pk);//如果还相等的话就用compareto()方法比较对象的getClass().getName();如果还相等的话就比较两个对象的System.identityHashCode(); TreeNode xp = p; if ((p = (dir <= 0) ? p.left : p.right) == null) { x.parent = xp; if (dir <= 0) xp.left = x; else xp.right = x; root = balanceInsertion(root, x); //红黑树调整的逻辑 break; } } } } moveRootToFront(tab, root); //将生成的红黑树的根节点root赋值给数组的index下的第一个元素; }
moveRootToFront()方法:
把红黑树的根节点设为其所在的数组的第一个元素
首先明确:TreeNode既是一个红黑树结构,也是一个双链表结构
这个方法里做的事情,就是保证树的根节点一定也要成为链表的首节点
staticvoid moveRootToFront(Node [] tab, TreeNode root) { int n; if (root != null && tab != null && (n = tab.length) > 0) { int index = (n - 1) & root.hash; TreeNode first = (TreeNode )tab[index]; if (root != first) { Node rn; tab[index] = root; TreeNode rp = root.prev; if ((rn = root.next) != null) ((TreeNode )rn).prev = rp; if (rp != null) rp.next = rn; if (first != null) first.prev = root; root.next = first; root.prev = null; } assert checkInvariants(root); } }
下面总结来说一下put(key,value)的过程:
1. 判断键值对数组tab[]是否为空或为null,否则以默认大小resize();
2. 根据键值key计算hash值得到插入的数组索引i,如果tab[i]==null,直接新建节点添加,否则进行下一步;
3. 判断当前数组中处理hash冲突的方式为链表还是红黑树(check第一个节点类型即可),分别处理:
- 如果为链表,那么就遍历链表,将元素插入到链表的最末尾(尾插法),并且判断链表的长度是否大于8,如果链表的长度大于8的话,先判断数组是否为空或者数组的长度是否小于64,如果小于64,则不用将链表转为红黑树,直接将数组扩容,如果链表长度大于等于64,那么就将链表转化为红黑树.
- 如果是红黑树,那么就将元素插入到红黑树中.
1.7和1.8它们判断扩容的条件有所不同:
jdk1.7:
jdk1.8:
让我们来看看1.8的扩容方法:
final Node[] resize() { Node [] 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 [] newTab = (Node [])new Node[newCap]; table = newTab; //初始化 //开始扩容 if (oldTab != null) { for (int j = 0; j < oldCap; ++j) { Node 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 )e).split(this, newTab, j, oldCap); else { // preserve order 对链表的元素扩容 Node loHead = null, loTail = null; Node hiHead = null, hiTail = null; Node 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; //扩容之后的元素要么在newTab[j]要么在newTab(j + oldCap); } } } } } return newTab; }
split()方法:红黑树的扩容;
((TreeNode)e).split(this, newTab, j, oldCap); //this:当前HashMap对象;newTab[]:新数组;j:数组的索引;oldCap:老数组的长度;
final void split(HashMapmap, Node [] tab, int index, int bit) { TreeNode b = this; // Relink into lo and hi lists, preserving order TreeNode loHead = null, loTail = null; //低位链表 TreeNode hiHead = null, hiTail = null; //高位链表 int lc = 0, hc = 0; for (TreeNode e = b, next; e != null; e = next) { //遍历红黑树的那个链表 next = (TreeNode )e.next; e.next = null; if ((e.hash & bit) == 0) { if ((e.prev = loTail) == null) loHead = e; else loTail.next = e; loTail = e; ++lc; //低位的链表个数; } else { if ((e.prev = hiTail) == null) hiHead = e; else hiTail.next = e; hiTail = e; ++hc;//高位的链表个数; } } if (loHead != null) { if (lc <= UNTREEIFY_THRESHOLD) // UNTREEIFY_THRESHOLD = 6; 当长度小于6的时候,还是链表 tab[index] = loHead.untreeify(map); else { //当长度大于6时 tab[index] = loHead; if (hiHead != null) //高位链表不为空 loHead.treeify(tab); //链表树化 } } if (hiHead != null) { if (hc <= UNTREEIFY_THRESHOLD) tab[index + bit] = hiHead.untreeify(map); else { tab[index + bit] = hiHead; if (loHead != null) hiHead.treeify(tab); } } }



