1 package java.util; 2 3 import sun.misc.SharedSecrets; 4 5 import java.io.IOException; 6 import java.io.InvalidObjectException; 7 import java.io.Serializable; 8 import java.lang.reflect.ParameterizedType; 9 import java.lang.reflect.Type; 10 import java.util.function.BiConsumer; 11 import java.util.function.BiFunction; 12 import java.util.function.Consumer; 13 import java.util.function.Function; 14 15 /** 16 * HashMap是經常使用的Java集合之一,是基於哈希表的Map接口的實現。與HashTable主要區別爲不支持同步和容許null做爲key和value。 17 * HashMap非線程安全,即任一時刻能夠有多個線程同時寫HashMap,可能會致使數據的不一致。 18 * 若是須要知足線程安全,能夠用 Collections的synchronizedMap方法使HashMap具備線程安全的能力,或者使用ConcurrentHashMap。 19 * 在JDK1.6中,HashMap採用數組+鏈表實現,即便用鏈表處理衝突,同一hash值的鏈表都存儲在一個鏈表裏。 20 * 可是當位於一個數組中的元素較多,即hash值相等的元素較多時,經過key值依次查找的效率較低。 21 * 而JDK1.8中,HashMap採用數組+鏈表+紅黑樹實現,當鏈表長度超過閾值8時,將鏈表轉換爲紅黑樹,這樣大大減小了查找時間。 22 * 本來Map.Entry接口的實現類Entry更名爲了Node。轉化爲紅黑樹時改用另外一種實現TreeNode。 23 */ 24 public class HashMap<K, V> extends AbstractMap<K, V> 25 implements Map<K, V>, Cloneable, Serializable { 26 27 private static final long serialVersionUID = 362498820763181265L; 28 29 30 /** 31 * 默認的初始容量(容量爲HashMap中槽的數目)是16,且實際容量必須是2的整數次冪。 32 */ 33 static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 34 35 /** 36 * 最大容量(必須是2的冪且小於2的30次方,傳入容量過大將被這個值替換) 37 */ 38 static final int MAXIMUM_CAPACITY = 1 << 30; 39 40 /** 41 * 默認裝填因子0.75,若是當前鍵值對個數 >= HashMap最大容量*裝填因子,進行rehash操做 42 */ 43 static final float DEFAULT_LOAD_FACTOR = 0.75f; 44 45 /** 46 * JDK1.8 新加,Entry鏈表最大長度,當桶中節點數目大於該長度時,將鏈表轉成紅黑樹存儲; 47 */ 48 static final int TREEIFY_THRESHOLD = 8; 49 50 /** 51 * JDK1.8 新加,當桶中節點數小於該長度,將紅黑樹轉爲鏈表存儲; 52 */ 53 static final int UNTREEIFY_THRESHOLD = 6; 54 55 /** 56 * 桶可能被轉化爲樹形結構的最小容量。當哈希表的大小超過這個閾值,纔會把鏈式結構轉化成樹型結構,不然僅採起擴容來嘗試減小衝突。 57 * 應該至少4*TREEIFY_THRESHOLD來避免擴容和樹形結構化之間的衝突。 58 */ 59 static final int MIN_TREEIFY_CAPACITY = 64; 60 61 /** 62 * JDK1.6用Entry描述鍵值對,JDK1.8中用Node代替Entry 63 */ 64 static class Node<K, V> implements Map.Entry<K, V> { 65 // hash存儲key的hashCode 66 final int hash; 67 // final:一個鍵值對的key不可改變 68 final K key; 69 V value; 70 //指向下個節點的引用 71 Node<K, V> next; 72 73 //構造函數 74 Node(int hash, K key, V value, Node<K, V> next) { 75 this.hash = hash; 76 this.key = key; 77 this.value = value; 78 this.next = next; 79 } 80 81 public final K getKey() { 82 return key; 83 } 84 85 public final V getValue() { 86 return value; 87 } 88 89 public final String toString() { 90 return key + "=" + value; 91 } 92 93 public final int hashCode() { 94 return Objects.hashCode(key) ^ Objects.hashCode(value); 95 } 96 97 public final V setValue(V newValue) { 98 V oldValue = value; 99 value = newValue; 100 return oldValue; 101 } 102 103 public final boolean equals(Object o) { 104 if (o == this) 105 return true; 106 if (o instanceof Map.Entry) { 107 Map.Entry<?, ?> e = (Map.Entry<?, ?>) o; 108 if (Objects.equals(key, e.getKey()) && 109 Objects.equals(value, e.getValue())) 110 return true; 111 } 112 return false; 113 } 114 } 115 116 /* ---------------- Static utilities -------------- */ 117 118 /** 119 * HashMap中鍵值對的存儲形式爲鏈表節點,hashCode相同的節點(位於同一個桶)用鏈表組織 120 * hash方法分爲三步: 121 * 1.取key的hashCode 122 * 2.key的hashCode高16位異或低16位 123 * 3.將第一步和第二步獲得的結果進行取模運算。 124 */ 125 static final int hash(Object key) { 126 int h; 127 //計算key的hashCode, h = Objects.hashCode(key) 128 //h >>> 16表示對h無符號右移16位,高位補0,而後h與h >>> 16按位異或 129 return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); 130 } 131 132 /** 133 * 若是參數x實現了Comparable接口,返回參數x的類名,不然返回null 134 */ 135 static Class<?> comparableClassFor(Object x) { 136 if (x instanceof Comparable) { 137 Class<?> c; 138 Type[] ts, as; 139 Type t; 140 ParameterizedType p; 141 if ((c = x.getClass()) == String.class) // bypass checks 142 return c; 143 if ((ts = c.getGenericInterfaces()) != null) { 144 for (int i = 0; i < ts.length; ++i) { 145 if (((t = ts[i]) instanceof ParameterizedType) && 146 ((p = (ParameterizedType) t).getRawType() == 147 Comparable.class) && 148 (as = p.getActualTypeArguments()) != null && 149 as.length == 1 && as[0] == c) // type arg is c 150 return c; 151 } 152 } 153 } 154 return null; 155 } 156 157 /** 158 * 若是x的類型爲kc,則返回k.compareTo(x),不然返回0 159 */ 160 @SuppressWarnings({"rawtypes", "unchecked"}) // for cast to Comparable 161 static int compareComparables(Class<?> kc, Object k, Object x) { 162 return (x == null || x.getClass() != kc ? 0 : 163 ((Comparable) k).compareTo(x)); 164 } 165 166 /** 167 * 結果爲>=cap的最小2的天然數冪 168 */ 169 static final int tableSizeFor(int cap) { 170 //先移位再或運算,最終保證返回值是2的整數冪 171 int n = cap - 1; 172 n |= n >>> 1; 173 n |= n >>> 2; 174 n |= n >>> 4; 175 n |= n >>> 8; 176 n |= n >>> 16; 177 return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; 178 } 179 180 /* ---------------- Fields -------------- */ 181 182 /** 183 * 哈希桶數組,分配的時候,table的長度老是2的冪 184 */ 185 transient Node<K, V>[] table; 186 187 /** 188 * HashMap將數據轉換成set的另外一種存儲形式,這個變量主要用於迭代功能 189 */ 190 transient Set<Map.Entry<K, V>> entrySet; 191 192 /** 193 * 實際存儲的數量,則HashMap的size()方法,實際返回的就是這個值,isEmpty()也是判斷該值是否爲0 194 */ 195 transient int size; 196 197 /** 198 * hashmap結構被改變的次數,fail-fast機制 199 */ 200 transient int modCount; 201 202 /** 203 * HashMap的擴容閾值,在HashMap中存儲的Node鍵值對超過這個數量時,自動擴容容量爲原來的二倍 204 * 205 * @serial 206 */ 207 int threshold; 208 209 /** 210 * HashMap的負加載因子,可計算出當前table長度下的擴容閾值:threshold = loadFactor * table.length 211 * 212 * @serial 213 */ 214 final float loadFactor; 215 216 /* ---------------- Public operations -------------- */ 217 218 /** 219 * 使用指定的初始化容量initial capacity 和加載因子load factor構造一個空HashMap 220 * 221 * @param initialCapacity 初始化容量 222 * @param loadFactor 加載因子 223 * @throws IllegalArgumentException 若是指定的初始化容量爲負數或者加載因子爲非正數 224 */ 225 public HashMap(int initialCapacity, float loadFactor) { 226 if (initialCapacity < 0) 227 throw new IllegalArgumentException("Illegal initial capacity: " + 228 initialCapacity); 229 if (initialCapacity > MAXIMUM_CAPACITY) 230 initialCapacity = MAXIMUM_CAPACITY; 231 if (loadFactor <= 0 || Float.isNaN(loadFactor)) 232 throw new IllegalArgumentException("Illegal load factor: " + 233 loadFactor); 234 this.loadFactor = loadFactor; 235 this.threshold = tableSizeFor(initialCapacity); 236 } 237 238 /** 239 * 使用指定的初始化容量initial capacity和默認加載因子DEFAULT_LOAD_FACTOR(0.75)構造一個空HashMap 240 * 241 * @param initialCapacity 初始化容量 242 * @throws IllegalArgumentException 若是指定的初始化容量爲負數 243 */ 244 public HashMap(int initialCapacity) { 245 this(initialCapacity, DEFAULT_LOAD_FACTOR); 246 } 247 248 /** 249 * 使用指定的初始化容量(16)和默認加載因子DEFAULT_LOAD_FACTOR(0.75)構造一個空HashMap 250 */ 251 public HashMap() { 252 this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted 253 } 254 255 /** 256 * 使用指定Map m構造新的HashMap。使用指定的初始化容量(16)和默認加載因子DEFAULT_LOAD_FACTOR(0.75) 257 * 258 * @param m 指定的map 259 * @throws NullPointerException 若是指定的map是null 260 */ 261 public HashMap(Map<? extends K, ? extends V> m) { 262 this.loadFactor = DEFAULT_LOAD_FACTOR; 263 putMapEntries(m, false); 264 } 265 266 /** 267 * Map.putAll and Map constructor的實現須要的方法 268 * 將m的鍵值對插入本map中 269 * 270 * @param m 指定的map 271 * @param evict 初始化map時使用false,不然使用true 272 */ 273 final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) { 274 int s = m.size(); 275 //若是參數map不爲空 276 if (s > 0) { 277 // 判斷table是否已經初始化 278 if (table == null) { // pre-size 279 // 未初始化,s爲m的實際元素個數 280 float ft = ((float) s / loadFactor) + 1.0F; 281 int t = ((ft < (float) MAXIMUM_CAPACITY) ? 282 (int) ft : MAXIMUM_CAPACITY); 283 // 計算獲得的t大於閾值,則初始化閾值 284 if (t > threshold) 285 //根據容量初始化臨界值 286 threshold = tableSizeFor(t); 287 // 已初始化,而且m元素個數大於閾值,進行擴容處理 288 } else if (s > threshold) 289 //擴容處理 290 resize(); 291 // 將m中的全部元素添加至HashMap中 292 for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) { 293 K key = e.getKey(); 294 V value = e.getValue(); 295 putVal(hash(key), key, value, false, evict); 296 } 297 } 298 } 299 300 /** 301 * 返回map中鍵值對映射的個數 302 * 303 * @return map中鍵值對映射的個數 304 */ 305 public int size() { 306 return size; 307 } 308 309 /** 310 * 若是map中沒有鍵值對映射,返回true 311 * 312 * @return 若是map中沒有鍵值對映射,返回true 313 */ 314 public boolean isEmpty() { 315 return size == 0; 316 } 317 318 /** 319 * 返回指定的key映射的value,若是value爲null,則返回null 320 * get能夠分爲三個步驟: 321 * 1.經過hash(Object key)方法計算key的哈希值hash。 322 * 2.經過getNode( int hash, Object key)方法獲取node。 323 * 3.若是node爲null,返回null,不然返回node.value。 324 * 325 * @see #put(Object, Object) 326 */ 327 public V get(Object key) { 328 Node<K, V> e; 329 //根據key及其hash值查詢node節點,若是存在,則返回該節點的value值 330 return (e = getNode(hash(key), key)) == null ? null : e.value; 331 } 332 333 /** 334 * 根據key的哈希值和key獲取對應的節點 335 * getNode可分爲如下幾個步驟: 336 * 1.若是哈希表爲空,或key對應的桶爲空,返回null 337 * 2.若是桶中的第一個節點就和指定參數hash和key匹配上了,返回這個節點。 338 * 3.若是桶中的第一個節點沒有匹配上,並且有後續節點 339 * 3.1若是當前的桶採用紅黑樹,則調用紅黑樹的get方法去獲取節點 340 * 3.2若是當前的桶不採用紅黑樹,即桶中節點結構爲鏈式結構,遍歷鏈表,直到key匹配 341 * 4.找到節點返回null,不然返回null。 342 * 343 * @param hash 指定參數key的哈希值 344 * @param key 指定參數key 345 * @return 返回node,若是沒有則返回null 346 */ 347 final Node<K, V> getNode(int hash, Object key) { 348 Node<K, V>[] tab; 349 Node<K, V> first, e; 350 int n; 351 K k; 352 //若是哈希表不爲空,並且key對應的桶上不爲空 353 if ((tab = table) != null && (n = tab.length) > 0 && 354 (first = tab[(n - 1) & hash]) != null) { 355 //若是桶中的第一個節點就和指定參數hash和key匹配上了 356 if (first.hash == hash && // always check first node 357 ((k = first.key) == key || (key != null && key.equals(k)))) 358 //返回桶中的第一個節點 359 return first; 360 //若是桶中的第一個節點沒有匹配上,並且有後續節點 361 if ((e = first.next) != null) { 362 //若是當前的桶採用紅黑樹,則調用紅黑樹的get方法去獲取節點 363 if (first instanceof TreeNode) 364 return ((TreeNode<K, V>) first).getTreeNode(hash, key); 365 //若是當前的桶不採用紅黑樹,即桶中節點結構爲鏈式結構 366 do { 367 //遍歷鏈表,直到key匹配 368 if (e.hash == hash && 369 ((k = e.key) == key || (key != null && key.equals(k)))) 370 return e; 371 } while ((e = e.next) != null); 372 } 373 } 374 //若是哈希表爲空,或者沒有找到節點,返回null 375 return null; 376 } 377 378 /** 379 * 若是map中含有key爲指定參數key的鍵值對,返回true 380 * 381 * @param key 指定參數key 382 * @return 若是map中含有key爲指定參數key的鍵值對,返回true 383 * key. 384 */ 385 public boolean containsKey(Object key) { 386 return getNode(hash(key), key) != null; 387 } 388 389 /** 390 * 將指定參數key和指定參數value插入map中,若是key已經存在,那就替換key對應的value 391 * put(K key, V value)能夠分爲三個步驟: 392 * 1.經過hash(Object key)方法計算key的哈希值。 393 * 2.經過putVal(hash(key), key, value, false, true)方法實現功能。 394 * 3.返回putVal方法返回的結果。 395 * 396 * @param key 指定key 397 * @param value 指定value 398 * @return 若是value被替換,則返回舊的value,不然返回null。固然,可能key對應的value就是null 399 */ 400 public V put(K key, V value) { 401 // 倒數第二個參數false:表示容許舊值替換 402 // 最後一個參數true:表示HashMap不處於建立模式 403 return putVal(hash(key), key, value, false, true); 404 } 405 406 /** 407 * Map.put和其餘相關方法的實現須要的方法 408 * putVal方法能夠分爲下面的幾個步驟: 409 * 1.若是哈希表爲空,調用resize()建立一個哈希表。 410 * 2.若是指定參數hash在表中沒有對應的桶,即爲沒有碰撞,直接將鍵值對插入到哈希表中便可。 411 * 3.若是有碰撞,遍歷桶,找到key映射的節點 412 * 3.1桶中的第一個節點就匹配了,將桶中的第一個節點記錄起來。 413 * 3.2若是桶中的第一個節點沒有匹配,且桶中結構爲紅黑樹,則調用紅黑樹對應的方法插入鍵值對。 414 * 3.3若是不是紅黑樹,那麼就確定是鏈表。遍歷鏈表,若是找到了key映射的節點,就記錄這個節點,退出循環。若是沒有找到,在鏈表尾部插入節點。插入後,若是鏈的長度大於TREEIFY_THRESHOLD這個臨界值,則使用treeifyBin方法把鏈表轉爲紅黑樹。 415 * 4.若是找到了key映射的節點,且節點不爲null 416 * 4.1記錄節點的vlaue。 417 * 4.2若是參數onlyIfAbsent爲false,或者oldValue爲null,替換value,不然不替換。 418 * 4.3返回記錄下來的節點的value。 419 * 5.若是沒有找到key映射的節點(二、3步中講了,這種狀況會插入到hashMap中),插入節點後size會加1,這時要檢查size是否大於臨界值threshold,若是大於會使用resize方法進行擴容。 420 * 421 * @param hash 指定參數key的哈希值 422 * @param key 指定參數key 423 * @param value 指定參數value 424 * @param onlyIfAbsent 若是爲true,即便指定參數key在map中已經存在,也不會替換value 425 * @param evict 若是爲false,數組table在建立模式中 426 * @return 若是value被替換,則返回舊的value,不然返回null。固然,可能key對應的value就是null。 427 */ 428 final V putVal(int hash, K key, V value, boolean onlyIfAbsent, 429 boolean evict) { 430 Node<K, V>[] tab; 431 Node<K, V> p; 432 int n, i; 433 //若是哈希表爲空,調用resize()建立一個哈希表,並用變量n記錄哈希表長度 434 if ((tab = table) == null || (n = tab.length) == 0) 435 n = (tab = resize()).length; 436 /** 437 * 若是指定參數hash在表中沒有對應的桶,即爲沒有碰撞 438 * Hash函數,(n - 1) & hash 計算key將被放置的槽位 439 * (n - 1) & hash 本質上是hash % n,位運算更快 440 */ 441 if ((p = tab[i = (n - 1) & hash]) == null) 442 //直接將鍵值對插入到map中便可 443 tab[i] = newNode(hash, key, value, null); 444 else {// 桶中已經存在元素 445 Node<K, V> e; 446 K k; 447 // 比較桶中第一個元素(數組中的結點)的hash值相等,key相等 448 if (p.hash == hash && 449 ((k = p.key) == key || (key != null && key.equals(k)))) 450 // 將第一個元素賦值給e,用e來記錄 451 e = p; 452 // 當前桶中無該鍵值對,且桶是紅黑樹結構,按照紅黑樹結構插入 453 else if (p instanceof TreeNode) 454 e = ((TreeNode<K, V>) p).putTreeVal(this, tab, hash, key, value); 455 // 當前桶中無該鍵值對,且桶是鏈表結構,按照鏈表結構插入到尾部 456 else { 457 for (int binCount = 0; ; ++binCount) { 458 // 遍歷到鏈表尾部 459 if ((e = p.next) == null) { 460 p.next = newNode(hash, key, value, null); 461 // 檢查鏈表長度是否達到閾值,達到將該槽位節點組織形式轉爲紅黑樹 462 if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st 463 treeifyBin(tab, hash); 464 break; 465 } 466 // 鏈表節點的<key, value>與put操做<key, value>相同時,不作重複操做,跳出循環 467 if (e.hash == hash && 468 ((k = e.key) == key || (key != null && key.equals(k)))) 469 break; 470 p = e; 471 } 472 } 473 // 找到或新建一個key和hashCode與插入元素相等的鍵值對,進行put操做 474 if (e != null) { // existing mapping for key 475 // 記錄e的value 476 V oldValue = e.value; 477 /** 478 * onlyIfAbsent爲false或舊值爲null時,容許替換舊值 479 * 不然無需替換 480 */ 481 if (!onlyIfAbsent || oldValue == null) 482 e.value = value; 483 // 訪問後回調 484 afterNodeAccess(e); 485 // 返回舊值 486 return oldValue; 487 } 488 } 489 // 更新結構化修改信息 490 ++modCount; 491 // 鍵值對數目超過閾值時,進行rehash 492 if (++size > threshold) 493 resize(); 494 // 插入後回調 495 afterNodeInsertion(evict); 496 return null; 497 } 498 499 /** 500 * 對table進行初始化或者擴容。 501 * 若是table爲null,則對table進行初始化 502 * 若是對table擴容,由於每次擴容都是翻倍,與原來計算(n-1)&hash的結果相比,節點要麼就在原來的位置,要麼就被分配到「原位置+舊容量」這個位置 503 * resize的步驟總結爲: 504 * 1.計算擴容後的容量,臨界值。 505 * 2.將hashMap的臨界值修改成擴容後的臨界值 506 * 3.根據擴容後的容量新建數組,而後將hashMap的table的引用指向新數組。 507 * 4.將舊數組的元素複製到table中。 508 * 509 * @return the table 510 */ 511 final Node<K, V>[] resize() { 512 //新建oldTab數組保存擴容前的數組table 513 Node<K, V>[] oldTab = table; 514 //獲取原來數組的長度 515 int oldCap = (oldTab == null) ? 0 : oldTab.length; 516 //原來數組擴容的臨界值 517 int oldThr = threshold; 518 int newCap, newThr = 0; 519 //若是擴容前的容量 > 0 520 if (oldCap > 0) { 521 //若是原來的數組長度大於最大值(2^30) 522 if (oldCap >= MAXIMUM_CAPACITY) { 523 //擴容臨界值提升到正無窮 524 threshold = Integer.MAX_VALUE; 525 //沒法進行擴容,返回原來的數組 526 return oldTab; 527 //若是如今容量的兩倍小於MAXIMUM_CAPACITY且如今的容量大於DEFAULT_INITIAL_CAPACITY 528 } else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && 529 oldCap >= DEFAULT_INITIAL_CAPACITY) 530 //臨界值變爲原來的2倍 531 newThr = oldThr << 1; 532 } else if (oldThr > 0) //若是舊容量 <= 0,並且舊臨界值 > 0 533 //數組的新容量設置爲老數組擴容的臨界值 534 newCap = oldThr; 535 else { //若是舊容量 <= 0,且舊臨界值 <= 0,新容量擴充爲默認初始化容量,新臨界值爲DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY 536 newCap = DEFAULT_INITIAL_CAPACITY;//新數組初始容量設置爲默認值 537 newThr = (int) (DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);//計算默認容量下的閾值 538 } 539 // 計算新的resize上限 540 if (newThr == 0) {//在當上面的條件判斷中,只有oldThr > 0成立時,newThr == 0 541 //ft爲臨時臨界值,下面會肯定這個臨界值是否合法,若是合法,那就是真正的臨界值 542 float ft = (float) newCap * loadFactor; 543 //當新容量< MAXIMUM_CAPACITY且ft < (float)MAXIMUM_CAPACITY,新的臨界值爲ft,不然爲Integer.MAX_VALUE 544 newThr = (newCap < MAXIMUM_CAPACITY && ft < (float) MAXIMUM_CAPACITY ? 545 (int) ft : Integer.MAX_VALUE); 546 } 547 //將擴容後hashMap的臨界值設置爲newThr 548 threshold = newThr; 549 //建立新的table,初始化容量爲newCap 550 @SuppressWarnings({"rawtypes", "unchecked"}) 551 Node<K, V>[] newTab = (Node<K, V>[]) new Node[newCap]; 552 //修改hashMap的table爲新建的newTab 553 table = newTab; 554 //若是舊table不爲空,將舊table中的元素複製到新的table中 555 if (oldTab != null) { 556 //遍歷舊哈希表的每一個桶,將舊哈希表中的桶複製到新的哈希表中 557 for (int j = 0; j < oldCap; ++j) { 558 Node<K, V> e; 559 //若是舊桶不爲null,使用e記錄舊桶 560 if ((e = oldTab[j]) != null) { 561 //將舊桶置爲null 562 oldTab[j] = null; 563 //若是舊桶中只有一個node 564 if (e.next == null) 565 //將e也就是oldTab[j]放入newTab中e.hash & (newCap - 1)的位置 566 newTab[e.hash & (newCap - 1)] = e; 567 //若是舊桶中的結構爲紅黑樹 568 else if (e instanceof TreeNode) 569 //將樹中的node分離 570 ((TreeNode<K, V>) e).split(this, newTab, j, oldCap); 571 else { //若是舊桶中的結構爲鏈表,鏈表重排,jdk1.8作的一系列優化 572 Node<K, V> loHead = null, loTail = null; 573 Node<K, V> hiHead = null, hiTail = null; 574 Node<K, V> next; 575 //遍歷整個鏈表中的節點 576 do { 577 next = e.next; 578 // 原索引 579 if ((e.hash & oldCap) == 0) { 580 if (loTail == null) 581 loHead = e; 582 else 583 loTail.next = e; 584 loTail = e; 585 } else {// 原索引+oldCap 586 if (hiTail == null) 587 hiHead = e; 588 else 589 hiTail.next = e; 590 hiTail = e; 591 } 592 } while ((e = next) != null); 593 // 原索引放到bucket裏 594 if (loTail != null) { 595 loTail.next = null; 596 newTab[j] = loHead; 597 } 598 // 原索引+oldCap放到bucket裏 599 if (hiTail != null) { 600 hiTail.next = null; 601 newTab[j + oldCap] = hiHead; 602 } 603 } 604 } 605 } 606 } 607 return newTab; 608 } 609 610 /** 611 * 將鏈表轉化爲紅黑樹 612 */ 613 final void treeifyBin(Node<K, V>[] tab, int hash) { 614 int n, index; 615 Node<K, V> e; 616 //若是桶數組table爲空,或者桶數組table的長度小於MIN_TREEIFY_CAPACITY,不符合轉化爲紅黑樹的條件 617 if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY) 618 //擴容 619 resize(); 620 //若是符合轉化爲紅黑樹的條件,並且hash對應的桶不爲null 621 else if ((e = tab[index = (n - 1) & hash]) != null) { 622 // 紅黑樹的頭、尾節點 623 TreeNode<K, V> hd = null, tl = null; 624 //遍歷鏈表 625 do { 626 //替換鏈表node爲樹node,創建雙向鏈表 627 TreeNode<K, V> p = replacementTreeNode(e, null); 628 // 肯定樹頭節點 629 if (tl == null) 630 hd = p; 631 else { 632 p.prev = tl; 633 tl.next = p; 634 } 635 tl = p; 636 } while ((e = e.next) != null); 637 //遍歷鏈表插入每一個節點到紅黑樹 638 if ((tab[index] = hd) != null) 639 hd.treeify(tab); 640 } 641 } 642 643 /** 644 * 將參數map中的全部鍵值對映射插入到hashMap中,若是有碰撞,則覆蓋value。 645 * 646 * @param m 參數map 647 * @throws NullPointerException 若是map爲null 648 */ 649 public void putAll(Map<? extends K, ? extends V> m) { 650 putMapEntries(m, true); 651 } 652 653 /** 654 * 刪除hashMap中key映射的node 655 * remove方法的實現能夠分爲三個步驟: 656 * 1.經過 hash(Object key)方法計算key的哈希值。 657 * 2.經過 removeNode 方法實現功能。 658 * 3.返回被刪除的node的value。 659 * 660 * @param key 參數key 661 * @return 若是沒有映射到node,返回null,不然返回對應的value 662 */ 663 public V remove(Object key) { 664 Node<K, V> e; 665 //根據key來刪除node。removeNode方法的具體實如今下面 666 return (e = removeNode(hash(key), key, null, false, true)) == null ? 667 null : e.value; 668 } 669 670 /** 671 * Map.remove和相關方法的實現須要的方法 672 * removeNode方法的步驟總結爲: 673 * 1.若是數組table爲空或key映射到的桶爲空,返回null。 674 * 2.若是key映射到的桶上第一個node的就是要刪除的node,記錄下來。 675 * 3.若是桶內不止一個node,且桶內的結構爲紅黑樹,記錄key映射到的node。 676 * 4.桶內的結構不爲紅黑樹,那麼桶內的結構就確定爲鏈表,遍歷鏈表,找到key映射到的node,記錄下來。 677 * 5.若是被記錄下來的node不爲null,刪除node,size-1被刪除。 678 * 6.返回被刪除的node。 679 * 680 * @param hash key的哈希值 681 * @param key key的哈希值 682 * @param value 若是 matchValue 爲true,則value也做爲肯定被刪除的node的條件之一,不然忽略 683 * @param matchValue 若是爲true,則value也做爲肯定被刪除的node的條件之一 684 * @param movable 若是爲false,刪除node時不會刪除其餘node 685 * @return 返回被刪除的node,若是沒有node被刪除,則返回null(針對紅黑樹的刪除方法) 686 */ 687 final Node<K, V> removeNode(int hash, Object key, Object value, 688 boolean matchValue, boolean movable) { 689 Node<K, V>[] tab; 690 Node<K, V> p; 691 int n, index; 692 //若是數組table不爲空且key映射到的桶不爲空 693 if ((tab = table) != null && (n = tab.length) > 0 && 694 (p = tab[index = (n - 1) & hash]) != null) { 695 Node<K, V> node = null, e; 696 K k; 697 V v; 698 //若是桶上第一個node的就是要刪除的node 699 if (p.hash == hash && 700 ((k = p.key) == key || (key != null && key.equals(k)))) 701 //記錄桶上第一個node 702 node = p; 703 else if ((e = p.next) != null) {//若是桶內不止一個node 704 //若是桶內的結構爲紅黑樹 705 if (p instanceof TreeNode) 706 //記錄key映射到的node 707 node = ((TreeNode<K, V>) p).getTreeNode(hash, key); 708 else {//若是桶內的結構爲鏈表 709 do {//遍歷鏈表,找到key映射到的node 710 if (e.hash == hash && 711 ((k = e.key) == key || 712 (key != null && key.equals(k)))) { 713 //記錄key映射到的node 714 node = e; 715 break; 716 } 717 p = e; 718 } while ((e = e.next) != null); 719 } 720 } 721 //若是獲得的node不爲null且(matchValue爲false||node.value和參數value匹配) 722 if (node != null && (!matchValue || (v = node.value) == value || 723 (value != null && value.equals(v)))) { 724 //若是桶內的結構爲紅黑樹 725 if (node instanceof TreeNode) 726 //使用紅黑樹的刪除方法刪除node 727 ((TreeNode<K, V>) node).removeTreeNode(this, tab, movable); 728 else if (node == p)//若是桶的第一個node的就是要刪除的node 729 //刪除node 730 tab[index] = node.next; 731 else//若是桶內的結構爲鏈表,使用鏈表刪除元素的方式刪除node 732 p.next = node.next; 733 ++modCount;//結構性修改次數+1 734 --size;//哈希表大小-1 735 afterNodeRemoval(node); 736 return node;//返回被刪除的node 737 } 738 } 739 return null;//若是數組table爲空或key映射到的桶爲空,返回null。 740 } 741 742 /** 743 * 刪除map中全部的鍵值對 744 */ 745 public void clear() { 746 Node<K, V>[] tab; 747 modCount++; 748 if ((tab = table) != null && size > 0) { 749 size = 0; 750 for (int i = 0; i < tab.length; ++i) 751 tab[i] = null; 752 } 753 } 754 755 /** 756 * 若是hashMap中的鍵值對有一對或多對的value爲參數value,返回true 757 * 758 * @param value 參數value 759 * @return 若是hashMap中的鍵值對有一對或多對的value爲參數value,返回true 760 */ 761 public boolean containsValue(Object value) { 762 Node<K, V>[] tab; 763 V v; 764 if ((tab = table) != null && size > 0) { 765 //遍歷數組table 766 for (int i = 0; i < tab.length; ++i) { 767 //遍歷桶中的node 768 for (Node<K, V> e = tab[i]; e != null; e = e.next) { 769 if ((v = e.value) == value || 770 (value != null && value.equals(v))) 771 return true; 772 } 773 } 774 } 775 return false; 776 } 777 778 /** 779 * 返回hashMap中全部key的視圖。 780 * 改變hashMap會影響到set,反之亦然。 781 * 若是當迭代器迭代set時,hashMap被修改(除非是迭代器本身的remove()方法),迭代器的結果是不肯定的。 782 * set支持元素的刪除,經過Iterator.remove、Set.remove、removeAll、retainAll、clear操做刪除hashMap中對應的鍵值對。 783 * 不支持add和addAll方法。 784 * 785 * @return 返回hashMap中全部key的set視圖 786 */ 787 public Set<K> keySet() { 788 Set<K> ks = keySet; 789 if (ks == null) { 790 ks = new KeySet(); 791 keySet = ks; 792 } 793 return ks; 794 } 795 796 /** 797 * 內部類KeySet 798 */ 799 final class KeySet extends AbstractSet<K> { 800 public final int size() { 801 return size; 802 } 803 804 public final void clear() { 805 HashMap.this.clear(); 806 } 807 808 public final Iterator<K> iterator() { 809 return new KeyIterator(); 810 } 811 812 public final boolean contains(Object o) { 813 return containsKey(o); 814 } 815 816 public final boolean remove(Object key) { 817 return removeNode(hash(key), key, null, false, true) != null; 818 } 819 820 public final Spliterator<K> spliterator() { 821 return new KeySpliterator<>(HashMap.this, 0, -1, 0, 0); 822 } 823 824 public final void forEach(Consumer<? super K> action) { 825 Node<K, V>[] tab; 826 if (action == null) 827 throw new NullPointerException(); 828 if (size > 0 && (tab = table) != null) { 829 int mc = modCount; 830 for (int i = 0; i < tab.length; ++i) { 831 for (Node<K, V> e = tab[i]; e != null; e = e.next) 832 action.accept(e.key); 833 } 834 if (modCount != mc) 835 throw new ConcurrentModificationException(); 836 } 837 } 838 } 839 840 /** 841 * 返回hashMap中全部value的collection視圖 842 * 改變hashMap會改變collection,反之亦然。 843 * 若是當迭代器迭代collection時,hashMap被修改(除非是迭代器本身的remove()方法),迭代器的結果是不肯定的。 844 * collection支持元素的刪除,經過Iterator.remove、Collection.remove、removeAll、retainAll、clear操做刪除hashMap中對應的鍵值對。 845 * 不支持add和addAll方法。 846 * 847 * @return 返回hashMap中全部key的collection視圖 848 */ 849 public Collection<V> values() { 850 Collection<V> vs = values; 851 if (vs == null) { 852 vs = new Values(); 853 values = vs; 854 } 855 return vs; 856 } 857 858 /** 859 * 內部類Values 860 */ 861 final class Values extends AbstractCollection<V> { 862 public final int size() { 863 return size; 864 } 865 866 public final void clear() { 867 HashMap.this.clear(); 868 } 869 870 public final Iterator<V> iterator() { 871 return new ValueIterator(); 872 } 873 874 public final boolean contains(Object o) { 875 return containsValue(o); 876 } 877 878 public final Spliterator<V> spliterator() { 879 return new ValueSpliterator<>(HashMap.this, 0, -1, 0, 0); 880 } 881 882 public final void forEach(Consumer<? super V> action) { 883 Node<K, V>[] tab; 884 if (action == null) 885 throw new NullPointerException(); 886 if (size > 0 && (tab = table) != null) { 887 int mc = modCount; 888 for (int i = 0; i < tab.length; ++i) { 889 for (Node<K, V> e = tab[i]; e != null; e = e.next) 890 action.accept(e.value); 891 } 892 if (modCount != mc) 893 throw new ConcurrentModificationException(); 894 } 895 } 896 } 897 898 /** 899 * 返回hashMap中全部鍵值對的set視圖 900 * 改變hashMap會影響到set,反之亦然。 901 * 若是當迭代器迭代set時,hashMap被修改(除非是迭代器本身的remove()方法),迭代器的結果是不肯定的。 902 * set支持元素的刪除,經過Iterator.remove、Set.remove、removeAll、retainAll、clear操做刪除hashMap中對應的鍵值對。 903 * 不支持add和addAll方法。 904 * 905 * @return 返回hashMap中全部鍵值對的set視圖 906 */ 907 public Set<Map.Entry<K, V>> entrySet() { 908 Set<Map.Entry<K, V>> es; 909 return (es = entrySet) == null ? (entrySet = new EntrySet()) : es; 910 } 911 912 /** 913 * 內部類EntrySet 914 */ 915 final class EntrySet extends AbstractSet<Map.Entry<K, V>> { 916 public final int size() { 917 return size; 918 } 919 920 public final void clear() { 921 HashMap.this.clear(); 922 } 923 924 public final Iterator<Map.Entry<K, V>> iterator() { 925 return new EntryIterator(); 926 } 927 928 public final boolean contains(Object o) { 929 if (!(o instanceof Map.Entry)) 930 return false; 931 Map.Entry<?, ?> e = (Map.Entry<?, ?>) o; 932 Object key = e.getKey(); 933 Node<K, V> candidate = getNode(hash(key), key); 934 return candidate != null && candidate.equals(e); 935 } 936 937 public final boolean remove(Object o) { 938 if (o instanceof Map.Entry) { 939 Map.Entry<?, ?> e = (Map.Entry<?, ?>) o; 940 Object key = e.getKey(); 941 Object value = e.getValue(); 942 return removeNode(hash(key), key, value, true, true) != null; 943 } 944 return false; 945 } 946 947 public final Spliterator<Map.Entry<K, V>> spliterator() { 948 return new EntrySpliterator<>(HashMap.this, 0, -1, 0, 0); 949 } 950 951 public final void forEach(Consumer<? super Map.Entry<K, V>> action) { 952 Node<K, V>[] tab; 953 if (action == null) 954 throw new NullPointerException(); 955 if (size > 0 && (tab = table) != null) { 956 int mc = modCount; 957 for (int i = 0; i < tab.length; ++i) { 958 for (Node<K, V> e = tab[i]; e != null; e = e.next) 959 action.accept(e); 960 } 961 if (modCount != mc) 962 throw new ConcurrentModificationException(); 963 } 964 } 965 } 966 967 // JDK8重寫的方法 968 969 /** 970 * 經過key映射到對應node,若是沒映射到則返回默認值defaultValue 971 * 972 * @param key 973 * @param defaultValue 974 * @return key映射到對應的node,若是沒映射到則返回默認值defaultValue 975 */ 976 @Override 977 public V getOrDefault(Object key, V defaultValue) { 978 Node<K, V> e; 979 return (e = getNode(hash(key), key)) == null ? defaultValue : e.value; 980 } 981 982 /** 983 * 在hashMap中插入參數key和value組成的鍵值對,若是key在hashMap中已經存在,不替換value 984 * 985 * @param key 986 * @param value 987 * @return 若是key在hashMap中不存在,返回舊value 988 */ 989 @Override 990 public V putIfAbsent(K key, V value) { 991 return putVal(hash(key), key, value, true, true); 992 } 993 994 /** 995 * 刪除hashMap中key爲參數key,value爲參數value的鍵值對。若是桶中結構爲樹,則級聯刪除 996 * 997 * @param key 998 * @param value 999 * @return 刪除成功,返回true 1000 */ 1001 @Override 1002 public boolean remove(Object key, Object value) { 1003 return removeNode(hash(key), key, value, true, true) != null; 1004 } 1005 1006 /** 1007 * 使用newValue替換key和oldValue映射到的鍵值對中的value 1008 * 1009 * @param key 1010 * @param oldValue 1011 * @param newValue 1012 * @return 替換成功,返回true 1013 */ 1014 @Override 1015 public boolean replace(K key, V oldValue, V newValue) { 1016 Node<K, V> e; 1017 V v; 1018 if ((e = getNode(hash(key), key)) != null && 1019 ((v = e.value) == oldValue || (v != null && v.equals(oldValue)))) { 1020 e.value = newValue; 1021 afterNodeAccess(e); 1022 return true; 1023 } 1024 return false; 1025 } 1026 1027 /** 1028 * 使用參數value替換key映射到的鍵值對中的value 1029 * 1030 * @param key 1031 * @param value 1032 * @return 替換成功,返回true 1033 */ 1034 @Override 1035 public V replace(K key, V value) { 1036 Node<K, V> e; 1037 if ((e = getNode(hash(key), key)) != null) { 1038 V oldValue = e.value; 1039 e.value = value; 1040 afterNodeAccess(e); 1041 return oldValue; 1042 } 1043 return null; 1044 } 1045 1046 @Override 1047 public V computeIfAbsent(K key, 1048 Function<? super K, ? extends V> mappingFunction) { 1049 if (mappingFunction == null) 1050 throw new NullPointerException(); 1051 int hash = hash(key); 1052 Node<K, V>[] tab; 1053 Node<K, V> first; 1054 int n, i; 1055 int binCount = 0; 1056 TreeNode<K, V> t = null; 1057 Node<K, V> old = null; 1058 if (size > threshold || (tab = table) == null || 1059 (n = tab.length) == 0) 1060 n = (tab = resize()).length; 1061 if ((first = tab[i = (n - 1) & hash]) != null) { 1062 if (first instanceof TreeNode) 1063 old = (t = (TreeNode<K, V>) first).getTreeNode(hash, key); 1064 else { 1065 Node<K, V> e = first; 1066 K k; 1067 do { 1068 if (e.hash == hash && 1069 ((k = e.key) == key || (key != null && key.equals(k)))) { 1070 old = e; 1071 break; 1072 } 1073 ++binCount; 1074 } while ((e = e.next) != null); 1075 } 1076 V oldValue; 1077 if (old != null && (oldValue = old.value) != null) { 1078 afterNodeAccess(old); 1079 return oldValue; 1080 } 1081 } 1082 V v = mappingFunction.apply(key); 1083 if (v == null) { 1084 return null; 1085 } else if (old != null) { 1086 old.value = v; 1087 afterNodeAccess(old); 1088 return v; 1089 } else if (t != null) 1090 t.putTreeVal(this, tab, hash, key, v); 1091 else { 1092 tab[i] = newNode(hash, key, v, first); 1093 if (binCount >= TREEIFY_THRESHOLD - 1) 1094 treeifyBin(tab, hash); 1095 } 1096 ++modCount; 1097 ++size; 1098 afterNodeInsertion(true); 1099 return v; 1100 } 1101 1102 public V computeIfPresent(K key, 1103 BiFunction<? super K, ? super V, ? extends V> remappingFunction) { 1104 if (remappingFunction == null) 1105 throw new NullPointerException(); 1106 Node<K, V> e; 1107 V oldValue; 1108 int hash = hash(key); 1109 if ((e = getNode(hash, key)) != null && 1110 (oldValue = e.value) != null) { 1111 V v = remappingFunction.apply(key, oldValue); 1112 if (v != null) { 1113 e.value = v; 1114 afterNodeAccess(e); 1115 return v; 1116 } else 1117 removeNode(hash, key, null, false, true); 1118 } 1119 return null; 1120 } 1121 1122 @Override 1123 public V compute(K key, 1124 BiFunction<? super K, ? super V, ? extends V> remappingFunction) { 1125 if (remappingFunction == null) 1126 throw new NullPointerException(); 1127 int hash = hash(key); 1128 Node<K, V>[] tab; 1129 Node<K, V> first; 1130 int n, i; 1131 int binCount = 0; 1132 TreeNode<K, V> t = null; 1133 Node<K, V> old = null; 1134 if (size > threshold || (tab = table) == null || 1135 (n = tab.length) == 0) 1136 n = (tab = resize()).length; 1137 if ((first = tab[i = (n - 1) & hash]) != null) { 1138 if (first instanceof TreeNode) 1139 old = (t = (TreeNode<K, V>) first).getTreeNode(hash, key); 1140 else { 1141 Node<K, V> e = first; 1142 K k; 1143 do { 1144 if (e.hash == hash && 1145 ((k = e.key) == key || (key != null && key.equals(k)))) { 1146 old = e; 1147 break; 1148 } 1149 ++binCount; 1150 } while ((e = e.next) != null); 1151 } 1152 } 1153 V oldValue = (old == null) ? null : old.value; 1154 V v = remappingFunction.apply(key, oldValue); 1155 if (old != null) { 1156 if (v != null) { 1157 old.value = v; 1158 afterNodeAccess(old); 1159 } else 1160 removeNode(hash, key, null, false, true); 1161 } else if (v != null) { 1162 if (t != null) 1163 t.putTreeVal(this, tab, hash, key, v); 1164 else { 1165 tab[i] = newNode(hash, key, v, first); 1166 if (binCount >= TREEIFY_THRESHOLD - 1) 1167 treeifyBin(tab, hash); 1168 } 1169 ++modCount; 1170 ++size; 1171 afterNodeInsertion(true); 1172 } 1173 return v; 1174 } 1175 1176 @Override 1177 public V merge(K key, V value, 1178 BiFunction<? super V, ? super V, ? extends V> remappingFunction) { 1179 if (value == null) 1180 throw new NullPointerException(); 1181 if (remappingFunction == null) 1182 throw new NullPointerException(); 1183 int hash = hash(key); 1184 Node<K, V>[] tab; 1185 Node<K, V> first; 1186 int n, i; 1187 int binCount = 0; 1188 TreeNode<K, V> t = null; 1189 Node<K, V> old = null; 1190 if (size > threshold || (tab = table) == null || 1191 (n = tab.length) == 0) 1192 n = (tab = resize()).length; 1193 if ((first = tab[i = (n - 1) & hash]) != null) { 1194 if (first instanceof TreeNode) 1195 old = (t = (TreeNode<K, V>) first).getTreeNode(hash, key); 1196 else { 1197 Node<K, V> e = first; 1198 K k; 1199 do { 1200 if (e.hash == hash && 1201 ((k = e.key) == key || (key != null && key.equals(k)))) { 1202 old = e; 1203 break; 1204 } 1205 ++binCount; 1206 } while ((e = e.next) != null); 1207 } 1208 } 1209 if (old != null) { 1210 V v; 1211 if (old.value != null) 1212 v = remappingFunction.apply(old.value, value); 1213 else 1214 v = value; 1215 if (v != null) { 1216 old.value = v; 1217 afterNodeAccess(old); 1218 } else 1219 removeNode(hash, key, null, false, true); 1220 return v; 1221 } 1222 if (value != null) { 1223 if (t != null) 1224 t.putTreeVal(this, tab, hash, key, value); 1225 else { 1226 tab[i] = newNode(hash, key, value, first); 1227 if (binCount >= TREEIFY_THRESHOLD - 1) 1228 treeifyBin(tab, hash); 1229 } 1230 ++modCount; 1231 ++size; 1232 afterNodeInsertion(true); 1233 } 1234 return value; 1235 } 1236 1237 @Override 1238 public void forEach(BiConsumer<? super K, ? super V> action) { 1239 Node<K, V>[] tab; 1240 if (action == null) 1241 throw new NullPointerException(); 1242 if (size > 0 && (tab = table) != null) { 1243 int mc = modCount; 1244 for (int i = 0; i < tab.length; ++i) { 1245 for (Node<K, V> e = tab[i]; e != null; e = e.next) 1246 action.accept(e.key, e.value); 1247 } 1248 if (modCount != mc) 1249 throw new ConcurrentModificationException(); 1250 } 1251 } 1252 1253 @Override 1254 public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { 1255 Node<K, V>[] tab; 1256 if (function == null) 1257 throw new NullPointerException(); 1258 if (size > 0 && (tab = table) != null) { 1259 int mc = modCount; 1260 for (int i = 0; i < tab.length; ++i) { 1261 for (Node<K, V> e = tab[i]; e != null; e = e.next) { 1262 e.value = function.apply(e.key, e.value); 1263 } 1264 } 1265 if (modCount != mc) 1266 throw new ConcurrentModificationException(); 1267 } 1268 } 1269 1270 /* ------------------------------------------------------------ */ 1271 // 克隆和序列化 1272 1273 /** 1274 * 淺拷貝。 1275 * clone方法雖然生成了新的HashMap對象,新的HashMap中的table數組雖然也是新生成的,可是數組中的元素仍是引用之前的HashMap中的元素。 1276 * 這就致使在對HashMap中的元素進行修改的時候,即對數組中元素進行修改,會致使原對象和clone對象都發生改變,但進行新增或刪除就不會影響對方,由於這至關因而對數組作出的改變,clone對象新生成了一個數組。 1277 * 1278 * @return hashMap的淺拷貝 1279 */ 1280 @SuppressWarnings("unchecked") 1281 @Override 1282 public Object clone() { 1283 HashMap<K, V> result; 1284 try { 1285 result = (HashMap<K, V>) super.clone(); 1286 } catch (CloneNotSupportedException e) { 1287 // this shouldn't happen, since we are Cloneable 1288 throw new InternalError(e); 1289 } 1290 result.reinitialize(); 1291 result.putMapEntries(this, false); 1292 return result; 1293 } 1294 1295 // These methods are also used when serializing HashSets 1296 final float loadFactor() { 1297 return loadFactor; 1298 } 1299 1300 final int capacity() { 1301 return (table != null) ? table.length : 1302 (threshold > 0) ? threshold : 1303 DEFAULT_INITIAL_CAPACITY; 1304 } 1305 1306 /** 1307 * 序列化hashMap到ObjectOutputStream中 1308 * 將hashMap的總容量capacity、實際容量size、鍵值對映射寫入到ObjectOutputStream中。鍵值對映射序列化時是無序的。 1309 * 1310 * @serialData The <i>capacity</i> of the HashMap (the length of the 1311 * bucket array) is emitted (int), followed by the 1312 * <i>size</i> (an int, the number of key-value 1313 * mappings), followed by the key (Object) and value (Object) 1314 * for each key-value mapping. The key-value mappings are 1315 * emitted in no particular order. 1316 */ 1317 private void writeObject(java.io.ObjectOutputStream s) 1318 throws IOException { 1319 int buckets = capacity(); 1320 // Write out the threshold, loadfactor, and any hidden stuff 1321 s.defaultWriteObject(); 1322 //寫入總容量 1323 s.writeInt(buckets); 1324 //寫入實際容量 1325 s.writeInt(size); 1326 //寫入鍵值對 1327 internalWriteEntries(s); 1328 } 1329 1330 /** 1331 * 到ObjectOutputStream中讀取hashMap 1332 * 將hashMap的總容量capacity、實際容量size、鍵值對映射讀取出來 1333 */ 1334 private void readObject(java.io.ObjectInputStream s) 1335 throws IOException, ClassNotFoundException { 1336 // 將hashMap的總容量capacity、實際容量size、鍵值對映射讀取出來 1337 s.defaultReadObject(); 1338 //重置hashMap 1339 reinitialize(); 1340 //若是加載因子不合法,拋出異常 1341 if (loadFactor <= 0 || Float.isNaN(loadFactor)) 1342 throw new InvalidObjectException("Illegal load factor: " + 1343 loadFactor); 1344 s.readInt(); //讀出桶的數量,忽略 1345 int mappings = s.readInt(); //讀出實際容量size 1346 //若是讀出的實際容量size小於0,拋出異常 1347 if (mappings < 0) 1348 throw new InvalidObjectException("Illegal mappings count: " + 1349 mappings); 1350 else if (mappings > 0) { // (if zero, use defaults) 1351 // Size the table using given load factor only if within 1352 // range of 0.25...4.0 1353 //調整hashMap大小 1354 float lf = Math.min(Math.max(0.25f, loadFactor), 4.0f); // 加載因子 1355 float fc = (float) mappings / lf + 1.0f; //初步獲得的總容量,後續還會處理 1356 //處理初步獲得的容量,確認最終的總容量 1357 int cap = ((fc < DEFAULT_INITIAL_CAPACITY) ? 1358 DEFAULT_INITIAL_CAPACITY : 1359 (fc >= MAXIMUM_CAPACITY) ? 1360 MAXIMUM_CAPACITY : 1361 tableSizeFor((int) fc)); 1362 //計算臨界值,獲得初步的臨界值 1363 float ft = (float) cap * lf; 1364 //獲得最終的臨界值 1365 threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ? 1366 (int) ft : Integer.MAX_VALUE); 1367 1368 // Check Map.Entry[].class since it's the nearest public type to 1369 // what we're actually creating. 1370 SharedSecrets.getJavaOISAccess().checkArray(s, Map.Entry[].class, cap); 1371 //新建桶數組table 1372 @SuppressWarnings({"rawtypes", "unchecked"}) 1373 Node<K, V>[] tab = (Node<K, V>[]) new Node[cap]; 1374 table = tab; 1375 1376 // 讀出key和value,並組成鍵值對插入hashMap中 1377 for (int i = 0; i < mappings; i++) { 1378 @SuppressWarnings("unchecked") 1379 K key = (K) s.readObject(); 1380 @SuppressWarnings("unchecked") 1381 V value = (V) s.readObject(); 1382 putVal(hash(key), key, value, false, false); 1383 } 1384 } 1385 } 1386 1387 /* ------------------------------------------------------------ */ 1388 // iterators 1389 1390 abstract class HashIterator { 1391 Node<K, V> next; // next entry to return 1392 Node<K, V> current; // current entry 1393 int expectedModCount; // for fast-fail 1394 int index; // current slot 1395 1396 HashIterator() { 1397 expectedModCount = modCount; 1398 Node<K, V>[] t = table; 1399 current = next = null; 1400 index = 0; 1401 if (t != null && size > 0) { // advance to first entry 1402 do { 1403 } while (index < t.length && (next = t[index++]) == null); 1404 } 1405 } 1406 1407 public final boolean hasNext() { 1408 return next != null; 1409 } 1410 1411 final Node<K, V> nextNode() { 1412 Node<K, V>[] t; 1413 Node<K, V> e = next; 1414 if (modCount != expectedModCount) 1415 throw new ConcurrentModificationException(); 1416 if (e == null) 1417 throw new NoSuchElementException(); 1418 if ((next = (current = e).next) == null && (t = table) != null) { 1419 do { 1420 } while (index < t.length && (next = t[index++]) == null); 1421 } 1422 return e; 1423 } 1424 1425 public final void remove() { 1426 Node<K, V> p = current; 1427 if (p == null) 1428 throw new IllegalStateException(); 1429 if (modCount != expectedModCount) 1430 throw new ConcurrentModificationException(); 1431 current = null; 1432 K key = p.key; 1433 removeNode(hash(key), key, null, false, false); 1434 expectedModCount = modCount; 1435 } 1436 } 1437 1438 final class KeyIterator extends HashIterator 1439 implements Iterator<K> { 1440 public final K next() { 1441 return nextNode().key; 1442 } 1443 } 1444 1445 final class ValueIterator extends HashIterator 1446 implements Iterator<V> { 1447 public final V next() { 1448 return nextNode().value; 1449 } 1450 } 1451 1452 final class EntryIterator extends HashIterator 1453 implements Iterator<Map.Entry<K, V>> { 1454 public final Map.Entry<K, V> next() { 1455 return nextNode(); 1456 } 1457 } 1458 1459 /* ------------------------------------------------------------ */ 1460 // spliterators 1461 1462 static class HashMapSpliterator<K, V> { 1463 final HashMap<K, V> map; 1464 Node<K, V> current; //記錄當前的節點 1465 int index; //當前節點的下標 1466 int fence; //堆大小 1467 int est; //估計大小 1468 int expectedModCount; // for comodification checks 1469 1470 HashMapSpliterator(HashMap<K, V> m, int origin, 1471 int fence, int est, 1472 int expectedModCount) { 1473 this.map = m; 1474 this.index = origin; 1475 this.fence = fence; 1476 this.est = est; 1477 this.expectedModCount = expectedModCount; 1478 } 1479 1480 final int getFence() { // initialize fence and size on first use 1481 int hi; 1482 if ((hi = fence) < 0) { 1483 HashMap<K, V> m = map; 1484 est = m.size; 1485 expectedModCount = m.modCount; 1486 Node<K, V>[] tab = m.table; 1487 hi = fence = (tab == null) ? 0 : tab.length; 1488 } 1489 return hi; 1490 } 1491 1492 public final long estimateSize() { 1493 getFence(); // force init 1494 return (long) est; 1495 } 1496 } 1497 1498 static final class KeySpliterator<K, V> 1499 extends HashMapSpliterator<K, V> 1500 implements Spliterator<K> { 1501 KeySpliterator(HashMap<K, V> m, int origin, int fence, int est, 1502 int expectedModCount) { 1503 super(m, origin, fence, est, expectedModCount); 1504 } 1505 1506 public KeySpliterator<K, V> trySplit() { 1507 int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; 1508 return (lo >= mid || current != null) ? null : 1509 new KeySpliterator<>(map, lo, index = mid, est >>>= 1, 1510 expectedModCount); 1511 } 1512 1513 public void forEachRemaining(Consumer<? super K> action) { 1514 int i, hi, mc; 1515 if (action == null) 1516 throw new NullPointerException(); 1517 HashMap<K, V> m = map; 1518 Node<K, V>[] tab = m.table; 1519 if ((hi = fence) < 0) { 1520 mc = expectedModCount = m.modCount; 1521 hi = fence = (tab == null) ? 0 : tab.length; 1522 } else 1523 mc = expectedModCount; 1524 if (tab != null && tab.length >= hi && 1525 (i = index) >= 0 && (i < (index = hi) || current != null)) { 1526 Node<K, V> p = current; 1527 current = null; 1528 do { 1529 if (p == null) 1530 p = tab[i++]; 1531 else { 1532 action.accept(p.key); 1533 p = p.next; 1534 } 1535 } while (p != null || i < hi); 1536 if (m.modCount != mc) 1537 throw new ConcurrentModificationException(); 1538 } 1539 } 1540 1541 public boolean tryAdvance(Consumer<? super K> action) { 1542 int hi; 1543 if (action == null) 1544 throw new NullPointerException(); 1545 Node<K, V>[] tab = map.table; 1546 if (tab != null && tab.length >= (hi = getFence()) && index >= 0) { 1547 while (current != null || index < hi) { 1548 if (current == null) 1549 current = tab[index++]; 1550 else { 1551 K k = current.key; 1552 current = current.next; 1553 action.accept(k); 1554 if (map.modCount != expectedModCount) 1555 throw new ConcurrentModificationException(); 1556 return true; 1557 } 1558 } 1559 } 1560 return false; 1561 } 1562 1563 public int characteristics() { 1564 return (fence < 0 || est == map.size ? Spliterator.SIZED : 0) | 1565 Spliterator.DISTINCT; 1566 } 1567 } 1568 1569 static final class ValueSpliterator<K, V> 1570 extends HashMapSpliterator<K, V> 1571 implements Spliterator<V> { 1572 ValueSpliterator(HashMap<K, V> m, int origin, int fence, int est, 1573 int expectedModCount) { 1574 super(m, origin, fence, est, expectedModCount); 1575 } 1576 1577 public ValueSpliterator<K, V> trySplit() { 1578 int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; 1579 return (lo >= mid || current != null) ? null : 1580 new ValueSpliterator<>(map, lo, index = mid, est >>>= 1, 1581 expectedModCount); 1582 } 1583 1584 public void forEachRemaining(Consumer<? super V> action) { 1585 int i, hi, mc; 1586 if (action == null) 1587 throw new NullPointerException(); 1588 HashMap<K, V> m = map; 1589 Node<K, V>[] tab = m.table; 1590 if ((hi = fence) < 0) { 1591 mc = expectedModCount = m.modCount; 1592 hi = fence = (tab == null) ? 0 : tab.length; 1593 } else 1594 mc = expectedModCount; 1595 if (tab != null && tab.length >= hi && 1596 (i = index) >= 0 && (i < (index = hi) || current != null)) { 1597 Node<K, V> p = current; 1598 current = null; 1599 do { 1600 if (p == null) 1601 p = tab[i++]; 1602 else { 1603 action.accept(p.value); 1604 p = p.next; 1605 } 1606 } while (p != null || i < hi); 1607 if (m.modCount != mc) 1608 throw new ConcurrentModificationException(); 1609 } 1610 } 1611 1612 public boolean tryAdvance(Consumer<? super V> action) { 1613 int hi; 1614 if (action == null) 1615 throw new NullPointerException(); 1616 Node<K, V>[] tab = map.table; 1617 if (tab != null && tab.length >= (hi = getFence()) && index >= 0) { 1618 while (current != null || index < hi) { 1619 if (current == null) 1620 current = tab[index++]; 1621 else { 1622 V v = current.value; 1623 current = current.next; 1624 action.accept(v); 1625 if (map.modCount != expectedModCount) 1626 throw new ConcurrentModificationException(); 1627 return true; 1628 } 1629 } 1630 } 1631 return false; 1632 } 1633 1634 public int characteristics() { 1635 return (fence < 0 || est == map.size ? Spliterator.SIZED : 0); 1636 } 1637 } 1638 1639 static final class EntrySpliterator<K, V> 1640 extends HashMapSpliterator<K, V> 1641 implements Spliterator<Map.Entry<K, V>> { 1642 EntrySpliterator(HashMap<K, V> m, int origin, int fence, int est, 1643 int expectedModCount) { 1644 super(m, origin, fence, est, expectedModCount); 1645 } 1646 1647 public EntrySpliterator<K, V> trySplit() { 1648 int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; 1649 return (lo >= mid || current != null) ? null : 1650 new EntrySpliterator<>(map, lo, index = mid, est >>>= 1, 1651 expectedModCount); 1652 } 1653 1654 public void forEachRemaining(Consumer<? super Map.Entry<K, V>> action) { 1655 int i, hi, mc; 1656 if (action == null) 1657 throw new NullPointerException(); 1658 HashMap<K, V> m = map; 1659 Node<K, V>[] tab = m.table; 1660 if ((hi = fence) < 0) { 1661 mc = expectedModCount = m.modCount; 1662 hi = fence = (tab == null) ? 0 : tab.length; 1663 } else 1664 mc = expectedModCount; 1665 if (tab != null && tab.length >= hi && 1666 (i = index) >= 0 && (i < (index = hi) || current != null)) { 1667 Node<K, V> p = current; 1668 current = null; 1669 do { 1670 if (p == null) 1671 p = tab[i++]; 1672 else { 1673 action.accept(p); 1674 p = p.next; 1675 } 1676 } while (p != null || i < hi); 1677 if (m.modCount != mc) 1678 throw new ConcurrentModificationException(); 1679 } 1680 } 1681 1682 public boolean tryAdvance(Consumer<? super Map.Entry<K, V>> action) { 1683 int hi; 1684 if (action == null) 1685 throw new NullPointerException(); 1686 Node<K, V>[] tab = map.table; 1687 if (tab != null && tab.length >= (hi = getFence()) && index >= 0) { 1688 while (current != null || index < hi) { 1689 if (current == null) 1690 current = tab[index++]; 1691 else { 1692 Node<K, V> e = current; 1693 current = current.next; 1694 action.accept(e); 1695 if (map.modCount != expectedModCount) 1696 throw new ConcurrentModificationException(); 1697 return true; 1698 } 1699 } 1700 } 1701 return false; 1702 } 1703 1704 public int characteristics() { 1705 return (fence < 0 || est == map.size ? Spliterator.SIZED : 0) | 1706 Spliterator.DISTINCT; 1707 } 1708 } 1709 1710 /* ------------------------------------------------------------ */ 1711 // LinkedHashMap support 1712 1713 1714 /* 1715 * The following package-protected methods are designed to be 1716 * overridden by LinkedHashMap, but not by any other subclass. 1717 * Nearly all other internal methods are also package-protected 1718 * but are declared final, so can be used by LinkedHashMap, view 1719 * classes, and HashSet. 1720 */ 1721 1722 // 建立一個鏈表結點 1723 Node<K, V> newNode(int hash, K key, V value, Node<K, V> next) { 1724 return new Node<>(hash, key, value, next); 1725 } 1726 1727 // 替換一個鏈表節點 1728 Node<K, V> replacementNode(Node<K, V> p, Node<K, V> next) { 1729 return new Node<>(p.hash, p.key, p.value, next); 1730 } 1731 1732 // 建立一個紅黑樹節點 1733 TreeNode<K, V> newTreeNode(int hash, K key, V value, Node<K, V> next) { 1734 return new TreeNode<>(hash, key, value, next); 1735 } 1736 1737 // 替換一個紅黑樹節點 1738 TreeNode<K, V> replacementTreeNode(Node<K, V> p, Node<K, V> next) { 1739 return new TreeNode<>(p.hash, p.key, p.value, next); 1740 } 1741 1742 /** 1743 * Reset to initial default state. Called by clone and readObject. 1744 */ 1745 void reinitialize() { 1746 table = null; 1747 entrySet = null; 1748 keySet = null; 1749 values = null; 1750 modCount = 0; 1751 threshold = 0; 1752 size = 0; 1753 } 1754 1755 // Callbacks to allow LinkedHashMap post-actions 1756 void afterNodeAccess(Node<K, V> p) { 1757 } 1758 1759 void afterNodeInsertion(boolean evict) { 1760 } 1761 1762 void afterNodeRemoval(Node<K, V> p) { 1763 } 1764 1765 // 寫入hashMap鍵值對到ObjectOutputStream中 1766 void internalWriteEntries(java.io.ObjectOutputStream s) throws IOException { 1767 Node<K, V>[] tab; 1768 if (size > 0 && (tab = table) != null) { 1769 for (int i = 0; i < tab.length; ++i) { 1770 for (Node<K, V> e = tab[i]; e != null; e = e.next) { 1771 s.writeObject(e.key); 1772 s.writeObject(e.value); 1773 } 1774 } 1775 } 1776 } 1777 1778 /* ------------------------------------------------------------ */ 1779 // Tree bins 1780 1781 /** 1782 * JDK1.8新增,用來支持桶的紅黑樹結構實現 1783 * 性質1. 節點是紅色或黑色。 1784 * 性質2. 根是黑色。 1785 * 性質3. 全部葉子都是黑色(葉子是NIL節點)。 1786 * 性質4. 每一個紅色節點必須有兩個黑色的子節點。(從每一個葉子到根的全部路徑上不能有兩個連續的紅色節點。) 1787 * 性質5. 從任一節點到其每一個葉子的全部簡單路徑都包含相同數目的黑色節點。 1788 */ 1789 1790 static final class TreeNode<K, V> extends LinkedHashMap.Entry<K, V> { 1791 TreeNode<K, V> parent; //節點的父親 1792 TreeNode<K, V> left; //節點的左孩子 1793 TreeNode<K, V> right; //節點的右孩子 1794 TreeNode<K, V> prev; //節點的前一個節點 1795 boolean red; //true表示紅節點,false表示黑節點 1796 1797 TreeNode(int hash, K key, V val, Node<K, V> next) { 1798 super(hash, key, val, next); 1799 } 1800 1801 /** 1802 * 獲取紅黑樹的根 1803 */ 1804 final TreeNode<K, V> root() { 1805 for (TreeNode<K, V> r = this, p; ; ) { 1806 if ((p = r.parent) == null) 1807 return r; 1808 r = p; 1809 } 1810 } 1811 1812 /** 1813 * 確保root是桶中的第一個元素 ,將root移到中中的第一個 1814 */ 1815 static <K, V> void moveRootToFront(Node<K, V>[] tab, TreeNode<K, V> root) { 1816 int n; 1817 if (root != null && tab != null && (n = tab.length) > 0) { 1818 int index = (n - 1) & root.hash; 1819 TreeNode<K, V> first = (TreeNode<K, V>) tab[index]; 1820 if (root != first) { 1821 Node<K, V> rn; 1822 tab[index] = root; 1823 TreeNode<K, V> rp = root.prev; 1824 if ((rn = root.next) != null) 1825 ((TreeNode<K, V>) rn).prev = rp; 1826 if (rp != null) 1827 rp.next = rn; 1828 if (first != null) 1829 first.prev = root; 1830 root.next = first; 1831 root.prev = null; 1832 } 1833 assert checkInvariants(root); 1834 } 1835 } 1836 1837 /** 1838 * 查找hash爲h,key爲k的節點 1839 */ 1840 final TreeNode<K, V> find(int h, Object k, Class<?> kc) { 1841 TreeNode<K, V> p = this; 1842 do { 1843 int ph, dir; 1844 K pk; 1845 TreeNode<K, V> pl = p.left, pr = p.right, q; 1846 if ((ph = p.hash) > h) 1847 p = pl; 1848 else if (ph < h) 1849 p = pr; 1850 else if ((pk = p.key) == k || (k != null && k.equals(pk))) 1851 return p; 1852 else if (pl == null) 1853 p = pr; 1854 else if (pr == null) 1855 p = pl; 1856 else if ((kc != null || 1857 (kc = comparableClassFor(k)) != null) && 1858 (dir = compareComparables(kc, k, pk)) != 0) 1859 p = (dir < 0) ? pl : pr; 1860 else if ((q = pr.find(h, k, kc)) != null) 1861 return q; 1862 else 1863 p = pl; 1864 } while (p != null); 1865 return null; 1866 } 1867 1868 /** 1869 * 獲取樹節點,經過根節點查找 1870 */ 1871 final TreeNode<K, V> getTreeNode(int h, Object k) { 1872 return ((parent != null) ? root() : this).find(h, k, null); 1873 } 1874 1875 /** 1876 * 比較2個對象的大小 1877 */ 1878 static int tieBreakOrder(Object a, Object b) { 1879 int d; 1880 if (a == null || b == null || 1881 (d = a.getClass().getName(). 1882 compareTo(b.getClass().getName())) == 0) 1883 d = (System.identityHashCode(a) <= System.identityHashCode(b) ? 1884 -1 : 1); 1885 return d; 1886 } 1887 1888 /** 1889 * 將鏈表轉爲二叉樹 1890 * 1891 * @return root of tree 1892 */ 1893 final void treeify(Node<K, V>[] tab) { 1894 TreeNode<K, V> root = null; 1895 for (TreeNode<K, V> x = this, next; x != null; x = next) { 1896 next = (TreeNode<K, V>) x.next; 1897 x.left = x.right = null; 1898 if (root == null) { 1899 x.parent = null; 1900 x.red = false; 1901 root = x; 1902 } else { 1903 K k = x.key; 1904 int h = x.hash; 1905 Class<?> kc = null; 1906 for (TreeNode<K, V> p = root; ; ) { 1907 int dir, ph; 1908 K pk = p.key; 1909 if ((ph = p.hash) > h) 1910 dir = -1; 1911 else if (ph < h) 1912 dir = 1; 1913 else if ((kc == null && 1914 (kc = comparableClassFor(k)) == null) || 1915 (dir = compareComparables(kc, k, pk)) == 0) 1916 dir = tieBreakOrder(k, pk); 1917 1918 TreeNode<K, V> xp = p; 1919 if ((p = (dir <= 0) ? p.left : p.right) == null) { 1920 x.parent = xp; 1921 if (dir <= 0) 1922 xp.left = x; 1923 else 1924 xp.right = x; 1925 root = balanceInsertion(root, x); 1926 break; 1927 } 1928 } 1929 } 1930 } 1931 moveRootToFront(tab, root); 1932 } 1933 1934 /** 1935 * 將二叉樹轉爲鏈表 1936 */ 1937 final Node<K, V> untreeify(HashMap<K, V> map) { 1938 Node<K, V> hd = null, tl = null; 1939 for (Node<K, V> q = this; q != null; q = q.next) { 1940 Node<K, V> p = map.replacementNode(q, null); 1941 if (tl == null) 1942 hd = p; 1943 else 1944 tl.next = p; 1945 tl = p; 1946 } 1947 return hd; 1948 } 1949 1950 /** 1951 * 添加一個鍵值對 1952 */ 1953 final TreeNode<K, V> putTreeVal(HashMap<K, V> map, Node<K, V>[] tab, 1954 int h, K k, V v) { 1955 Class<?> kc = null; 1956 boolean searched = false; 1957 TreeNode<K, V> root = (parent != null) ? root() : this; 1958 for (TreeNode<K, V> p = root; ; ) { 1959 int dir, ph; 1960 K pk; 1961 if ((ph = p.hash) > h) 1962 dir = -1; 1963 else if (ph < h) 1964 dir = 1; 1965 else if ((pk = p.key) == k || (k != null && k.equals(pk))) 1966 return p; 1967 else if ((kc == null && 1968 (kc = comparableClassFor(k)) == null) || 1969 (dir = compareComparables(kc, k, pk)) == 0) { 1970 if (!searched) { 1971 TreeNode<K, V> q, ch; 1972 searched = true; 1973 if (((ch = p.left) != null && 1974 (q = ch.find(h, k, kc)) != null) || 1975 ((ch = p.right) != null && 1976 (q = ch.find(h, k, kc)) != null)) 1977 return q; 1978 } 1979 dir = tieBreakOrder(k, pk); 1980 } 1981 1982 TreeNode<K, V> xp = p; 1983 if ((p = (dir <= 0) ? p.left : p.right) == null) { 1984 Node<K, V> xpn = xp.next; 1985 TreeNode<K, V> x = map.newTreeNode(h, k, v, xpn); 1986 if (dir <= 0) 1987 xp.left = x; 1988 else 1989 xp.right = x; 1990 xp.next = x; 1991 x.parent = x.prev = xp; 1992 if (xpn != null) 1993 ((TreeNode<K, V>) xpn).prev = x; 1994 moveRootToFront(tab, balanceInsertion(root, x)); 1995 return null; 1996 } 1997 } 1998 } 1999 2000 /** 2001 * Removes the given node, that must be present before this call. 2002 * This is messier than typical red-black deletion code because we 2003 * cannot swap the contents of an interior node with a leaf 2004 * successor that is pinned by "next" pointers that are accessible 2005 * independently during traversal. So instead we swap the tree 2006 * linkages. If the current tree appears to have too few nodes, 2007 * the bin is converted back to a plain bin. (The test triggers 2008 * somewhere between 2 and 6 nodes, depending on tree structure). 2009 */ 2010 final void removeTreeNode(HashMap<K, V> map, Node<K, V>[] tab, 2011 boolean movable) { 2012 int n; 2013 if (tab == null || (n = tab.length) == 0) 2014 return; 2015 int index = (n - 1) & hash; 2016 TreeNode<K, V> first = (TreeNode<K, V>) tab[index], root = first, rl; 2017 TreeNode<K, V> succ = (TreeNode<K, V>) next, pred = prev; 2018 if (pred == null) 2019 tab[index] = first = succ; 2020 else 2021 pred.next = succ; 2022 if (succ != null) 2023 succ.prev = pred; 2024 if (first == null) 2025 return; 2026 if (root.parent != null) 2027 root = root.root(); 2028 if (root == null || root.right == null || 2029 (rl = root.left) == null || rl.left == null) { 2030 tab[index] = first.untreeify(map); // too small 2031 return; 2032 } 2033 TreeNode<K, V> p = this, pl = left, pr = right, replacement; 2034 if (pl != null && pr != null) { 2035 TreeNode<K, V> s = pr, sl; 2036 while ((sl = s.left) != null) // find successor 2037 s = sl; 2038 boolean c = s.red; 2039 s.red = p.red; 2040 p.red = c; // swap colors 2041 TreeNode<K, V> sr = s.right; 2042 TreeNode<K, V> pp = p.parent; 2043 if (s == pr) { // p was s's direct parent 2044 p.parent = s; 2045 s.right = p; 2046 } else { 2047 TreeNode<K, V> sp = s.parent; 2048 if ((p.parent = sp) != null) { 2049 if (s == sp.left) 2050 sp.left = p; 2051 else 2052 sp.right = p; 2053 } 2054 if ((s.right = pr) != null) 2055 pr.parent = s; 2056 } 2057 p.left = null; 2058 if ((p.right = sr) != null) 2059 sr.parent = p; 2060 if ((s.left = pl) != null) 2061 pl.parent = s; 2062 if ((s.parent = pp) == null) 2063 root = s; 2064 else if (p == pp.left) 2065 pp.left = s; 2066 else 2067 pp.right = s; 2068 if (sr != null) 2069 replacement = sr; 2070 else 2071 replacement = p; 2072 } else if (pl != null) 2073 replacement = pl; 2074 else if (pr != null) 2075 replacement = pr; 2076 else 2077 replacement = p; 2078 if (replacement != p) { 2079 TreeNode<K, V> pp = replacement.parent = p.parent; 2080 if (pp == null) 2081 root = replacement; 2082 else if (p == pp.left) 2083 pp.left = replacement; 2084 else 2085 pp.right = replacement; 2086 p.left = p.right = p.parent = null; 2087 } 2088 2089 TreeNode<K, V> r = p.red ? root : balanceDeletion(root, replacement); 2090 2091 if (replacement == p) { // detach 2092 TreeNode<K, V> pp = p.parent; 2093 p.parent = null; 2094 if (pp != null) { 2095 if (p == pp.left) 2096 pp.left = null; 2097 else if (p == pp.right) 2098 pp.right = null; 2099 } 2100 } 2101 if (movable) 2102 moveRootToFront(tab, r); 2103 } 2104 2105 /** 2106 * 將結點太多的桶分割 2107 * 2108 * @param map the map 2109 * @param tab the table for recording bin heads 2110 * @param index the index of the table being split 2111 * @param bit the bit of hash to split on 2112 */ 2113 final void split(HashMap<K, V> map, Node<K, V>[] tab, int index, int bit) { 2114 TreeNode<K, V> b = this; 2115 // Relink into lo and hi lists, preserving order 2116 TreeNode<K, V> loHead = null, loTail = null; 2117 TreeNode<K, V> hiHead = null, hiTail = null; 2118 int lc = 0, hc = 0; 2119 for (TreeNode<K, V> e = b, next; e != null; e = next) { 2120 next = (TreeNode<K, V>) e.next; 2121 e.next = null; 2122 if ((e.hash & bit) == 0) { 2123 if ((e.prev = loTail) == null) 2124 loHead = e; 2125 else 2126 loTail.next = e; 2127 loTail = e; 2128 ++lc; 2129 } else { 2130 if ((e.prev = hiTail) == null) 2131 hiHead = e; 2132 else 2133 hiTail.next = e; 2134 hiTail = e; 2135 ++hc; 2136 } 2137 } 2138 2139 if (loHead != null) { 2140 if (lc <= UNTREEIFY_THRESHOLD) 2141 tab[index] = loHead.untreeify(map); 2142 else { 2143 tab[index] = loHead; 2144 if (hiHead != null) // (else is already treeified) 2145 loHead.treeify(tab); 2146 } 2147 } 2148 if (hiHead != null) { 2149 if (hc <= UNTREEIFY_THRESHOLD) 2150 tab[index + bit] = hiHead.untreeify(map); 2151 else { 2152 tab[index + bit] = hiHead; 2153 if (loHead != null) 2154 hiHead.treeify(tab); 2155 } 2156 } 2157 } 2158 2159 /* ------------------------------------------------------------ */ 2160 // 紅黑樹方法,都是從CLR中修改的 2161 2162 /** 2163 * 左旋轉 2164 * 2165 * @param root 2166 * @param p 2167 * @param <K> 2168 * @param <V> 2169 * @return 2170 */ 2171 static <K, V> TreeNode<K, V> rotateLeft(TreeNode<K, V> root, 2172 TreeNode<K, V> p) { 2173 TreeNode<K, V> r, pp, rl; 2174 if (p != null && (r = p.right) != null) { 2175 if ((rl = p.right = r.left) != null) 2176 rl.parent = p; 2177 if ((pp = r.parent = p.parent) == null) 2178 (root = r).red = false; 2179 else if (pp.left == p) 2180 pp.left = r; 2181 else 2182 pp.right = r; 2183 r.left = p; 2184 p.parent = r; 2185 } 2186 return root; 2187 } 2188 2189 /** 2190 * 右旋轉 2191 * 2192 * @param root 2193 * @param p 2194 * @param <K> 2195 * @param <V> 2196 * @return 2197 */ 2198 static <K, V> TreeNode<K, V> rotateRight(TreeNode<K, V> root, 2199 TreeNode<K, V> p) { 2200 TreeNode<K, V> l, pp, lr; 2201 if (p != null && (l = p.left) != null) { 2202 if ((lr = p.left = l.right) != null) 2203 lr.parent = p; 2204 if ((pp = l.parent = p.parent) == null) 2205 (root = l).red = false; 2206 else if (pp.right == p) 2207 pp.right = l; 2208 else 2209 pp.left = l; 2210 l.right = p; 2211 p.parent = l; 2212 } 2213 return root; 2214 } 2215 2216 /** 2217 * 保證插入後平衡 2218 * 2219 * @param root 2220 * @param x 2221 * @param <K> 2222 * @param <V> 2223 * @return 2224 */ 2225 static <K, V> TreeNode<K, V> balanceInsertion(TreeNode<K, V> root, 2226 TreeNode<K, V> x) { 2227 x.red = true; 2228 for (TreeNode<K, V> xp, xpp, xppl, xppr; ; ) { 2229 if ((xp = x.parent) == null) { 2230 x.red = false; 2231 return x; 2232 } else if (!xp.red || (xpp = xp.parent) == null) 2233 return root; 2234 if (xp == (xppl = xpp.left)) { 2235 if ((xppr = xpp.right) != null && xppr.red) { 2236 xppr.red = false; 2237 xp.red = false; 2238 xpp.red = true; 2239 x = xpp; 2240 } else { 2241 if (x == xp.right) { 2242 root = rotateLeft(root, x = xp); 2243 xpp = (xp = x.parent) == null ? null : xp.parent; 2244 } 2245 if (xp != null) { 2246 xp.red = false; 2247 if (xpp != null) { 2248 xpp.red = true; 2249 root = rotateRight(root, xpp); 2250 } 2251 } 2252 } 2253 } else { 2254 if (xppl != null && xppl.red) { 2255 xppl.red = false; 2256 xp.red = false; 2257 xpp.red = true; 2258 x = xpp; 2259 } else { 2260 if (x == xp.left) { 2261 root = rotateRight(root, x = xp); 2262 xpp = (xp = x.parent) == null ? null : xp.parent; 2263 } 2264 if (xp != null) { 2265 xp.red = false; 2266 if (xpp != null) { 2267 xpp.red = true; 2268 root = rotateLeft(root, xpp); 2269 } 2270 } 2271 } 2272 } 2273 } 2274 } 2275 2276 /** 2277 * 刪除後調整平衡 2278 * 2279 * @param root 2280 * @param x 2281 * @param <K> 2282 * @param <V> 2283 * @return 2284 */ 2285 static <K, V> TreeNode<K, V> balanceDeletion(TreeNode<K, V> root, 2286 TreeNode<K, V> x) { 2287 for (TreeNode<K, V> xp, xpl, xpr; ; ) { 2288 if (x == null || x == root) 2289 return root; 2290 else if ((xp = x.parent) == null) { 2291 x.red = false; 2292 return x; 2293 } else if (x.red) { 2294 x.red = false; 2295 return root; 2296 } else if ((xpl = xp.left) == x) { 2297 if ((xpr = xp.right) != null && xpr.red) { 2298 xpr.red = false; 2299 xp.red = true; 2300 root = rotateLeft(root, xp); 2301 xpr = (xp = x.parent) == null ? null : xp.right; 2302 } 2303 if (xpr == null) 2304 x = xp; 2305 else { 2306 TreeNode<K, V> sl = xpr.left, sr = xpr.right; 2307 if ((sr == null || !sr.red) && 2308 (sl == null || !sl.red)) { 2309 xpr.red = true; 2310 x = xp; 2311 } else { 2312 if (sr == null || !sr.red) { 2313 if (sl != null) 2314 sl.red = false; 2315 xpr.red = true; 2316 root = rotateRight(root, xpr); 2317 xpr = (xp = x.parent) == null ? 2318 null : xp.right; 2319 } 2320 if (xpr != null) { 2321 xpr.red = (xp == null) ? false : xp.red; 2322 if ((sr = xpr.right) != null) 2323 sr.red = false; 2324 } 2325 if (xp != null) { 2326 xp.red = false; 2327 root = rotateLeft(root, xp); 2328 } 2329 x = root; 2330 } 2331 } 2332 } else { // symmetric 2333 if (xpl != null && xpl.red) { 2334 xpl.red = false; 2335 xp.red = true; 2336 root = rotateRight(root, xp); 2337 xpl = (xp = x.parent) == null ? null : xp.left; 2338 } 2339 if (xpl == null) 2340 x = xp; 2341 else { 2342 TreeNode<K, V> sl = xpl.left, sr = xpl.right; 2343 if ((sl == null || !sl.red) && 2344 (sr == null || !sr.red)) { 2345 xpl.red = true; 2346 x = xp; 2347 } else { 2348 if (sl == null || !sl.red) { 2349 if (sr != null) 2350 sr.red = false; 2351 xpl.red = true; 2352 root = rotateLeft(root, xpl); 2353 xpl = (xp = x.parent) == null ? 2354 null : xp.left; 2355 } 2356 if (xpl != null) { 2357 xpl.red = (xp == null) ? false : xp.red; 2358 if ((sl = xpl.left) != null) 2359 sl.red = false; 2360 } 2361 if (xp != null) { 2362 xp.red = false; 2363 root = rotateRight(root, xp); 2364 } 2365 x = root; 2366 } 2367 } 2368 } 2369 } 2370 } 2371 2372 /** 2373 * 檢測是否符合紅黑樹 2374 */ 2375 static <K, V> boolean checkInvariants(TreeNode<K, V> t) { 2376 TreeNode<K, V> tp = t.parent, tl = t.left, tr = t.right, 2377 tb = t.prev, tn = (TreeNode<K, V>) t.next; 2378 if (tb != null && tb.next != t) 2379 return false; 2380 if (tn != null && tn.prev != t) 2381 return false; 2382 if (tp != null && t != tp.left && t != tp.right) 2383 return false; 2384 if (tl != null && (tl.parent != t || tl.hash > t.hash)) 2385 return false; 2386 if (tr != null && (tr.parent != t || tr.hash < t.hash)) 2387 return false; 2388 if (t.red && tl != null && tl.red && tr != null && tr.red) 2389 return false; 2390 if (tl != null && !checkInvariants(tl)) 2391 return false; 2392 if (tr != null && !checkInvariants(tr)) 2393 return false; 2394 return true; 2395 } 2396 } 2397 2398 }