Java HashMap expansion process analysis

 

  . 1  Final the Node <K, V> resize () {
   2  
  . 3      // array for storing the re-hash 
  . 4      the Node <K, V> [] newtab;
   . 5      
  . 6      // if the original array is empty, then the resize initialization operation is performed, instead of the expansion operation 
  . 7      IF (Table == null ) {
   . 8          // initial capacity of 16 
  . 9          int CAP = DEFAULT_CAPACITY; // . 1 <<<. 4
 10  
. 11          // initial load factor is 0.75, use 3/4 the capacity that is necessary when re-expansion 
12 is          loadFactor = DEFAULT_LOADFACTOR;
 13 is  
14          // set the expansion threshold 
15          threshold = (int ) (* CAP loadFactor);
 16  
. 17          newtab = (the Node <K, V> []) ( new new the Node [CAP]);
 18 is          return newtab;
 . 19      }
 20 is  
21 is      // ... process details are omitted, the capacity of e.g. how should I do to reach the maximum value, the program segment focuses on the expansion process
 22  
23      // if the original array is not empty, the first expansion, then re-hash of the original data into the array after the new expansion of the 
24-      int OLDCAP = table.length;
 25      int newCap = OLDCAP <<. 1 ;
 26 is      threshold = newCap * loadFactor;
 27      newtab = (the Node <K, V> []) ( new new the Node [newCap]);
 28  
29     // Loop through the original array, re-hashed 
30      for ( int I = 0; I <table.length; I ++ ) {
 31 is  
32          IF (Table [I] == null ) {
 33 is              Continue ;
 34 is          }
 35  
36          the Node <K, V> E = Table [I];
 37 [          Table [I] = null ;
 38 is  
39          // array at each element is a list
 40          // If there is only one element of the list, then it directly into the new position 
41 is          IF (e.next == null ) {
 42 is              newtab [& e.hash (-newCap. 1)] = E;
43 is              Continue ;
 44 is          }
 45  
46 is          // If there are multiple elements of the list, you need to be divided into two lists
 47          // why two lists it? Since the time when the hash & (-OLDCAP. 1) == I,
 48          // hash & (-newCap. 1) has only two values: i + OLDCAP and I
 49          @ proof:
 50          //     Suppose = 16 = 0b10000 OLDCAP
 51 is          //     the <<<. 1 = 16 = newCap 0b100000
 52 is          //     the OLDCAP -. 1 = 0b1111
 53 is          //          newCap -. 1 = 0b11111 makes the
 54 is          //     the hash & (oldCap-1) and hash & (newCap-1) only from the ED fifth different from the left
 55          //     and because 0b10000 = oldCap 
56          //     so & the hash (-newCap. 1) == the hash & (-OLDCAP. 1) == I 
 57 is          //           or & the hash (-newCap. 1) == the hash & (-OLDCAP. 1) + I + OLDCAP OLDCAP ==
 58  
59          @ list 1: in situ, i.e., hash & (newCap-1) = hash & (OLDCAP-1) = I 
60          the Node <K, V> hiHead, hiTail;
 61 is          // list 2: on the i + oldCap place, i.e. hash & ( . 1-newCap) = the hash & (-OLDCAP. 1) + I + = OLDCAP OLDCAP 
62 is          the Node <K, V> loHead, loTail;
 63 is          do {
 64              // the original list of elements classified into list 1 and 2
 65              
66              // If the same as the original hash value (I) 
67              IF ((& e.hash (-OLDCAP. 1)) == (& e.hash(newCap-1))) {
 68                  IF (loHead == null ) {
 69                      loHead = E;
 70                  } the else {
 71 is                      loTail.next = E;
 72                  }
 73 is                  loTail = E;
 74              } the else {
 75                  // If the hash values are not the original as (I + OLDCAP) 
76                  IF (hiHead == null ) {
 77                      hiHead = E;
 78                  } the else {
79                      hiTail.next = E;
 80                  }
 81                  hiTail = E;
 82              }
 83              E = e.next;
 84          } the while (! E = null );
 85  
86          IF (hiHead =! Null ) {
 87              hiTail.next = null ;
 88              
89              // in the new location 
90              newtab [I + OLDCAP] = hiHead;
 91 is          }
 92          IF (loHead =!null ) {
 93              loTail.next = null ;
 94  
95              // its place 
96              newtab [I] = loHead;
 97          }
 98      }
 99  
100      return newtab;
 101 }

 

Guess you like

Origin www.cnblogs.com/cbhe/p/12623296.html