Fork me on GitHub

Java集合--HashMap源码

本文基于JKD1.8

1.关键的属性:

    //默认容量
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
    //最大容量
    static final int MAXIMUM_CAPACITY = 1 << 30;
    //默认负载因子
    static final float DEFAULT_LOAD_FACTOR = 0.75f;
    //链表树化的阀值
    static final int TREEIFY_THRESHOLD = 8;
    //树形转为链表的阀值
    static final int UNTREEIFY_THRESHOLD = 6;
    //树化要求的node容量
    static final int MIN_TREEIFY_CAPACITY = 64;
    //实际存放节点的数组
    transient Node<K,V>[] table;
    //KV的Set,相当于一个快照
    transient Set<Map.Entry<K,V>> entrySet;
    //存储的节点的个数
    transient int size;
    //fial-fast机制的modify count个数
    transient int modCount;
    //容量×负载因子得到的阈值,超过该阈值进行扩容
    int threshold;
    //负载因子
    final float loadFactor;

2.构造函数

构造函数可以自定义初始化容量和负载因子的大小,其中初始化容量会自动提升为2的n幂次(n>31),提升容量的算法tableSizeFor写的很好

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);
    }

/**
     * Returns a power of two size for the given target capacity.
     */
    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;
    }

n |= n>>>1的作用是将n的二进制中第一个1和这个1的右边一位按位与,结果赋值给n;也就是n的非0最高位起,前两位变成了1;
n |= n>>>2的作用是将n的二进制中非0最高位中的前两位和n按位与,结果赋值给n;也就是n的非0最高位起,前四位变成1了;
以此类推...
n的非0最高位到最低位都是1,然后return n+1,得到最接近的2的幂次

3.新增方法

再来看下关键的put方法,put方法会设置map中key对应的value,如果该key存在,覆盖旧值,并将其返回;

/**
     * Associates the specified value with the specified key in this map.
     * If the map previously contained a mapping for the key, the old
     * value is replaced.
     *
     * @param key key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with <tt>key</tt>, or
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
     *         (A <tt>null</tt> return can also indicate that the map
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
     */
    public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }

hash()将key的高16位拿来参与运算,这样做的目的是减少哈希碰撞,因为只取低位的值作为hash,碰撞很严重;参考:JDK 源码中 HashMap 的 hash 方法原理是什么?

    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

putVal()方法是新增节点的实际方法

    /**
     * Implements Map.put and related methods
     *
     * @param hash hash for key
     * @param key the key
     * @param value the value to put
     * @param onlyIfAbsent if true, don't change existing value
     * @param evict if false, the table is in creation mode.
     * @return previous value, or null if none
     */
    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) //空map,按照默认大小或指定参数开辟空间,lazy init
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null) // key的hash值在map中的value如果是空的,
            tab[i] = newNode(hash, key, value, null);//赋新值
        else { //hash位置已存在旧值
            Node<K,V> e; K k;
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k)))) //p的值是hash对应的旧的node节点,如果p的key和参数key值相等的话,e = p
                e = p;
            else if (p instanceof TreeNode) //旧值的key和参数key不相等,并且如果p是树节点,将新的KV放入树里
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else {//旧值的key和参数key不相等
                for (int binCount = 0; ; ++binCount) {
                    if ((e = p.next) == null) { //当前节点是不是链表的尾节点
                        p.next = newNode(hash, key, value, null); //在尾节点新增参数KV
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st //在链表的尾节点判断链表节点的个数是否满足树化的条件
                            treeifyBin(tab, hash);
                        break;
                    }
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))//在链表中找和参数key同值或相等的key
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key //e这个节点的作用就是保存以前存在的旧值,用来return
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)//当前节点的个数是否大于阈值
            resize(); //翻倍扩容数组
        afterNodeInsertion(evict);//给LinkedHashMap留的坑位,便于插入节点后处理
        return null;
    }

现在putTreeVal()、treeifyBin()这两个树化操作还有待分析

