Fork me on GitHub
如果对您有帮助,麻烦您点赞留言吧~~~

java集合之HashMap源码解读

源自:jdk1.8.0_121
HashMap继承自AbstractMap,实现了MapCloneableSerializable

HashMap内部是由数组、链表、红黑树实现的

变量

    // 默认大小
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;

    // 最大容量
    static final int MAXIMUM_CAPACITY = 1 << 30;

    // 默认负载因子,默认0.75,当数组
    static final float DEFAULT_LOAD_FACTOR = 0.75f;

    // 链表长度大于8的时候转红黑树
    static final int TREEIFY_THRESHOLD = 8;

    // 红黑树节点小于6的时候转链表
    static final int UNTREEIFY_THRESHOLD = 6;

    // 转换为红黑树之前还得判断数组的容量是否大于64
    static final int MIN_TREEIFY_CAPACITY = 64;

    // 数组
    transient Node<K,V>[] table;

    transient Set<Map.Entry<K,V>> entrySet;

    // 数组table的大小
    transient int size;

    // 操作次数
    transient int modCount;

	/**
     * 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.)
    // 这个注释很关键,如果table数组还没被分配时,阈值threshold等于数组的数组容量,反之threshold = capacity * load factor
    int threshold;

    // 负载因子
    final float loadFactor;
    

构造方法

    public HashMap(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);
        this.loadFactor = loadFactor;
        this.threshold = tableSizeFor(initialCapacity);
    }

    public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }

    public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    }

    public HashMap(Map<? extends K, ? extends V> m) {
        this.loadFactor = DEFAULT_LOAD_FACTOR;
        putMapEntries(m, false);
    }

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;
        // 如果数组为null或者数组的大小为0时,对数组进行扩容
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        // 因为n为2的a次,(2^a - 1) & hash < 2^a,所以不会越界,当tab[i]为空时(索引不冲突),直接插入数组中
        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;
            // 当p为红黑树时
            else if (p instanceof TreeNode)
                // Node转型TreeNode*
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            // 当p为链表时
            else {
                // 一直循环到链表的最后一个结点
                for (int binCount = 0; ; ++binCount) {
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        // 当结点数大于等于8
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            // 链表转红黑树
                            treeifyBin(tab, hash);
                        break;
                    }
                    // 当hash和key都冲突时,也就是找到了此结点,用于替换
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            // hash和key都冲突时,e才会不等于null
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                // 如果onlyIfAbsent为false就不会替换原有的值
                if (!onlyIfAbsent || oldValue == null)
                    // 替换原有的值
                    e.value = value;
                afterNodeAccess(e);
                // 返回被替换的值
                return oldValue;
            }
        }
        ++modCount;
        // 超过最大容量(length * Load factor)时,扩容
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

tableSizeFor方法

    // 返回一个最接近cap的2^n幂
    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;
    }

get方法

    // 通过key的和key的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;
        // 首先获取这个key所在的哪一个链表或者红黑树
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {
            // 如果头结点的key和hash都与要查找的相等时,直接返回头结点
            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);
                // 链表,就一直循环到匹配到的key,否则返回null
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);
            }
        }
        return null;
    }

treeifyBin方法

    // 将Node结点转换成TreeNode结点(其实也就是TreeNode结点的双向链表)
    final void treeifyBin(Node<K,V>[] tab, int hash) {
        int n, index; Node<K,V> e;
        // 数组为空或者数组的大小小于64,扩容
        if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
            resize();
        // 第一个结点不为空时
        else if ((e = tab[index = (n - 1) & hash]) != null) {
            // hd 头结点,tl 尾结点
            TreeNode<K,V> hd = null, tl = null;
            do {
                // 将Node结点转换成TreeNode结点
                TreeNode<K,V> 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);
        }
    }

内部类

HashMap$TreeNode类(红黑树结点)

红黑树的特性

  1. 每个结点是红色或者黑色。
  2. 根结点是黑色。
  3. 每个叶子节点(NIL)是黑色。
  4. 如果一个节点是红色的,则它的子节点必须是黑色的。
  5. 从一个节点到该节点的子孙节点的所有路径上包含相同数目的黑节点。

treeify方法

将红黑树结点树形化,变成红黑树的结构。【暂未深入了解】

        final void treeify(Node<K,V>[] tab) {
            TreeNode<K,V> root = null;
            for (TreeNode<K,V> x = this, next; x != null; x = next) {
                next = (TreeNode<K,V>)x.next;
                x.left = x.right = null;
                // 确认根结点,黑色
                if (root == null) {
                    x.parent = null;
                    x.red = false;
                    root = x;
                }
                else {
                    K k = x.key;
                    int h = x.hash;
                    Class<?> kc = null;
                    for (TreeNode<K,V> p = root;;) {
                        int dir, ph;
                        K pk = p.key;
                        // 根结点(p)的hash > 要插入结点(x)的hash时
                        // 插入到根结点的左边
                        if ((ph = p.hash) > h)
                            dir = -1;
                        // 插入到根结点的右边
                        else if (ph < h)
                            dir = 1;
                        else if ((kc == null &&
                                  (kc = comparableClassFor(k)) == null) ||
                                 (dir = compareComparables(kc, k, pk)) == 0)
                            dir = tieBreakOrder(k, pk);

                        TreeNode<K,V> xp = p;
                        if ((p = (dir <= 0) ? p.left : p.right) == null) {
                            x.parent = xp;
                            if (dir <= 0)
                                xp.left = x;
                            else
                                xp.right = x;
                            root = balanceInsertion(root, x);
                            break;
                        }
                    }
                }
            }
            moveRootToFront(tab, root);
        }

getTreeNode方法

        final TreeNode<K,V> getTreeNode(int h, Object k) {
            return ((parent != null) ? root() : this).find(h, k, null);
        }

        final TreeNode<K,V> find(int h, Object k, Class<?> kc) {
            TreeNode<K,V> p = this;
            do {
                int ph, dir; K pk;
                TreeNode<K,V> pl = p.left, pr = p.right, q;
                if ((ph = p.hash) > h)
                    p = pl;
                else if (ph < h)
                    p = pr;
                else if ((pk = p.key) == k || (k != null && k.equals(pk)))
                    return p;
                else if (pl == null)
                    p = pr;
                else if (pr == null)
                    p = pl;
                else if ((kc != null ||
                          (kc = comparableClassFor(k)) != null) &&
                         (dir = compareComparables(kc, k, pk)) != 0)
                    p = (dir < 0) ? pl : pr;
                else if ((q = pr.find(h, k, kc)) != null)
                    return q;
                else
                    p = pl;
            } while (p != null);
            return null;
        }

疑问?

hash % 2^n == hash & (2^n-1)

hash % 2^n 余数是0~2^n-1
hash & (2^n-1) ,也就是取hash的后n位,后n位最大值是2^n-1

当hash = n时

hash n hash % 2^n hash & (2^n-1)
0 0 0 0
1 1 1 1
... ... ... ...
n n n n

当hash = n-a时(hash < n)

hash n hash % 2^n hash & (2^n-1)
0 a 0 0
1 a+1 1 1
... ... ... ...
n-a n n-a n-a

当hash = n+a时(hash > n)

hash n hash % 2^n hash & (2^n-1)
0 -a 0 0
1 -a+1 1 1
... ... ... ...
n+a n n+a n+a
综上所述hash % 2^n == hash & (2^n-1)成立。

Node转型TreeNode(向下转型)

NodeTreeNode都是HashMap的内部类,怎么还能转型的呢?

    // 在HashMap里内部类TreeNode继承了LinkedHashMap的内部类Entry
    static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V>
    // 在LinkedHashMap里内部类Entry又继承了HashMap的内部类Node
    static class Entry<K,V> extends HashMap.Node<K,V>
posted @ 2018-04-24 11:48  jarjune  阅读(335)  评论(0编辑  收藏  举报