{"id":10280,"date":"2023-11-28T21:31:27","date_gmt":"2023-11-28T13:31:27","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=10280"},"modified":"2023-11-28T21:31:33","modified_gmt":"2023-11-28T13:31:33","slug":"java-zhonghashmap-de-kuo-rong-cao-zuo-shi-zen-me-s","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/java-zhonghashmap-de-kuo-rong-cao-zuo-shi-zen-me-s\/","title":{"rendered":"Java\u4e2dHashMap\u7684\u6269\u5bb9\u64cd\u4f5c\u662f\u600e\u4e48\u5b9e\u73b0\u7684?"},"content":{"rendered":"<p>1\u3001\u5728 jdk1.8 \u4e2d\uff0cresize()\u65b9\u6cd5\u662f\u5728 hashmap \u4e2d\u7684\u952e\u503c\u5bf9\u5927\u4e8e\u9600\u503c\u65f6\u6216\u8005\u521d\u59cb\u5316\u65f6\uff0c\u5c31\u8c03\u7528 resize()\u65b9\u6cd5\u8fdb\u884c\u6269\u5bb9\uff1b<br \/>\n2\u3001\u6bcf\u6b21\u6269\u5c55\u7684\u65f6\u5019\uff0c\u90fd\u662f\u6269\u5c55 2 \u500d\uff1b<br \/>\n3\u3001\u6269\u5c55\u540e Node \u5bf9\u8c61\u7684\u4f4d\u7f6e\u8981\u4e48\u5728\u539f\u4f4d\u7f6e\uff0c\u8981\u4e48\u79fb\u52a8\u5230\u539f\u504f\u79fb\u91cf\u4e24\u500d\u7684\u4f4d\u7f6e\u3002\u5728 putVal()\u4e2d,\u6211\u4eec\u770b\u5230\u5728\u8fd9\u4e2a\u51fd\u6570\u91cc\u9762\u4f7f\u7528\u5230\u4e86 2 \u6b21 resize()\u65b9\u6cd5,resize()\u65b9\u6cd5\u8868\u793a\u7684\u5728\u8fdb\u884c\u7b2c\u4e00\u6b21\u521d\u59cb\u5316\u65f6\u4f1a\u5bf9\u5176\u8fdb\u884c\u6269\u5bb9\uff0c\u6216\u8005\u5f53\u8be5\u6570\u7ec4\u7684\u5b9e\u9645\u5927\u5c0f\u5927\u4e8e\u5176\u4e34\u754c\u503c(\u7b2c\u4e00\u6b21\u4e3a 12)\uff0c\u8fd9\u4e2a\u65f6\u5019\u5728\u6269\u5bb9\u7684\u540c\u65f6\u4e5f\u4f1a\u4f34\u968f\u7684\u6876\u4e0a\u9762\u7684\u5143\u7d20\u8fdb\u884c\u91cd\u65b0\u5206\u53d1\uff0c\u8fd9\u4e5f\u662f JDK1.8 \u7248\u672c\u7684\u4e00\u4e2a\u4f18\u5316\u7684\u5730\u65b9\uff0c\u5728 1.7 \u4e2d\uff0c\u6269\u5bb9\u4e4b\u540e\u9700\u8981\u91cd\u65b0\u53bb\u8ba1\u7b97\u5176 Hash \u503c\uff0c\u6839\u636e Hash \u503c\u5bf9\u5176\u8fdb\u884c\u5206\u53d1\uff0c\u4f46\u5728 1.8 \u7248\u672c\u4e2d\uff0c\u5219\u662f\u6839\u636e\u5728\u540c\u4e00\u4e2a\u6876\u7684\u4f4d\u7f6e\u4e2d\u8fdb\u884c\u5224\u65ad(e.hash &amp; oldCap)\u662f\u5426\u4e3a 0\uff0c\u91cd\u65b0\u8fdb\u884chash \u5206\u914d\u540e\uff0c\u8be5\u5143\u7d20\u7684\u4f4d\u7f6e\u8981\u4e48\u505c\u7559\u5728\u539f\u59cb\u4f4d\u7f6e\uff0c\u8981\u4e48\u79fb\u52a8\u5230\u539f\u59cb\u4f4d\u7f6e+\u589e\u52a0\u7684\u6570\u7ec4\u5927\u5c0f\u8fd9\u4e2a\u4f4d\u7f6e\u4e0a<br \/>\nfinal Node &lt;K, V&gt; [] resize(){<br \/>\nNode &lt;K\uff0c V&gt;[] oldTab = table\uff1b\/\/oldTab \u6307\u5411 hash \u6876\u6570\u7ec4<br \/>\nint oldCap =(oldTab == null)? 0 \uff1aoldTab.length;<br \/>\nint oldThr = threshold;<br \/>\nint newCap,newThr = 0;<br \/>\nif(oldCap&gt;0){ \/\/\u5982\u679c oldCap \u4e0d\u4e3a\u7a7a\u7684\u8bdd\uff0c\u5c31\u662f hash \u6876\u6570\u7ec4\u4e0d\u4e3a\u7a7a<br \/>\nif (oldCap &gt;= MAXIMM_CAPACITY)(\/\/\u5982\u679c\u5927\u4e8e\u6700\u5927\u5bb9\u91cf\u4e86\uff0c\u5c31\u8d4b\u503c\u4e3a\u6574\u6570\u6700\u5927\u7684<br \/>\n\u9600\u503c<br \/>\nthreshold = Integer.MAX_VALUE;<br \/>\nreturn oldTab\uff1b\/\/\u8fd4\u56de<br \/>\n}\/\/\u5982\u679c\u5f53\u524d hash \u6876\u6570\u7ec4\u7684\u957f\u5ea6\u5728\u6269\u5bb9\u540e\u4ecd\u7136\u5c0f\u4e8e\u6700\u5927\u5bb9\u91cf\u5e76\u4e14oldCap \u5927\u4e8e\u9ed8\u8ba4\u503c16<br \/>\nelse if((newCap = oldCap&lt;&lt;1)&lt;MAXIMML_CAPACITY &amp;&amp; oldCap &gt;=<br \/>\nDEFAULT_INITIAL_CAPACITY) newThr = oldThr &lt;&lt; 1;\/\/ double threshold \u53cc\u500d\u6269\u5bb9\u9600\u503c<br \/>\nthreshold<br \/>\n}<br \/>\n\/\/ \u65e7\u7684\u5bb9\u91cf\u4e3a 0\uff0c\u4f46 threshold \u5927\u4e8e\u96f6\uff0c\u4ee3\u8868\u6709\u53c2\u6784\u9020\u6709 cap \u4f20\u5165\uff0cthreshold \u5df2\u7ecf\u88ab\u521d\u59cb\u5316\u6210\u6700\u5c0f 2 \u7684n \u6b21\u5e42<br \/>\n\/\/\u76f4\u63a5\u5c06\u8be5\u503c\u8d4b\u7ed9\u65b0\u7684\u5bb9\u91cf<br \/>\nelse if (oldThr&gt;0)\/\/ initial capacity was placed in threshold<br \/>\nnewCap = oldThr;<br \/>\n\/\/\u65e0\u53c2\u6784\u9020\u521b\u5efa\u7684 map\uff0c\u7ed9\u51fa\u9ed8\u8ba4\u5bb9\u91cf\u548c threshold 16\uff0c 16*0.75<br \/>\nelse { \/\/ zero initial threshold signifies using defaults<br \/>\nnewCap = DEFAULT_INITIAL_CAPACITY;<br \/>\nnewThr =(int)(DEFAULT_LOAD_FACTOR * DBFAULT_INTTIAL_CAPACITY)\uff1b<br \/>\n}<br \/>\n\/\/\u65b0\u7684 threshold =\u65b0\u7684 cap * 0.75<br \/>\nif (newThr == 0){<br \/>\nfloat ft=(float) newCap * loadFactor;<br \/>\nnewThr = (newCap&lt;MAXIMUM_CAPACITY &amp;&amp; ft &lt;(float) MAxIMUM_CAPACITY?(int) ft: Integer.MAX_VALUE);<br \/>\n}<br \/>\nthreshold = newThr;<br \/>\n\/\/\u8ba1\u7b97\u51fa\u65b0\u7684\u6570\u7ec4\u957f\u5ea6\u540e\u8d4b\u7ed9\u5f53\u524d\u6210\u5458\u53d8\u91cftable<br \/>\n@SuppressWarnings({&quot;rawtypes&quot;,&quot;unchecked&quot;})<br \/>\nNode &lt; K, V&gt;[] newTab =(Node &lt; K, V&gt;[]) new Node [newCap]\uff1b\/\/\u65b0\u5efa hash \u6876<br \/>\n\u6559\u7ec4<br \/>\ntable = newTab\uff1b\/\/\u5c06\u65b0\u6570\u7ec4\u7684\u503c\u590d\u5236\u7ed9\u65e7\u7684 hash \u6876\u6570\u7ec4<br \/>\n\/\/ \u5982\u679c\u539f\u5148\u7684\u6570\u7ec4\u6ca1\u6709\u521d\u59cb\u5316\uff0c\u90a3\u4e48 resize \u7684\u521d\u59cb\u5316\u5de5\u4f5c\u5230\u6b64\u7ed3\u675f\uff0c\u5426\u5219\u8fdb\u5165\u6269\u5bb9\u5143\u7d20\u91cd\u6392\u903b\u8f91\uff0c\u4f7f\u5176\u5747\u5300\u7684\u5206\u6563<br \/>\nif (oldTab != null){<br \/>\n\/\/\u904d\u5386\u65b0\u6570\u7ec4\u7684\u6240\u6709\u6876\u4e0b\u6807<br \/>\nfor (int j = 0;j &lt; oldCap:++j){<br \/>\nNode&lt;K,V&gt; e\uff1a<br \/>\nif((e = oldTab[j]\uff09!= null){<br \/>\n\/\/\u65e7\u6570\u7ec4\u7684\u6876\u4e0b\u6807\u8d4b\u7ed9\u4e34\u65f6\u53d8\u91cf e\uff0c\u5e76\u4e14\u89e3\u9664\u65e7\u6570\u7ec4\u4e2d\u7684\u5f15\u7528\uff0c\u5426\u5219\u5c31\u6570\u7ec4\u65e0\u6cd5\u88ab GC \u56de\u6536<br \/>\noldTab[j]= null;<br \/>\n\/\/\u5982\u679c e.next==null\uff0c\u4ee3\u8868\u6876\u4e2d\u5c31\u4e00\u4e2a\u5143\u7d20\uff0c\u4e0d\u5b58\u5728\u94fe\u8868\u6216\u8005\u7ea2\u9ed1\u6811<br \/>\nif (e.next == null)<br \/>\n\/\/\u7528\u540c\u6837\u7684 hash \u6620\u5c04\u7b97\u6cd5\u628a\u8be5\u5143\u7d20\u52a0\u5165\u65b0\u7684\u6570\u7ec4<br \/>\nnewTab[e.hash &amp; (newCap &#8211; 1)] =e;<br \/>\n\/\/\u5982\u679c e\u662f TreeNode \u5e76\u4e14 e.next!=null\uff0c\u90a3\u4e48\u5904\u7406\u6811\u4e2d\u5143\u7d20\u7684\u91cd\u6392<br \/>\nelse if (e instanceof TreeNode)<br \/>\n((TreeNode &lt; K, V&gt;)e).split(this, newTab, j, oldCap);<br \/>\n\/\/e\u662f\u94fe\u8868\u7684\u5934\u5e76\u4e14e.next!=null\uff0c\u90a3\u4e48\u5904\u7406\u94fe\u8868\u4e2d\u5143\u7d20\u91cd\u6392<br \/>\nelse {\/\/ preserve order<br \/>\n\/\/loHlead,1oTail \u4ee3\u8868\u6269\u5bb9\u540e\u4e0d\u7528\u53d8\u6362\u4e0b\u6807\uff0c\u89c1\u6ce8 1<br \/>\nNode&lt; K\uff0c V&gt; loHead = null, loTail = null;<br \/>\n\/\/ hiHead\uff0c hiTail \u4ee3\u8868\u6269\u5bb9\u540e\u53d8\u6362\u4e0b\u6807\uff0c\u89c1\u6ce8 1<br \/>\nNode&lt; K, V&gt; hiHead = null, hiTail = null;<br \/>\nNode&lt; K, V&gt; next;<br \/>\n\/\/\u904d\u5386\u94fe\u8868<br \/>\ndo {<br \/>\nnext = e.next;<br \/>\nif((e.hash &amp; oldCap)== 0){<br \/>\nif (loTail == null)<br \/>\n\/\/\u521d\u59cb\u5316 head \u6307\u5411\u94fe\u8868\u5f53\u524d\u5143\u7d20 e\uff0c e \u4e0d\u4e00\u5b9a\u662f\u94fe\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u521d\u59cb\u5316\u540e 1oHead<br \/>\n\/\/\u4ee3\u8868\u4e0b\u6807\u4fdd\u6301\u4e0d\u53d8\u7684\u94fe\u8868\u7684\u5934\u5143\u7d20<br \/>\nloHead = e;<br \/>\nelse<br \/>\n\/\/ 1oTail. next \u6307\u5411\u5f53\u524d e<br \/>\nloTail.next =e;<br \/>\n\/\/ 1oTail \u6307\u5411\u5f53\u524d\u7684\u5143\u7d20 e<br \/>\n\/\/\u521d\u59cb\u5316\u540e\uff0cloTail \u548c loHead \u6307\u5411\u76f8\u540c\u7684\u5185\u5b58\uff0c\u6240\u4ee5\u5f53<br \/>\nloTail.next \u6307\u5411\u4e0b\u4e00\u4e2a\u5143\u7d20\u65f6,<br \/>\n\/\/\u5e95\u5c42\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u7684 next \u5f15\u7528\u4e5f\u76f8\u5e94\u53d1\u751f\u53d8\u5316\uff0c\u9020\u6210<br \/>\nlowHead.next.next&#8230;..<br \/>\n\/\/\u8ddf\u968f loTail \u540c\u6b65\uff0c\u4f7f\u5f97 1owHead \u53ef\u4ee5\u94fe\u63a5\u5230\u6240\u6709\u5c5e\u4e8e\u8be5\u94fe\u8868\u7684\u5143\u7d20\u3002<br \/>\nloTail = e;<br \/>\n} else {<br \/>\nif (hiTail == null)<br \/>\n\/\/\u521d\u59cb\u5316 head \u6307\u5411\u94fe\u8868\u5f53\u524d\u5143\u7d20 e\uff0c\u521d\u59cb\u5316\u540e hiHead \u4ee3\u8868\u4e0b\u6807\u66f4\u6539\u7684\u94fe\u8868\u5934\u5143\u7d20<br \/>\nhiHead = e;<br \/>\nelse hiTail.next = e;<br \/>\nhiTail = e;<br \/>\n}<br \/>\n} while ((e = next)!= null);<br \/>\n\/\/\u904d\u5386\u7ed3\u675f\uff0c\u5c06 tail \u6307\u5411 null,\u5e76\u628a\u94fe\u8868\u5934\u653e\u5165\u65b0\u6570\u7ec4\u7684\u76f8\u5e94\u4e0b\u6807,\u5f62\u6210\u65b0\u7684\u6620\u5c04\u3002<br \/>\nif (loTail != null){<br \/>\nloTail.next = null;<br \/>\nnewTab[j]= loHead;<br \/>\n}<br \/>\nif (hiTail != null){<br \/>\nhiTail.next = null;<br \/>\nnewTab[j + oldCap]= hiHead;<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\nreturn newTab;<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u5728 jdk1.8 \u4e2d\uff0cresize()\u65b9\u6cd5\u662f\u5728 hashmap \u4e2d\u7684\u952e\u503c\u5bf9\u5927\u4e8e\u9600\u503c\u65f6\u6216\u8005\u521d\u59cb\u5316\u65f6\uff0c\u5c31\u8c03\u7528  [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[99],"tags":[],"class_list":["post-10280","post","type-post","status-publish","format-standard","hentry","category-javabase"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=10280"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10280\/revisions"}],"predecessor-version":[{"id":10281,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10280\/revisions\/10281"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=10280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=10280"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=10280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}