HashMap源码学习笔记(一)

天涯浪子 提交于 2020-01-06 21:40:00

HashMap源码学习笔记(一)

HashMap中的部分属性

    /**
    * The default initial capacity - MUST be a power of two.
    * 默认的初始化容量
    */
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
    
    /**
    * 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;
    
    /**
    * 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.
    * 元素节点从链表转化为树的阈值(注意:还有一个条件是容量必须大于 MIN_TREEIFY_CAPACITY)
    */
    static final int TREEIFY_THRESHOLD = 8;
    
    /**
    * 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;
    
    /**
    * 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;
    
    
    /**
    * 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.)
    * HashMap数据存放处,该数组大小必须是2的幂次方也为0(为什么必须为2的幂次方?)
    */
    transient Node<K,V>[] table;

HashMap的初始化

空构造函数和有参数的构造函数是有区别的。
###空构造函数
源码如下

    Map<String, String> map = new HashMap();
    
    public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    }

空构造函数只是将loadFactor赋予默认值,不做其它处理。当向Map中添加数据时才调用对应的初始化

有参构造函数

    Map<String, String> map = new HashMap(16, 0.75);
    
    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);//该方法保证了容器的容量一定是2的幂次方
    }
    //该方法就是求不小于给定数的2的幂次方数
    //eg:   in  :   3       4       235
    //      out :   4(2^2)  4(2^2)  256(2^8) 
    //思路:2的幂次方数的特征是只有最高位为1,其它位都为0
    //将所有位全部设为1。再加1就获得2的幂次方数。
    static final int tableSizeFor(int cap) {
        //int类型的2进制数位32位
        int n = cap - 1;//这一个步骤是为了当入参本身就是2的幂次方数的时候。
        n |= n >>> 1;//保证最高位2位为1
        n |= n >>> 2;//保证最高位4位为1
        n |= n >>> 4;//保证最高位8位为1
        n |= n >>> 8;//保证最高位16位为1
        n |= n >>> 16;//保证最高位32位为1
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }
    

HashMap 添加元素

原理:HashMap底层是一个Node数组。元素的下标index=(n - 1) & hash。其中hash是由hash算法获取,n为容量。
当键值的hash值相同时,底层的数据结构不一样。分为链表和红黑树。
###链表
当键值的hash值相同,且链表长度小于8。新加入的元素放入链表末尾。
当链表的长度大于8时,但是容器的容量小于64。会调用resize()方法进行扩容并且进行数据的重新排列
当链表的长度大于8时,但是容器的容量不小于64。则将链表转化为红黑树

    //hash算法。获取hash值。思考key.hashCode()) ^ (h >>> 16)的用意
    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }
    
     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;
            if ((p = tab[i = (n - 1) & hash]) == null)
                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;//如果新加的数据的键值和该下标的键值相同,直接覆盖该下标的元素的值
                else if (p instanceof TreeNode)
                    e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);//当该节点是树结构时
                else {
                    //当该节点是链表时。
                    for (int binCount = 0; ; ++binCount) {
                        //当前节点为最后一个节点时
                        if ((e = p.next) == null) {
                            p.next = newNode(hash, key, value, null);
                            if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                                treeifyBin(tab, hash);//当前链表的长度大于等于8时,进行数据的重新调整
                            break;
                        }
                         //当前节点的的键值和新加元素的键值相同是
                        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;
                }
            }
            ++modCount;
            if (++size > threshold)
                resize();//扩容并重新调整数据
            afterNodeInsertion(evict);
            return null;
        }


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!