Summer Blog

Map 源码分析

map是常用的数据类型,我经常用的是HashMap,好处是简单效率也还不错。本篇同时会涉及到TreeMapConcurrentHashMap,作为有序和线程安全的map的常用实现,这两种类型也是比较常用的。

HashMap

以数组实现的哈希桶数组,用 Key 的哈希值取模桶数组的大小可得到数组下标。

HashMap不是线程安全的类,在扩容时可能产生死循环报错。

成员

    // hash表,会在第一次使用的时候初始化,必要的时候会进行扩容。容量总是2的次方
    transient Node<K,V>[] table;
    // 对于entrySet()方法的一个缓存,实际并不会存储任何数据,只是对`map`数据做操作
    transient Set<Map.Entry<K,V>> entrySet;
    // 表示存储了多少key-value mappings
    transient int size;
    // 用来fail-fast
    transient int modCount;
    // 扩充阀值 等于capacity * loadFactor
    int threshold;
    // 加载因子
    final float loadFactor;

Put

    public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }

    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; // 如果当前table为空,扩充一次
        if ((p = tab[i = (n - 1) & hash]) == null) // 当前key应该在table的 (n-1) & hash 的位置
            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; // 如果存在同样的key,替换
            else if (p instanceof TreeNode) // 如果节点是tree node,则按照tree node方式添加
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else {
                for (int binCount = 0; ; ++binCount) { // 挨个查找桶中的value
                    if ((e = p.next) == null) { // 当前桶的最后一个节点
                        p.next = newNode(hash, key, value, null); // 连接当前值
                        if (binCount >= TREEIFY_THRESHOLD - 1) // 桶中连长度大于TREEIFY_THRESHOLD 则改用tree node
                            treeifyBin(tab, hash);
                        break;
                    }
                    if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k))))
                        break; // 已经存在当前值,直接跳出,此时e就是有相同key的节点
                    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;
    }

hash

    // 扰动函数,减少碰撞
    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

Get

计算出 hash 值,从数组中找到链表头部,循环链表找到对应值

    public V get(Object key) {
        Node<K,V> e;
        return (e = getNode(hash(key), key)) == null ? null : e.value;
    }

    final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k; // first是头节点 e是临时变量
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {
            if (first.hash == hash && // always check first node 减少一次判断,如果头节点就是需要的能快速返回
                ((k = first.key) == key || (key != null && key.equals(k))))
                return first;
            if ((e = first.next) != null) {
                if (first instanceof TreeNode)
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key);
                do {// 循环链表找到key完全一样的值
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);
            }
        }
        return null;
    }

ConcurrentHashMap

线程安全的map容器,在节点内添加新的值、替换值、变为红黑树时会使用synchronized锁住头节点,在扩容时会利用CAS方式获取对象的乐观锁sizeCtl的方式保证线程安全,同时在扩容的具体实现中也有用到CAS保证transferIndex值的正确


comments powered by Disqus