java集合中的HashMap源码分析

1.hashMap中的成员分析

   transient Node<K,V>[] table;    //为hash桶的数量
/**
     * The number of key-value mappings contained in this map.
     */
    transient int size;      //hashMap中键值对的数量

    /**
     * The number of times this HashMap has been structurally modified
     * Structural modifications are those that change the number of mappings in
     * the HashMap or otherwise modify its internal structure (e.g.,
     * rehash).  This field is used to make iterators on Collection-views of
     * the HashMap fail-fast.  (See ConcurrentModificationException).
     */
    transient int modCount;  //用来记录hashMap被改变的次数,进行fail-fast

    /**
     * The next size value at which to resize (capacity * load factor).
     *
     * @serial
     */
    // (The javadoc description is true upon serialization.
    // Additionally, if the table array has not been allocated, this
    // field holds the initial array capacity, or zero signifying
    // DEFAULT_INITIAL_CAPACITY.)
    int threshold;          //用来表示HashMap的阙值 threshold = capacity*loadFactor

    /**
     * The load factor for the hash table.
     *
     * @serial
     */
    final float loadFactor;  //用来表示HashMap的加载因子

2.hashMap中的重要方法分析

  (1).hash方法(用来根据key来获取hash值)

static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); //注意当key为null是会返回0,hashmap允许key为null进行存储,且存在table[0]的位置。
                                           另外会对获取的hashcode进行高低16位按位与,减小hash冲突的概率
}

  (2).tableSizeFor(使用此方法来让我们的容量变为2的倍数)

static final int tableSizeFor(int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}

  (3).put方法

public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true); //会去调用putVal方法
}
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)   //判断table是否为null或table的大小是否为0
            n = (tab = resize()).length;             //如果上述条件成立,那么调用resize()方法去扩容
        if ((p = tab[i = (n - 1) & hash]) == null)       //计算插入的元素在hash桶中的位置,若该位置还没有元素
            tab[i] = newNode(hash, key, value, null);     //新建一个node节点,并将该节点添加到该位置
        else {                            //否则,执行以下代码
            Node<K,V> e; K k;
            if (p.hash == hash &&                //判断该位置的第一个元素是否与我们要插入的元素相同
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = 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);   
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st  //判断链表的长度是否大于8
                            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   //此时e节点中记录的是hashMap中与我们要插入的节点相同的节点,在这里统一进行处理
                V oldValue = e.value;              //记录旧的value值
                if (!onlyIfAbsent || oldValue == null)     //通过onlyIfAbsent与oldValue的值判断是否要进行覆盖
                    e.value = value;              //覆盖旧的值
                afterNodeAccess(e);              //此方法与LinkedHashMap相关,是一个回调方法,我们之后的文章再进行分析
                return oldValue;                //返回旧的值
            }
        }
        ++modCount;                      //代表hashMap的结构被改变
        if (++size > threshold)               //判断是否要进行扩容
            resize();
        afterNodeInsertion(evict);              //此方法也是与LinkedHashMap相关的方法
        return null;
    }

  (4).resize(用来对hashMap进行扩容)

final Node<K,V>[] resize() {
        Node<K,V>[] oldTab = table;                //记录原来的table
        int oldCap = (oldTab == null) ? 0 : oldTab.length;   //判断原来的table是否为空,若为空则oldCap = 0,否则oldCap = oldTab.length
        int oldThr = threshold;                   //记录原来的阙值
        int newCap, newThr = 0;                   //创建变量用来记录新的容量和阙值
        if (oldCap > 0) {                      //判断原来的容量是否大于0,由于HashMap是在第一次put是才会进行初始化,因此此方法也是判断table是要扩容还是要初始化.大于0代表已经初始化过了
            if (oldCap >= MAXIMUM_CAPACITY) {          //如果原来的容量大于0且大于等于最大值
                threshold = Integer.MAX_VALUE;         //将阙值设为最大值,并返回原来的容量,代表该table已经不能再进行扩容
                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    //如果说oldCap为0(代表hashMap没有被初始化过)且原来的阙值大于0(此处需要看一下hashmap的构造方法)
            newCap = oldThr;                              //将新的容量设置为原来的阙值
        else {               // zero initial threshold signifies using defaults//否则说明我们在新建hashMap是没有指定初始值或是我们将初始大小设为了0
            newCap = DEFAULT_INITIAL_CAPACITY;                   //设为默认值16
            newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);  //阙值设为16*0.75
        }
        if (newThr == 0) {                              //如果新的阙值为0(此处表示在新建hashMap是给定了capacity且不为0)
            float ft = (float)newCap * loadFactor;                //设置为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数组(可能是初始化,也可能是扩容)
        table = newTab;                      
        if (oldTab != null) {                //若原来的table是否为空,代表现在是要进行扩容操作
            for (int j = 0; j < oldCap; ++j) {      //遍历hash桶
                Node<K,V> e;
                if ((e = oldTab[j]) != null) {      //遍历每一个hash桶中的元素,并记录第一个节点到变量e
                    oldTab[j] = null;           //将原来的位置设为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;     //将每一个桶中的元素分为两类,扩容后的位置与原来相同则记录到loHead,loTail这个链表中,扩容后与原来的位置不同则记录到hiHead,hiTail中
                        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;      //将loHead链表写入到新的table
                        }
                        if (hiTail != null) {        //将hiHead链表记录到新的table
                            hiTail.next = null;
                            newTab[j + oldCap] = hiHead;
                        }
                    }
                }
            }
        }
        return newTab;
    }

3.hashMap中的一些细节问题

  (1).为什么table的大小为2的倍数,且以2倍进行扩容?

    ① 在传统的散列中我们一般使用奇数作为table的长度,由于奇数的因子只有1与本身,在进行取余操作时可以避免hash冲突的概率。但是当我们在进行resize操作时需要去一个个的去计算新的位置。

    ② 当我们以二倍扩容后,我们发现每次扩容后只是hashCode多长来了一位计算为,我们只需要去判断多出来的计算位是1 or 0 就可以判断新的位置不变还是在(oldCap+OldPos)的位置。

  (2).为什么在链表节点大于8是转换为红黑树?(参考源码中的注释)

    

猜你喜欢

转载自www.cnblogs.com/liwangcai/p/10739303.html