HashMap源码详细解析

HashMap的继承:

  • HashMap实现了Cloneable接口,所以可以被克隆

  • HashMap实现了Serializable接口,可以被序列化

  • HashMap继承了AbstractMap并实现了Map接口,具有Map接口的所有功能

存储结构:

  • JDK1.7(包括1.7)之前HashMap底层是数组+链表结合而成的高级数据结构,即 链表散列,之所以要使用链表是因为,HashMap在存储数据是通过 key 的 hashCode 经过扰动函数处理过后得到 hash 值,然后通过 (n - 1) & hash 判断当前元素存放的位置(这里的 n 指的是数组的长度),如果当前位置存在元素的话,就判断该元素与要存入的元素的 hash 值以及 key 是否相同,如果相同的话,直接覆盖,不相同就产生了Hash冲突,然后通过拉链法解决冲突,即将Hash值相同但值不同的元素以链表的形式放置在链表的尾部。但是链表长度太长查询效率就会变低,所以在jdk8作了优化

  • 在jdk1.8后,在链表长度大于8且容量>=64,就会进行树化,即链表转红黑树。数组的查询效率为O(1),链表的查询效率是O(k),红黑树的查询效率是O(log n)。所以当元素数量非常多的时候,转化为红黑树能极大地提高查询效率

源码解析(jdk1.8):

/**
     * 默认的初始容量为16
     */
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

    /**
     * 最大的容量为2的30次方
     * The maximum capacity, used if a higher value is implicitly specified
     * by either of the constructors with arguments.
     * MUST be a power of two <= 1<<30.
     */
    static final int MAXIMUM_CAPACITY = 1 << 30;

    /**
     * 默认的装载因子
     * The load factor used when none specified in constructor.
     */
    static final float DEFAULT_LOAD_FACTOR = 0.75f;

    /**
     * 当一个桶中的元素个数大于等于8时进行树化
     * The bin count threshold for using a tree rather than list for a
     * bin.  Bins are converted to trees when adding an element to a
     * bin with at least this many nodes. The value must be greater
     * than 2 and should be at least 8 to mesh with assumptions in
     * tree removal about conversion back to plain bins upon
     * shrinkage.
     */
    static final int TREEIFY_THRESHOLD = 8;

    /**
     * 当一个桶中的元素个数小于等于6时把树转化为链表
     * The bin count threshold for untreeifying a (split) bin during a
     * resize operation. Should be less than TREEIFY_THRESHOLD, and at
     * most 6 to mesh with shrinkage detection under removal.
     */
    static final int UNTREEIFY_THRESHOLD = 6;

    /**
     * 当桶的个数达到64的时候才进行树化
     * The smallest table capacity for which bins may be treeified.
     * (Otherwise the table is resized if too many nodes in a bin.)
     * Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts
     * between resizing and treeification thresholds.
     */
    static final int MIN_TREEIFY_CAPACITY = 64;
    /* ---------------- Fields -------------- */

    /**
     * 存储元素的数组,总是2的幂次倍
     * The table, initialized on first use, and resized as
     * necessary. When allocated, length is always a power of two.
     * (We also tolerate length zero in some operations to allow
     * bootstrapping mechanics that are currently not needed.)
     */
    transient Node<K,V>[] table;

    /**
     * 保存entrySet()的缓存
     * Holds cached entrySet(). Note that AbstractMap fields are used
     * for keySet() and values().
     */
    transient Set<Map.Entry<K,V>> entrySet;

    /**
     * 元素的数量
     * The number of key-value mappings contained in this map.
     */
    transient int size;

    /**
     * 每次扩容和更改map结构的计数器
     * 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;

    /**
     * 临界值 当实际大小(容量*填充因子)超过临界值时,会进行扩容
     * 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;

    /**
     * The load factor for the hash table.
     *加载因子
     * @serial
     */
    final float loadFactor;

1.容量:为数组的长度,即桶的个数,默认为16,最大为2的30次方,当容量达到64时才可以树化

2.加载因子:loadFactor是控制素组存放数据的疏密程度,loadFactor越趋近于1,entry(数组)中存放的元素也就越密集,产生hash冲突的可能性越大,loadFactor太小(越趋趋近0)会导致数组存储空间利用率太低。所以为了二者平衡,官方给了一个相对临界的值:默认0.75,即初始容量 capacity = 16 , loadFactor = 0.75,threshold = capacity * loadFactor = 12,所以在数组长度达到12就会进行扩容,而非达到16才扩容。

3.树化:当容量达到64且链表的长度达到8时进行树化,当链表的长度小于6时反树化

4.Node<K,V>[] table: Node节点中有四个常量:

final int hash;

final K key;

V value;

Node<K,V> next; // 指向下一个节点

HashMap集合的put方法源码解析:

put方法如下:

public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);//hash(key)根据hash方法计算出key的hash值
    }

hash方法如下:

atic final int hash(Object key) {
        int h;
        /**
        *如果key为null,则hash值为0,否则调用key的hashCode()方法
        *并让高16位与整个hash异或,这种做法是为了尽可能减少hash碰撞
        */
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

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;
        //table未初始化或者长度为0,则扩容
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        //(n - 1) & hash 计算元素在哪个桶中
        //如果这个桶中还没有元素,则把这个元素放在桶中的第一个位置
        if ((p = tab[i = (n - 1) & hash]) == null)
             //新建一个节点放在桶中
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e; K k;
            // 如果桶中第一个元素的key与待插入元素的key相同,保存在e中e来记录,后续通过e判断是否直接return
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;
            
            // 如果第一个元素是树节点,则调用树节点的putTreeVal插入元素
            else if (p instanceof TreeNode)
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            //链表
            else {
                // 遍历这个桶对应的链表,binCount用于存储链表中元素的个数
                for (int binCount = 0; ; ++binCount) {
                    // 如果链表遍历完了都没有找到相同key的元素,说明该key对应的元素不存在,则在链表最后插入一个新节点
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        // 如果插入新节点后链表长度大于8,则判断是否需要树化,因为第一个元素没有加到binCount中,所以这里-1
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            // 如果此时数组长度小于64还是会扩容不会树化
                            treeifyBin(tab, hash);
                        break;
                    }
                    // 如果待插入的key在链表中找到了,则退出循环
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key    
                // 记录下旧值
                V oldValue = e.value;  
                // 判断是否需要替换旧值
                if (!onlyIfAbsent || oldValue == null)                
                    // 替换旧值为新值
                    e.value = value;
                afterNodeAccess(e);   
                // 返回旧值
                return oldValue;
            }
        }
        
        // 修改次数加1
        ++modCount;
        // 元素数量加1,判断是否需要扩容
        if (++size > threshold)
            // 扩容
            resize();
        afterNodeInsertion(evict);
        // 没找到元素返回null
        return null;
    }

猜你喜欢

转载自blog.csdn.net/weixin_71243923/article/details/128838778
今日推荐