hashmap拡張条件、原理jdk 1.8

5032 ワード

hashmap:
  HashMap           ,           (           ),           ,   HashMap       ,             ArrayList ,           ,             ,       “  ”  ,    ,  hashmap      ,           :                      ,    ,   resize。
    HashMap         ? hashmap            *loadFactor ,        ,loadFactor     0.75,    ,     ,     16,   hashmap       16*0.75=12   ,          2*16=32,     ,                 ,              ,          hashmap      ,                hashmap   。   ,   1000   new HashMap(1000),        new HashMap(1024)   ,    annegu    ,   1000,hashmap         1024。   new HashMap(1024)       ,  0.75*1000 < 1000,        0.75 * size > 1000,       new HashMap(2048)    ,    &   ,    resize   。
        hashmap         
        1.8                        
            
    final Node[] resize() {
         Node[] oldTab = table;  //   
         int oldCap = (oldTab == null) ? 0 : oldTab.length;  //    /oldcap     
         int oldThr = threshold;    //      16*0.75
         int newCap, newThr = 0;    //   haspmap   
         if (oldCap > 0) {         //>0
             if (oldCap >= MAXIMUM_CAPACITY) {   //  i nt MAXIMUM_CAPACITY = 1 << 30;
                 threshold = Integer.MAX_VALUE;  // MAX_VALUE = 0x7fffffff  ==2147483647;
                 return oldTab;
             }
             else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
                      oldCap >= DEFAULT_INITIAL_CAPACITY)   //      :1. cap=oldcap2 ,    1 << 30
                 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) {    //==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[] newTab = (Node[])new Node[newCap];       //       
         table = newTab;
         if (oldTab != null) {
             for (int j = 0; j < oldCap; ++j) {
                 Node e;
                 if ((e = oldTab[j]) != null) {
                     oldTab[j] = null;
                     if (e.next == null)
                         newTab[e.hash & (newCap - 1)] = e; //(e.hash & newCap-1)       
                     else if (e instanceof TreeNode)//     
                         ((TreeNode)e).split(this, newTab, j, oldCap);
                     else { // preserve order //      
                         Node loHead = null, loTail = null; //  hash   ((e.hash & oldCap) == 0))          
                         Node hiHead = null, hiTail = null;//  hash    ((e.hash & oldCap) == 0))         
                         Node next;
                         do {                    //      
                             next = e.next;    //           
                             if ((e.hash & oldCap) == 0) {  //e.hash & oldCap     ( oldCap=8,hash 3,11,19,27 ,(e.hash & oldCap)    0,8,0,8,
                                                                   3,19      
                                                               ,index 3; 11,27      ,    index 3+8;)
                                 if (loTail == null)        //loTail  ,                
                                     loHead = e;            //         
                                 else                             
                                     loTail.next = e;                  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;
     }