溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java中怎么利用resize函數(shù)解析HashMap

發(fā)布時間:2021-06-11 15:04:44 來源:億速云 閱讀:111 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關Java中怎么利用resize函數(shù)解析HashMap,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

HashMap的resize函數(shù),用于對HashMap初始化或者擴容。

首先看一下該函數(shù)的注釋,如下圖。從注釋中可以看到,該函數(shù)的作用是初始化或者使table的size翻倍。如果table是null,那么就申請空間進行初始化。否則,因為我們在使用2的指數(shù)的擴張,在原來table的每個位置的元素,在新的table中,他們要么待在原來的位置,要么移動2的指數(shù)的偏移。從這里可以看出,擴容前table每個位置上如果有多個元素,元素之間組成鏈表時,在擴容后,該鏈表中的元素,有一部分會待在原地,剩下的元素會往后移動2的指數(shù)的偏移。

  /**
   * 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數(shù)組,用作的新的HashMap的存儲空間。

之后的過程是,對原來HashMap中的每一個位置進行遍歷,把該位置上的各個元素重新放置到新的table中。所以在循環(huán)的過程中,會定義loHead,loTail,hiHead,hiTail,分別表示留著原地的鏈表的頭和尾,移動到更高位置的鏈表的頭和尾。這里需要注意一點,在jdk1.8中,擴容后鏈表中元素的順序和擴容前鏈表中元素的位置,是相同的,并不會像jdk1.7那樣會發(fā)生逆序。

看完上述內(nèi)容,你們對Java中怎么利用resize函數(shù)解析HashMap有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI