上文详解HashMap源码解析(上)介绍了HashMap整体介绍了一下数据结构,次要属性字段,获取数组的索引下标,以及几个构造方法。本文重点解说元素的增加查找扩容等次要办法。

增加元素

put(K key, V value)

public V put(K key, V value) {    return putVal(hash(key), key, value, false, true);}

首先算出key的哈希码,调用hash办法,获取到hash值。

  • 调用putVal()

      /**   * @param hash hash for key       hash 值   * @param key the key             key 值   * @param value the value to put  value 值   * @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                                返回上一个值,如果不存在返回null   */  final V putVal(int hash, K key, V value, boolean onlyIfAbsent,                 boolean evict) {      // 申明一个node数组 tab,node 节点       Node<K,V>[] tab; Node<K,V> p; int n, i;      // 如果 table 为 null 或者 tab的长度为 0 ,|| 两边都要做一下判断,table 为空,或者table的长度为0      if ((tab = table) == null || (n = tab.length) == 0)          // table 初始化          n = (tab = resize()).length;      //  不存在,间接新建一个Node节点      if ((p = tab[i = (n - 1) & hash]) == null)          // 新建节点          tab[i] = newNode(hash, key, value, null);      else {          // 存在节点          Node<K,V> e; K k;          // hash值 和 p 节点的hash值统一,(键值的地址)统一或者(键的值)统一,间接替换          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);                      // 链表个数大于等于 8,因为从零开始所以要减一                      if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st                          // 转成红黑树                          treeifyBin(tab, hash);                      break;                  }                  // hash统一 或者 值统一                   if (e.hash == hash &&                      ((k = e.key) == key || (key != null && key.equals(k))))                      break;                  p = e;              }          }         // 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;  }
  • 首先判断哈希数组table是否为null,如果为null,就扩容。
  • (n - 1) & hash对应的下标是否存在节点。

    • 不存在节点,就创立新的节点并赋值。
    • 存在节点

      • 节点key值是否相等,相等就替换 value
      • 是否为红黑树,增加数据到红黑树中。
      • 下面都不合乎,就是一般链表,遍历链表,如果链表存在雷同key就替换,否则在链表最初增加数据。

流程图:

putAll(Map<? extends K, ? extends V> m)

putAll 是将汇合元素全副增加到HashMap中,putAll调用了putMapEntries办法,putMapEntries先判断是否须要扩容,而后遍历元素,调用putVal增加元素,上面是增加元素代码:

for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {    K key = e.getKey();    V value = e.getValue();    putVal(hash(key), key, value, false, evict);}

获取数据

get(Object key)

通过key找到哈希表的中Node节点的value值。

// 返回map映射对应的value值public V get(Object key) {    Node<K,V> e;    return (e = getNode(hash(key), key)) == null ? null : e.value;}

首先应用hash办法算出哈希值,而后再调用getNode()获取数据:

final Node<K,V> getNode(int hash, Object key) {    Node<K,V>[] tab; Node<K,V> first, e; int n; K k;    // 判断tab有数据,并且对应下标存在数据    if ((tab = table) != null && (n = tab.length) > 0 &&        (first = tab[(n - 1) & hash]) != null) {        // hash相等以及key相等(key地址相等或者key的值相等),找的就是第一个元素        if (first.hash == hash && // always check first node            ((k = first.key) == key || (key != null && key.equals(k))))            return first;        // 遍历链表            if ((e = first.next) != null) {            // 红黑树找到以后key所在的节点地位             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;}
  • 判断哈希数组是否不为null并且数组下标(n - 1) & hash处不为null,如果都有值,就查问首节点first,否则返回null
  • 找到首节点,匹配上相等的hashkey,返回首节点。
  • 链表有多个元素,是否为红黑树

    • 是红黑树,在红黑树查找
    • 不是红黑树,就遍历一般链表,直到匹配到雷同的hashkey值。

流程图:

resize 扩容

当哈希数组为null,或元素个数超过了阈值,就调用resize扩容办法:

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) {        // 数组长度大于或者等于MAXIMUM_CAPACITY(1>>30)不做扩容操作。        if (oldCap >= MAXIMUM_CAPACITY) {            threshold = Integer.MAX_VALUE;            return oldTab;        }        // 扩容后长度小于MAXIMUM_CAPACITY(1>>30)并且数组原来长度大于16        // 阈值和新容量都翻倍        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        newCap = oldThr;    else {               // zero initial threshold signifies using defaults        // oldCap 和 oldThr 都小于等于0,阐明是调用无参构造方法,赋值默认容量16,默认阈值12。        newCap = DEFAULT_INITIAL_CAPACITY;        newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);    }    if (newThr == 0) {        // 新阈值为零,计算阈值        float ft = (float)newCap * loadFactor;        newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?                  (int)ft : Integer.MAX_VALUE);    }    threshold = newThr;    @SuppressWarnings({"rawtypes","unchecked"})    // 新建Node数组。调用无参构造方法,并不会创立数组,在第一次调用put办法,才会调用resize办法,才会创立数组,提早加载,提高效率。    Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];    table = newTab;    // 原来的数组不为空,把原来的数组的元素重新分配到新的数组中    // 如果是第一次调用resize办法,就不须要重新分配数组。    if (oldTab != null) {        // 旧数组遍历         for (int j = 0; j < oldCap; ++j) {            Node<K,V> e;            // 存在下标下的第一个元素            if ((e = oldTab[j]) != null) {                oldTab[j] = null;                // 以后元素下一个元素为空,阐明此处只有一个元素,间接应用元素的hash值和新数组的容量取模,取得新下标的地位                if (e.next == null)                    newTab[e.hash & (newCap - 1)] = e;                // 红黑树,拆分红黑树,必要时可能进化为链表                    else if (e instanceof TreeNode)                    ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);                // 长度大于1的一般链表                    else { // preserve order                    // loHead、loTail别离代表旧地位的头尾节点                    Node<K,V> loHead = null, loTail = null;                    // hiHead、hiTail别离代表新地位的头尾节点                    Node<K,V> hiHead = null, hiTail = null;                    Node<K,V> next;                    // 遍历链表                    do {                        next = e.next;                        // & 与运算,两个都会1,后果才为1                        // 元素的hash值和oldCap与运算为0,原地位不变                        if ((e.hash & oldCap) == 0) {                            if (loTail == null)                                loHead = e;                            else                                loTail.next = e;                            loTail = e;                        }                        // 挪动到原来地位 + oldCap                        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;}
  • 原容量是否为空

    • 不为空,是否大于最大容量

      • 大于最大容量,不做扩容
      • 小于最大容量,并且大于默认容量16。阈值和容量都翻倍。
    • 为空,原阈值大于零, 就阈值赋值给新容量。
  • 原容量和原阈值都小于等于零,赋值默认容量16和默认阈值12。
  • 做完阈值和容量的赋值之后,遍历数组。
  • 有值,是否只有一个元素,如果是就放入新数组n-1&hash下标处。
  • 如果是红黑树就拆分红黑树。
  • 下面两个都不合乎就是一般链表。
  • 遍历链表,如果hash&数组原长度为0

    • 放在数组原下标处。
    • 不为零,放在原地位+原数组长度处。

流程图:

总结

本文次要解说了元素的增加查找扩容等次要办法,其中增加查问都须要先获取数组的下标,而后进行对应的操作。

put增加

  • 首次增加数据须要对数组进行扩容。
  • 对应下标是否有值

    • 没有值,间接赋值
    • 有值

      • key统一,替换value值。
      • key不统一

        • 是红黑树,在红黑树增加数据。
        • 不是红黑树,就是链表,遍历链表,存在雷同节点key,替换。否者增加在链表的尾部。

get查问

  • 下标是否有值

    • 没有值,返回null
    • 有值
      *hashkey相等的话,返回节点。

      • 是否是多链表。

        • 不是,返回null
        • 是的话,是否是红黑树。

          • 红黑树,在红黑树中查找
          • 否则就是一般链表,遍历链表晓得匹配到雷同的hashkey

resize 扩容

  • 容量大于零

    • 大于最大容量值,不再扩容。
    • 介于最大和默认容量之间,阈值和容量都翻倍。
  • 初始化的时候,设置默认容量和默认阈值。
  • 遍历原数组
  • 节点有值,并且只有一个值,赋值给新数组n-1&hash处。
  • 如果是红黑树,就拆分红黑树。
  • 以上都不合乎,就是一般链表,遍历链表。因为数组长度都是2的幂次方,扩容后元素的地位*要么是在原地位,要么是在原地位再挪动2次幂的地位

    • hash&与运算原数组长度,等于0,存在原来的地位。
    • 不等于0,就寄存下标原来地位+原数组长度地位处。