zoukankan      html  css  js  c++  java
  • JDK1.8的HashMap实现原理和源码解析

      哈希表(hash table)也叫散列表,是一种非常重要的数据结构。许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表,本文会对java集合框架中的对应实现HashMap的实现原理进行讲解,然后会对JDK8的HashMap源码进行分析。

    一、什么是哈希表

    先了解下基本数据结构。

    1. 数组:采用一段连续的存储单元来存储数据。对于指定下标的查找,时间复杂度为O(1);通过给定值进行查找,需要遍历数组,逐一比对给定关键字和数组元素,时间复杂度为O(n),对于一般的插入删除操作,涉及到数组元素的移动,其平均复杂度也为O(n)。
    2. 线性链表:对于链表的新增,删除等操作(在找到指定操作位置后),仅需处理结点间的引用即可,时间复杂度为O(1),而查找操作需要遍历链表逐一进行比对,复杂度为O(n)。
    3. 二叉树:对一棵相对平衡的有序二叉树,对其进行插入,查找,删除等操作,平均复杂度均为O(logn)。
    4. 哈希表:相比上述几种数据结构,在哈希表中进行添加,删除,查找等操作,性能十分之高,不考虑哈希冲突的情况下,仅需一次定位即可完成,时间复杂度为O(1)。

    我们知道,数据结构的物理存储结构只有两种:顺序存储结构链式存储结构(像栈,队列,树,图等是从逻辑结构去抽象的,映射到内存中,也这两种物理组织形式),而在上面我们提到过,在数组中根据下标查找某个元素,一次定位就可以达到,哈希表利用了这种特性,哈希表的主干就是数组存储位置 = f(关键字),f函数就是哈希函数,关键字就是key这个函数的设计好坏会直接影响到哈希表的优劣。

       5.哈希冲突 : 然而万事无完美,如果两个不同的元素,通过哈希函数得出的实际存储地址相同怎么办?也就是说,当我们对某个元素进行哈希运算,得到一个存储地址,然后要进行插入的时候,发现已经被其他元素占用了,其实这就是所谓的哈希冲突,也叫哈希碰撞。哈希冲突的解决方案有多种:开放定址法(发生冲突,继续寻找下一块未被占用的存储地址),再散列函数法,链地址法,而HashMap即是采用了链地址法,也就是数组+链表的方式。

    二、HashMap实现原理

      HashMap的主干是一个Node数组。Node是HashMap的基本组成单元,每一个Node包含一个key-value键值对。

    /**
      * 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.)
      * 第一次使用的时候才进行初始化,如果有需要会重新调整大小,当重新分配内存的时候,数组长度总是2的次方
      */
      transient Node<K,V>[] table;
    

       Node是HashMap中的一个静态内部类。代码如下:

    /**
     * Basic hash bin node, used for most entries.  (See below for
     * TreeNode subclass, and in LinkedHashMap for its Entry subclass.)
     */
    // 与1.7中 Entry的内容大同小异,只是换了个名称而已!
    static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;  //对key的hashcode值进行hash运算后得到的值,存储在Node,避免重复计算
        final K key;
        V value;
        Node<K,V> next;  //存储指向下一个Node的引用
    
        Node(int hash, K key, V value, Node<K,V> next) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = next;
        }
    
        public final K getKey()        { return key; }
        public final V getValue()      { return value; }
        public final String toString() { return key + "=" + value; }
    
        public final int hashCode() {
            return Objects.hashCode(key) ^ Objects.hashCode(value);
        }
    
        public final V setValue(V newValue) {
            V oldValue = value;
            value = newValue;
            return oldValue;
        }
    
        public final boolean equals(Object o) {
            if (o == this)
                return true;
            if (o instanceof Map.Entry) {
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
                if (Objects.equals(key, e.getKey()) &&
                    Objects.equals(value, e.getValue()))
                    return true;
            }
            return false;
        }
    }
    

      几个重要属性:

     /**
      * The default initial capacity - MUST be a power of two.
      * 默认初始化容量大小16,容量大小必须是2的次方
      */
     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.
      * 最大的容量为 2^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 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). 
      * 用于快速失败,由于HashMap非线程安全,在对HashMap进行迭代时,如果期间其他线程的参与导致HashMap的结构发生变化了
      *(比如put,remov*e等操作),需要抛出异常ConcurrentModificationException
      */
     transient int modCount;
     /**
      * The next size value at which to resize (capacity * load factor).
      *
      * @serial
      *阈值,当table == {}时,该值为初始容量(初始容量默认为16);当table被填充了,也就是为table分配内存空间后,
      *threshold一般为capacity*loadFactory。HashMap在进行扩容时需要参考threshold
      */
     int threshold;
    /**
      * 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.
      *当一个bucket是一个链表,链表个数大于等于8时,就要树状化,也就是要从链表结构变成红黑树结构
      */
     static final int TREEIFY_THRESHOLD = 8;

      HashMap构造函数:有4个构造器,其他构造器如果用户没有传入initialCapacity 和loadFactor这两个参数,会使用默认值initialCapacity默认为16,loadFactory默认为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);
        }
    //指定初始容量
    public HashMap(int initialCapacity) {
            this(initialCapacity, DEFAULT_LOAD_FACTOR);
        }
    //无参
    public HashMap() {
            this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
        }
    //将已存在的map放进去进行初始化,若为空则抛null异常
    public HashMap(Map<? extends K, ? extends V> m) {
            this.loadFactor = DEFAULT_LOAD_FACTOR;
            putMapEntries(m, false);
        }
    

    在常规构造器HashMap()中,没有为数组table分配内存空间,而是在执行put操作的时候才真正构建table数组。以下是put方法:

    // 如果已经存在key对应的节点,则覆盖value值
    public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }
    // 重写,putVal的第四个参数onlyIfAbsent=true,如果已经存在key对应的节点,不覆盖value值
    @Override
    public V putIfAbsent(K key, V value) {
        return putVal(hash(key), key, value, true, true);
    }
    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) // 如果map为空时,调用resize()进行初始化!
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null) // 如果没有在数组中找到对应的节点,则直接插入一个Node (未发生碰撞)
            tab[i] = newNode(hash, key, value, null);
        else {     // 找到了(n - 1) & hash 对应下标的数组(tab)中的节点 ,也就是发生了碰撞
            Node<K,V> e; K k;
    
            // 1. hash值一样,key值一样,则找到目标Node
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                   e = p;
            // 2. 数组中找到的这个节点p是TreeNode类型,则需要插入到RBT里面一个节点
            else if (p instanceof TreeNode)
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else {
    
            // 3. 不是TreeNode类型,则表示是一个链表,这里就类似与jdk1.7中的操作
                for (int binCount = 0; ; ++binCount) { // 遍历链表
                    if ((e = p.next) == null) {
    
                        // 4. 此时查找当前链表的次数已经超过7个,则需要链表RBT化!
    
                        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)))) // 5. 找到链表中对应的节点
                        break;
                    p = e;
                }
            }
            // 如果e不为空,则表示在HashMap中找到了对应的节点
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                // 当onlyIfAbsent=false 或者key对应的旧value为空时,用新的value替换旧value
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount; // 操作次数+1
        if (++size > threshold) // hashmap节点个数+1,并判断是否超过阈值,如果超过则重建结构!
            resize();
        afterNodeInsertion(evict);
        return null;
    }
    

    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;//定义临时Node数组型变量,作为hash table  
            //读取hash table的长度  
            int oldCap = (oldTab == null) ? 0 : oldTab.length;  
            int oldThr = threshold;//读取扩容门限  
            int newCap, newThr = 0;//初始化新的table长度和门限值  
            if (oldCap > 0) {  
                //执行到这里,说明table已经初始化  
                if (oldCap >= MAXIMUM_CAPACITY) {  
                    threshold = Integer.MAX_VALUE;  
                    return oldTab;  
                }  
                //二倍扩容,容量和门限值都加倍  
                else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&  
                         oldCap >= DEFAULT_INITIAL_CAPACITY)  
                    newThr = oldThr << 1; // double threshold  
            }  
            else if (oldThr > 0) // initial capacity was placed in threshold  
            //用构造器初始化了门限值,将门限值直接赋给新table容量  
                newCap = oldThr;  
            else {                
     // zero initial threshold signifies using defaults  
    //老的table容量和门限值都为0,初始化新容量,新门限值,在调用hashmap()方式构造容器时,就采用这种方式初始化  
                newCap = DEFAULT_INITIAL_CAPACITY;  
                newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);  
            }  
            if (newThr == 0) {  
                //如果门限值为0,重新设置门限  
                float ft = (float)newCap * loadFactor;  
                newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?  
                          (int)ft : Integer.MAX_VALUE);  
            }  
            threshold = newThr;//更新新门限值为threshold  
            @SuppressWarnings({"rawtypes","unchecked"})  
           //初始化新的table数组  
            Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];  
            table = newTab;  
            //当原来的table不为null时,需要将table[i]中的节点迁移  
            if (oldTab != null) {  
                for (int j = 0; j < oldCap; ++j) {  
                    Node<K,V> e;  
                    //取出链表中第一个节点保存,若不为null,继续下面操作  
                    if ((e = oldTab[j]) != null) {  
                        oldTab[j] = null;//主动释放  
                        if (e.next == null)  
        //链表中只有一个节点,没有后续节点,则直接重新计算在新table中的index,并将此节点存储到新table对应的index位置处  
                            newTab[e.hash & (newCap - 1)] = e;  
                        else if (e instanceof TreeNode)  
                        //若e是红黑树节点,则按红黑树移动  
                            ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);  
                        else { // preserve order  
                        //迁移单链表中的每个节点  
                            Node<K,V> loHead = null, loTail = null;  
                            Node<K,V> hiHead = null, hiTail = null;  
                            Node<K,V> next;  
                            do {  
    //下面这段暂时没有太明白,通过e.hash & oldCap将链表分为两队,参考知乎上的一段解释  
    /** 
    * 把链表上的键值对按hash值分成lo和hi两串,lo串的新索引位置与原先相同[原先位 
    * j],hi串的新索引位置为[原先位置j+oldCap]; 
    * 链表的键值对加入lo还是hi串取决于 判断条件if ((e.hash & oldCap) == 0),因为* capacity是2的幂,所以oldCap为10...0的二进制形式,若判断条件为真,意味着 
    * oldCap为1的那位对应的hash位为0,对新索引的计算没有影响(新索引 
    * =hash&(newCap-*1),newCap=oldCap<<2);若判断条件为假,则 oldCap为1的那位* 对应的hash位为1, 
    * 即新索引=hash&( newCap-1 )= hash&( (oldCap<<2) - 1),相当于多了10...0, 
    * 即 oldCap 
     
    * 例子: 
    * 旧容量=16,二进制10000;新容量=32,二进制100000 
    * 旧索引的计算: 
    * hash = xxxx xxxx xxxy xxxx 
    * 旧容量-1 1111 
    * &运算 xxxx 
    * 新索引的计算: 
    * hash = xxxx xxxx xxxy xxxx 
    * 新容量-1 1 1111 
    * &运算 y xxxx 
    * 新索引 = 旧索引 + y0000,若判断条件为真,则y=0(lo串索引不变),否则y=1(hi串 
    * 索引=旧索引+旧容量10000) 
       */  
      
                                next = e.next;  
                                if ((e.hash & oldCap) == 0) {  
                                    if (loTail == null)  
                                        loHead = e;  
                                    else  
                                        loTail.next = e;  
                                    loTail = e;  
                                }  
                                else {  
                                    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;  
        }  
    

    get函数:

    /** 
         * Returns the value to which the specified key is mapped, 
         * or {@code null} if this map contains no mapping for the key. 
         * 
         * <p>More formally, if this map contains a mapping from a key 
         * {@code k} to a value {@code v} such that {@code (key==null ? k==null : 
         * key.equals(k))}, then this method returns {@code v}; otherwise 
         * it returns {@code null}.  (There can be at most one such mapping.) 
         * 
         * <p>A return value of {@code null} does not <i>necessarily</i> 
         * indicate that the map contains no mapping for the key; it's also 
         * possible that the map explicitly maps the key to {@code null}. 
         * The {@link #containsKey containsKey} operation may be used to 
         * distinguish these two cases. 
         * 
         * @see #put(Object, Object) 
         */  
        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) {  
                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);  
                    do {  
                        if (e.hash == hash &&  
                            ((k = e.key) == key || (key != null && key.equals(k))))  
                            return e;  
                    } while ((e = e.next) != null);  
                }  
            }  
            return null;  
        }  
      
      
      
    /** 
         * Returns <tt>true</tt> if this map contains a mapping for the 
         * specified key. 
         * 
         * @param   key   The key whose presence in this map is to be tested 
         * @return <tt>true</tt> if this map contains a mapping for the specified 
         * key. 
         */  
        public boolean containsKey(Object key) {  
            return getNode(hash(key), key) != null;  
        }  
    

      

    下面主要关注是三个函数:

    • putTreeVal(this, tab, hash, key, value);

    • treeifyBin(tab, hash);

    • treeify().

    原理图:

     参考:https://www.cnblogs.com/chengxiao/p/6059914.html#t1

        https://blog.csdn.net/crazy1235/article/details/75579654

        https://blog.csdn.net/lizhongkaide/article/details/50595719

  • 相关阅读:
    php ReflectionObject类
    is_callable
    Zend Framework: Accessing Request, Response & Router Object from anywhere
    (转)zend异常处理
    call_user_func
    zend framework基础教程6—视图
    php func_get_arg
    zend framework基础教程3—创建index.php
    【算法02】3种方法求解斐波那契数列
    【算法01】寻找丑数
  • 原文地址:https://www.cnblogs.com/harvey2017/p/8886744.html
Copyright © 2011-2022 走看看