4.resize()扩容方法

/**
     * Initializes or doubles table size.  If null, allocates in
     * accord with initial capacity target held in field threshold.
     * Otherwise, because we are using power-of-two expansion, the
     * elements from each bin must either stay at same index, or move
     * with a power of two offset in the new table.
     *
     * @return the table
     */
    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) { //旧表的容量大于0
            if (oldCap >= MAXIMUM_CAPACITY) { //旧表容量阀值超过定义的最大容量(2的30次方)
                threshold = Integer.MAX_VALUE; //不扩容,但是调整容量阀值为整形的最大值
                return oldTab;
            }
            else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && //原来容量小于1<<30时,新容量为原来容量的二倍
                     oldCap >= DEFAULT_INITIAL_CAPACITY)
                newThr = oldThr << 1; // double threshold
        }
        else if (oldThr > 0) // initial capacity was placed in threshold //旧表容量小于等于0并且旧表阀值大于0
            newCap = oldThr; //新表容量等于旧表阀值
        else {               // zero initial threshold signifies using defaults //旧表容量和阀值都小于等于0
            newCap = DEFAULT_INITIAL_CAPACITY; //无参初始化,默认容量16,阀值12
            newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
        }
        if (newThr == 0) { //如果没翻倍扩容,也没走默认容量,例如:oldCap<<29,newCap为1<<30时
            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;
        if (oldTab != null) {
            for (int j = 0; j < oldCap; ++j) { //遍历旧数组
                Node<K,V> e;
                if ((e = oldTab[j]) != null) { //数组元素不为空
                    oldTab[j] = null;
                    if (e.next == null) //数组元素不是链表,也不是树形结构,即没有hash冲突,只有一个元素的时候
                        newTab[e.hash & (newCap - 1)] = e; //rehash到新表
                    else if (e instanceof TreeNode) //红黑树的mapping
                        ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
                    else { // preserve order //维持原来顺序,并且是两条链表(loHead,hiHead),分别进行尾插法
                        Node<K,V> loHead = null, loTail = null;//low位置链表,位置=原表位置
                        Node<K,V> hiHead = null, hiTail = null;//high位置链表,位置=原表位置+原表大小
                        Node<K,V> next;
                        do {
                            next = e.next;
                            if ((e.hash & oldCap) == 0) {//rehash落在和原表相同的位置的元素
                                if (loTail == null)
                                    loHead = e;
                                else
                                    loTail.next = e;
                                loTail = e;
                            }
                            else {//rehash落在后半部分的元素
                                if (hiTail == null)
                                    hiHead = e;
                                else
                                    hiTail.next = e;
                                hiTail = e;
                            }
                        } while ((e = next) != null);
                        if (loTail != null) {
                            loTail.next = null;
                            newTab[j] = loHead; //前一半
                        }
                        if (hiTail != null) {
                            hiTail.next = null;
                            newTab[j + oldCap] = hiHead; //后一半
                        }
                    }
                }
            }
        }
        return newTab;
    }
  • Q:为什么最大容量是2的30次方?
  • A:如果是32位整型的最大值(2的31次方-1),就不能构成翻倍扩容
  • Q:会造成Java1.7的多线程死循环问题吗?
  • A:不会,因为都是尾插法,不会形成环形结构
    假设原数组大小为4,阀值也是4。四个冲突的元素链表如下:

    扩容到8的时候,重新hash后得到两个链表:

5.根据key获取value

获取value主要是get()方法

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

    /**
     * Implements Map.get and related methods
     *
     * @param hash hash for key
     * @param key the key
     * @return the node, or null if none
     */
    final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {//根据hash与数组按位与得到第一个节点
            if (first.hash == hash && // always check first node //判断第一个节点与key是否相等或值相等
                ((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 {//在链表中获取节点
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);
            }
        }
        return null;
    }

在树中获取节点getTreeNode()还有待分析

posted @ 2019-02-22 16:47  gitmoji  阅读(243)  评论(0编辑  收藏  举报