zoukankan      html  css  js  c++  java
  • HashMap数据结构分析(jdk8)

    看了下HashMap的源码,做下记录,首先还是先从流程图开始

    下面用代码分析下方法

        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相同
                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);
                            //大于等于7 转为树节点
                            if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                                treeifyBin(tab, hash);
                            break;
                        }
                        //如果链表不是尾部节点,并且是否遇到了key和hash都相同的
                        if (e.hash == hash &&
                            ((k = e.key) == key || (key != null && key.equals(k))))
                            break;
                        p = e;
                    }
                }
                // 找到了 key和value都相同的节点
                if (e != null) { // existing mapping for key
                    V oldValue = e.value;
                    // value不为null 并且 onlyIfAbsent为false 就重新赋下值
                    if (!onlyIfAbsent || oldValue == null)
                        e.value = value;
                    // 一个待实现的方法,hashMap无用
                    afterNodeAccess(e);
                    // 结束
                    return oldValue;
                }
            }
            //结构更改的计数,为了在迭代中快速判断是否被修改了 而抛出异常
            ++modCount;
            // 长度++,扩容阈值
            if (++size > threshold)
                resize();
            //一个待实现的方法,hashMap无用
            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;
                }
                //新容量为旧容量*2 并且小于最大值,且旧容量大于最小的初始化容量
                else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
                         oldCap >= DEFAULT_INITIAL_CAPACITY)
                    //新阈值也为旧阈值*2
                    newThr = oldThr << 1; 
            }
            //旧阈值不为0,新的容量就是旧的阈值
            else if (oldThr > 0) // initial capacity was placed in threshold
                newCap = oldThr;
            //两个值都是0,此时是初始化代码
            else {            
                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;
            //数据从旧table复制到新table
            if (oldTab != null) {
                //从低到高复制
                for (int j = 0; j < oldCap; ++j) {
                    Node<K,V> e;
                    if ((e = oldTab[j]) != null) {
                        oldTab[j] = null;
                        //单纯一个Node对象结构,直接复制
                        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
                            //把原始的链表拆成两个,判断标准就是 hash& oldCap ==0 
                            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);
                            //拆完之后,新table的j位置放lohead
                            if (loTail != null) {
                                loTail.next = null;
                                newTab[j] = loHead;
                            }
                            //拆完后,新table的j+oldCap的位置放hihead
                            if (hiTail != null) {
                                hiTail.next = null;
                                newTab[j + oldCap] = hiHead;
                            }
                        }
                    }
                }
            }
            return newTab;
        }
    
    
  • 相关阅读:
    Java知识系统回顾整理01基础04操作符02关系操作符
    Java知识系统回顾整理01基础04操作符01算术操作符
    Java知识系统回顾整理01基础03变量09块
    Java知识系统回顾整理01基础03变量08表达式
    Java知识系统回顾整理01基础03变量07final关键字
    Java知识系统回顾整理01基础03变量06变量的作用域
    Java知识系统回顾整理01基础03变量05变量命名规则
    Java知识系统回顾整理01基础03变量04类型转换
    leetcode-----64. 最小路径和
    leetcode-----63. 不同路径 II
  • 原文地址:https://www.cnblogs.com/june777/p/11715018.html
Copyright © 2011-2022 走看看