Java HashMap阅读笔记

HashMap:

  • 默认的初始容量16,最大容量为2的30次方,负载因子为0.75F,树行阈值为8,树形缩减为6,最小树化容量64;

详细细节,请看下面代码的注释及位置

    
    /**
     * The default initial capacity - MUST be a power of two.
     */
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

    /**
     * 最小树化容量64
     * Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts
     * between resizing and treeification thresholds.
     */
    static final int MIN_TREEIFY_CAPACITY = 64;


    /**
     * 我们进行的put操作
     */
    final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node[] tab; Node p; int n, i;
        // 如果map表为空(hash槽数组都不存在的意思),则初始并重置大小;
        // 如果 hash槽为空,则初始化第一个链表节点;
        // 否则,说明有 Hash冲突存在
        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 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)p).putTreeVal(this, tab, hash, key, value);
            else {
                for (int binCount = 0; ; ++binCount) {
                    // 如果链表后续节点为空,则插入新节点(尾插),同时是否到达树化阈值8,是则进行树化处理
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        if (binCount >= TREEIFY_THRESHOLD - 1)
                            treeifyBin(tab, hash);
                        break;
                    }
                    // 如果后续节点 hash和内容相同,说明是同一个key,直接返回原节点
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            // Key相同返回原节点,新插入返回 null
            // 如果具有相同 Key,则根据参数 onlyIfAbsent和原值为 null决定是否进行覆盖原值
            if (e != null) {
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e); // LinkedHashMap的一些节点排序操作,HashMap中不做处理
                return oldValue;
            }
        }
        ++modCount; // map结构的变更操作次数累加
        if (++size > threshold) // 如果当前容量大于了负载容量,则进行 扩容操作
            resize();
        afterNodeInsertion(evict); // LinkedHashMap的一些节点排序操作,HashMap中不做处理
        return null;
    }


    /**
     * 当到达树化阈值时,进行树化
     */
    final void treeifyBin(Node[] tab, int hash) {
        int n, index; Node e;
        if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY) // 小于最小树化容量64不转换树
            resize();
        else if ((e = tab[index = (n - 1) & hash]) != null) {
            TreeNode hd = null, tl = null;
            do {
                TreeNode p = replacementTreeNode(e, null);
                if (tl == null)
                    hd = p;
                else {
                    p.prev = tl;
                    tl.next = p;
                }
                tl = p;
            } while ((e = e.next) != null);
            if ((tab[index] = hd) != null)
                hd.treeify(tab); // 从头节点开始 进行排序及转换红黑树
        }
    }

ConcurrentHashMap:

  • 默认的初始容量16,最大容量为2的30次方,负载因子为0.75F,树行阈值为8,树形缩减为6,最小树化容量64;
  • PUT时,逻辑大致和HashMap相同;
  • 其操作采用的 CAS + synchronized 锁来同步并发安全,不是同时使用,而是某些操作采用一种;
  • PUT中,在初始化槽数组、插入未形成链表的节点(节点数=1)或树化节点平衡时,采用的CAS锁。当创建整个链表和插入已成型的链表(节点数>1)和链表树化时,采用 synchronized 锁来保证安全。

LinkedHashMap:

  • 继承自HashMap,实现了HashMap预留的三个扩展方法;
  • 同时添加了一个排序开关,用来实现 LRU形式的排序操作;

心理学感悟:读书的目的不是让我们记住书中内容,而是你学会了思考。

你可能感兴趣的:(Java HashMap阅读笔记)