美文网首页
HashMap源码分析

HashMap源码分析

作者: 秦汉邮侠 | 来源:发表于2017-12-31 15:17 被阅读3次

    HashMap数据结构

    • HashMap数据结构.png

    HashMap继承图

    • HashMap-class.jpg

    HashMap的主要属性

    • table的默认初始大小16,必须是2的整数次方
        /**
         * The default initial capacity - MUST be a power of two.
         */
        static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
    
    • table的最大长度,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;
    
    • 负载率,已经使用的bucket和table长度的比值
        /**
         * The load factor used when none specified in constructor.
         */
        static final float DEFAULT_LOAD_FACTOR = 0.75f;
    

    HashMap主要方法

    • 求hash值,高位参与运输
        static final int hash(Object key) {
            int h;
            return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
        }
    
    • 设计者想了一个顾全大局的方法(综合考虑了速度、作用、质量),就是把高16bit和低16bit异或了一下。设计者还解释到因为现在大多数的hashCode的分布已经很不错了,就算是发生了碰撞也用O(logn)的tree去做了。仅仅异或一下,既减少了系统的开销,也不会造成的因为高位没有参与下标的计算(table长度比较小时),从而引起的碰撞。
    • 求index,h & (length -1)
    static int indexFor(int h, int length) {  //jdk1.7的实现
         return h & (length-1);  //第三步 取模运算
    }
    tab[i = (n - 1) & hash]  //jdk1.8中实现
    
    • 这样做的原因:位操作的效率高于模的效率
    • 这样做的条件:table的length为2的整数次方时,h % length 和h & (length-1)是等价的
    • put方法
    public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }
    //onlyIfAbsent控制已存在Key的行为,若为true,则Key存在时不修改
    //evict用于控制插入回调函数的行为,构造函数中调用evict为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;
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e; K k;
           //根节点的Key就是要插入的Key
            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);
                        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;
    }
    
    • HashMap-put.png

    参考来源

    相关文章

      网友评论

          本文标题:HashMap源码分析

          本文链接:https://www.haomeiwen.com/subject/ixzwottx.html