首页 > 编程 > Java > 正文

Java源码解析HashMap的resize函数

2019-11-26 09:22:49
字体:
来源:转载
供稿:网友

HashMap的resize函数,用于对HashMap初始化或者扩容。

首先看一下该函数的注释,如下图。从注释中可以看到,该函数的作用是初始化或者使table的size翻倍。如果table是null,那么就申请空间进行初始化。否则,因为我们在使用2的指数的扩张,在原来table的每个位置的元素,在新的table中,他们要么待在原来的位置,要么移动2的指数的偏移。从这里可以看出,扩容前table每个位置上如果有多个元素,元素之间组成链表时,在扩容后,该链表中的元素,有一部分会待在原地,剩下的元素会往后移动2的指数的偏移。

  /**   * 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   **/

接下来看一下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) {      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      newCap = oldThr;    else {        // zero initial threshold signifies using defaults      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<K,V>[] newTab = (Node<K,V>[])new Node[newCap];    table = newTab;    if (oldTab != null) {      for (int j = 0; j < oldCap; ++j) {        Node<K,V> e;        if ((e = oldTab[j]) != null) {          oldTab[j] = null;          if (e.next == null)            newTab[e.hash & (newCap - 1)] = e;          else if (e instanceof TreeNode)            ((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 {              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;  }

扩容的过程分为两部分,第一部分是对threshold和table的初始化或者重新计算,第二部分是对HashMap中的元素进行重新放置。初始化的过程比较简单,基本就是使用默认值,初始化HashMap的各个成员变量。重新计算时,是会申请一个2倍大小的Node数组,用作的新的HashMap的存储空间。

之后的过程是,对原来HashMap中的每一个位置进行遍历,把该位置上的各个元素重新放置到新的table中。所以在循环的过程中,会定义loHead,loTail,hiHead,hiTail,分别表示留着原地的链表的头和尾,移动到更高位置的链表的头和尾。这里需要注意一点,在jdk1.8中,扩容后链表中元素的顺序和扩容前链表中元素的位置,是相同的,并不会像jdk1.7那样会发生逆序。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对武林网的支持。如果你想了解更多相关内容请查看下面相关链接

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表