其他分享
首页 > 其他分享> > jdk1.7和jdk1.8 hashMap扩容

jdk1.7和jdk1.8 hashMap扩容

作者:互联网

什么时候扩容

if ((size >= threshold) && (null != table[bucketIndex])) {
            resize(2 * table.length);   //两倍扩容
            hash = (null != key) ? hash(key) : 0;
            bucketIndex = indexFor(hash, table.length);
        }
 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;
            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;
                }
            }
            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;
    }

怎么扩容

  static final int UNTREEIFY_THRESHOLD = 6;



if (lc <= UNTREEIFY_THRESHOLD)
                    tab[index] = loHead.untreeify(map);   //将双向链表转换成单链表
                else {
                    tab[index] = loHead;
                    if (hiHead != null) // (else is already treeified)
                        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);
                }
            }
```:
### 扩容后有什么问题(多线程环境下)
* jdk1.7
  * 多线程环境下会形成环形链表
* jdk 1.8 
  * 多线程环境下会有数据丢失,因为是直接覆盖。

### hash冲突的数据结构解决
* jdk1.7 
  * 产生冲突后会形成单向链表
* jdk 1.8
  * 产生冲突后会形成单向链表
  * 如果单向链表的长度大于等于8,会转换成红黑树+双向链表(扩容时使用),扩容后发现双向链表的节点小于等于6个,则会将双向链表转换成单向链表,否则转换成红黑树

标签:jdk1.8,hash,hashMap,tab,jdk1.7,value,链表,key,null
来源: https://www.cnblogs.com/liuzhidao/p/14241764.html