{"id":9827,"date":"2023-11-28T21:14:44","date_gmt":"2023-11-28T13:14:44","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=9827"},"modified":"2023-11-28T21:15:21","modified_gmt":"2023-11-28T13:15:21","slug":"java-zhonghashmap-deput-fang-fa-de-ju-ti-liu-cheng","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/java-zhonghashmap-deput-fang-fa-de-ju-ti-liu-cheng\/","title":{"rendered":"Java\u4e2dHashMap\u7684put\u65b9\u6cd5\u7684\u5177\u4f53\u6d41\u7a0b?"},"content":{"rendered":"<p>\u5f53\u6211\u4eec put \u7684\u65f6\u5019\uff0c\u9996\u5148\u8ba1\u7b97 key \u7684 hash \u503c\uff0c\u8fd9\u91cc\u8c03\u7528\u4e86 hash \u65b9\u6cd5\uff0chash\u65b9\u6cd5\u5b9e\u9645\u662f\u8ba9 key.hashCode()\u4e0e key.hashCode ()&gt;&gt;&gt;16 \u8fdb\u884c\u5f02\u6216\u64cd\u4f5c,\u9ad816bit \u8865 0\uff0c\u4e00\u4e2a\u6570\u548c 0 \u5f02\u6216\u4e0d\u53d8\uff0c\u6240\u4ee5 hash \u51fd\u6570\u5927\u6982\u7684\u4f5c\u7528\u5c31\u662f\uff1a\u9ad816bit\u4e0d\u53d8\uff0c\u4f4e16bit\u548c\u9ad816bit\u505a\u4e86\u4e00\u4e2a\u5f02\u6216\uff0c\u76ee\u7684\u662f\u51cf\u5c11\u78b0\u649e\u3002\u6309\u7167\u51fd\u6570\u6ce8\u91ca\uff0c\u56e0\u4e3a bucket \u6570\u7ec4\u5927\u5c0f\u662f 2 \u7684\u5e42,\u8ba1\u7b97\u4e0b\u6807 index=\uff08table.length-1)&amp; hash\uff0c\u5982\u679c\u4e0d\u505a hash \u5904\u7406\uff0c\u76f8\u5f53\u4e8e\u6563\u5217\u751f\u6548\u7684\u53ea\u6709\u51e0\u4e2a\u4f4e bit \u4f4d\uff0c\u4e3a\u4e86\u51cf\u5c11\u6563\u5217\u7684\u78b0\u649e\uff0c\u8bbe\u8ba1\u8005\u7efc\u5408\u8003\u8651\u4e86\u901f\u5ea6\u3001\u4f5c\u7528\u3001\u8d28\u91cf\u4e4b\u540e\uff0c\u4f7f\u7528\u9ad816bit\u548c\u4f4e16bit\u5f02\u6216\u4f86\u7b80\u5355\u5904\u7406\u51cf\u5c11\u78b0\u649e\uff0c\u800c\u4e14JDK8 \u4e2d\u7528\u4e86\u590d\u6742\u5ea6 O(logn)\u7684\u6811\u7ed3\u6784\u6765\u63d0\u5347\u78b0\u649e\u4e0b\u7684\u6027\u80fd\u3002<br \/>\nputVal\u65b9\u6cd5\u6267\u884c\u6d41\u7a0b\u56fe<br \/>\n<img decoding=\"async\" src=\"https:\/\/s2.loli.net\/2023\/11\/28\/9Ii7plndKSRYWte.jpg\" alt=\"\" \/><\/p>\n<p>Public V put(K key, V value){<br \/>\nreturn putVal (hash(key), key, value, false, true);<br \/>\n}<br \/>\nstatic final int hash(Object key) {<br \/>\nint h;<br \/>\nreturn (key == null) ? 0 : (h = key.hashCode())^(h &gt;&gt;&gt; 16);<br \/>\n}<br \/>\n\/\/\u5b9e\u73b0 Map.put \u548c\u76f8\u5173\u65b9\u6cd5<br \/>\nfinal V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {<br \/>\nNode &lt; K, V &gt; [] tab;<br \/>\nNode &lt; K, V &gt; p;<br \/>\nint n, i;<br \/>\n\/\/\u6b65\u9aa4\u2460\uff1atab \u4e3a\u7a7a\u5219\u521b\u5efa<br \/>\n\/\/table \u672a\u521d\u59cb\u5316\u6216\u8005\u957f\u5ea6\u4e3a 0\uff0c\u8fdb\u884c\u6269\u5bb9<br \/>\nif((tab = table\uff09== null ||(n = tab.length)== 0) n =(tab = resize()).length;<br \/>\n\/\/\u6b65\u9aa4\u2461\uff1a\u8ba1\u7b97 index\uff0c\u5e76\u5bf9 null \u505a\u5904\u7406<br \/>\n\/\/\uff08n &#8211; 1\uff09&amp; hash \u786e\u5b9a\u5143\u7d20\u5b58\u653e\u5728\u54ea\u4e2a\u6876\u4e2d\uff0c\u6876\u4e3a\u7a7a\uff0c\u65b0\u751f\u6210\u7ed3\u70b9\u653e\u5165\u6876\u4e2d\uff08\u6b64\u65f6\uff0c\u8fd9\u4e2a\u7ed3\u70b9\u662f\u653e\u5728\u6570\u7ec4\u4e2d)<br \/>\nif((p = tab[i =\uff08n &#8211; 1\uff09&amp; hash]\uff09== null\uff09tab[i]= newNode(hash, key, value,null)\uff1b<br \/>\n\/\/\u6876\u4e2d\u5df2\u7ecf\u5b58\u5728\u5143\u7d20<br \/>\nelse {<br \/>\nNode &lt; K, V&gt; e;<br \/>\nK k;<br \/>\n\/\/\u6b65\u9aa4\u2462\uff1a\u8282\u70b9 key \u5b58\u5728\uff0c\u76f4\u63a5\u8986\u76d6 value<br \/>\n\/\/\u6bd4\u8f83\u6876\u4e2d\u7b2c\u4e00\u4e2a\u5143\u7d20(\u6570\u7ec4\u4e2d\u7684\u7ed3\u70b9)\u7684 hash \u503c\u76f8\u7b49\uff0ckey \u76f8\u7b49<br \/>\nif (p.hash == hash &amp;&amp; ((k = p.key)== key || (key != null &amp;&amp; key. equals(k))))<br \/>\n\/\/\u5c06\u7b2c\u4e00\u4e2a\u5143\u7d20\u8d4b\u503c\u7ed9 e\uff0c\u7528 e \u6765\u8bb0\u5f55<br \/>\ne = p;<br \/>\n\/\/\u6b65\u9aa4\u2463\uff0c\u5224\u65ad\u8be5\u94fe\u4e3a\u7ea2\u9ed1\u6811<br \/>\n\/\/ hash \u503c\u4e0d\u76f8\u7b49\uff0c\u5373key \u4e0d\u76f8\u7b49\uff1b\u4e3a\u7ea2\u9ed1\u6811\u7ed3\u70b9<br \/>\n\/\/ \u5982\u679c\u5f53\u524d\u5143\u7d20\u7c7b\u578b\u4e3a TreeNode\uff0c\u8868\u793a\u4e3a\u7ea2\u9ed1\u6811\uff0cputTreeVal \u8fd4\u56de\u5f85\u5b58\u653e\u7684 node\uff0c e\u53ef\u80fd\u4e3anull<br \/>\nelse if (p instanceof TreeNode)<br \/>\n\/\/\u653e\u5165\u6811\u4e2d<br \/>\ne=((TreeNode&lt;K, V&gt;)p).putTreeVal(this, tab, hash, key, value);<br \/>\n\/\/\u6b65\u9aa4\u2464\uff1a\u8be5\u94fe\u4e3a\u94fe\u8868<br \/>\n\/\/\u4e3a\u94fe\u8868\u7ed3\u70b9<br \/>\nelse {<br \/>\n\/\/\u5728\u94fe\u8868\u6700\u672b\u63d2\u5165\u7ed3\u70b9<br \/>\nfor(int binCount = 0;;++binCount) {<br \/>\n\/\/\u5230\u8fbe\u94fe\u8868\u7684\u5c3e\u90e8<br \/>\n\/\/\u5224\u65ad\u8be5\u94fe\u8868\u5c3e\u90e8\u6307\u9488\u662f\u4e0d\u662f\u7a7a\u7684<br \/>\nif ((e = p.next)== null){<br \/>\n\/\/\u5728\u5c3e\u90e8\u63d2\u5165\u65b0\u7ed3\u70b9<br \/>\np.next = newNode (hash, key, value, null);<br \/>\n\/\/\u5224\u65ad\u94fe\u8868\u7684\u957f\u5ea6\u662f\u5426\u8fbe\u5230\u8f6c\u5316\u7ea2\u9ed1\u6811\u7684\u4e34\u754c\u503c\uff0c\u4e34\u754c\u503c\u4e3a 8<br \/>\nif (binCount &gt;= TREEIFY_THRESHOLD &#8211; 1\uff09\/\/-1 for 1st<br \/>\n\/\/\u94fe\u8868\u7ed3\u6784\u8f6c\u6811\u5f62\u7ed3\u6784<br \/>\ntreeifyBin(tab, hash);<br \/>\n\/\/\u8df3\u51fa\u5faa\u73af<br \/>\nbreak;<br \/>\n}<br \/>\n\/\/\u5224\u65ad\u94fe\u8868\u4e2d\u7ed3\u70b9\u7684 key \u503c\u4e0e\u63d2\u5165\u7684\u5143\u7d20\u7684 key \u503c\u662f\u5426\u76f8\u7b49<br \/>\nif (e.hash == hash &amp;&amp; ((k = a.key)== key || (key != null &amp;&amp; key.equals(k))))<br \/>\n\/\/\u76f8\u7b49\uff0c\u8df3\u51fa\u5faa\u73af<br \/>\nbreak\uff1b<br \/>\n\/\/\u7528\u4e8e\u904d\u5386\u6876\u4e2d\u7684\u94fe\u8868\uff0c\u4e0e\u524d\u9762\u7684 e = p.next \u7ec4\u5408\uff0c\u53ef\u4ee5\u904d\u5386\u94fe\u8868<br \/>\np= e\uff1b<br \/>\n}<br \/>\n}<br \/>\n\/\/\u5224\u65ad\u5f53\u524d\u7684 key \u5df2\u7ecf\u5b58\u5728\u7684\u60c5\u51b5\u4e0b\uff0c<br \/>\n\/\/\u518d\u6765\u4e00\u4e2a\u76f8\u540c\u7684 hash \u503c\u3001key \u503c\u65f6\uff0c\u8fd4\u56de\u65b0\u6765\u7684 value \u8fd9\u4e2a\u503c<br \/>\nif (e!= null){<br \/>\n\/\/\u8bb0\u5f55e\u7684 value<br \/>\nV oldValue = e.value;<br \/>\n\/\/ onlyIfAbsent \u4e3a false \u6216\u8005\u65e7\u503c\u4e3a null<br \/>\nif (!onlyIfAbsent || oldValue == null\uff09<br \/>\n\/\/\u7528\u65b0\u503c\u66ff\u6362\u65e7\u503c<br \/>\ne.value = value;<br \/>\n\/\/\u8bbf\u95ee\u540e\u56de\u8c03<br \/>\nafterNodeAccess(e);<br \/>\n\/\/\u8fd4\u56de\u65e7\u503c<br \/>\nreturn oldValue;<br \/>\n}<br \/>\n}<br \/>\n\/\/\u7ed3\u6784\u6027\u4fee\u6539<br \/>\n++modCount\uff1b<br \/>\n\/\/\u6b65\u9aa4\u2465\uff1a\u8d85\u8fc7\u6700\u5927\u5bb9\u91cf\u5c31\u6269\u5bb9<br \/>\n\/\/\u5b9e\u9645\u5927\u5c0f\u5927\u4e8e\u9608\u503c\u5219\u6269\u5bb9<br \/>\nif(++size&gt;threshold) resize();<br \/>\n\/\/\u63d2\u5165\u540e\u56de\u8c03<br \/>\nafterNodeInsertion (evict);<br \/>\nreturn null;<br \/>\n}<br \/>\n1\u3001\u5224\u65ad\u952e\u503c\u5bf9\u6570\u7ec4 table[i]\u662f\u5426\u4e3a\u7a7a\u6216\u4e3a null\uff0c\u5426\u5219\u6267\u884c resize()\u8fdb\u884c\u6269\u5bb9\uff1b<br \/>\n2\u3001\u6839\u636e\u952e\u503c key \u8ba1\u7b97 hash \u503c\u5f97\u5230\u63d2\u5165\u7684\u6570\u7ec4\u7d22\u5f15 i\uff0c\u5982\u679ctable[i]==null,\u76f4\u63a5\u65b0\u5efa\u8282\u70b9\u6dfb\u52a0\uff0c\u8f6c\u5411\u2465\uff0c\u5982\u679c table[i]\u4e0d\u4e3a\u7a7a\uff0c\u8f6c\u5411\u2462\uff1b<br \/>\n3\u3001\u5224\u65ad table[i]\u7684\u9996\u4e2a\u5143\u7d20\u662f\u5426\u548c key \u4e00\u6837\uff0c\u5982\u679c\u76f8\u540c\u76f4\u63a5\u8986\u76d6value\uff0c\u5426\u5219\u8f6c\u5411\u2463\uff0c\u8fd9\u91cc\u7684\u76f8\u540c\u6307\u7684\u662f hashCode \u4ee5\u53ca equals\uff1b<br \/>\n4\u3001\u5224\u65ad table[i]\u662f\u5426\u4e3a treeNode\uff0c\u5373 table[i]\u662f\u5426\u662f\u7ea2\u9ed1\u6811\uff0c\u5982\u679c\u662f\u7ea2\u9ed1\u6811\uff0c\u5219\u76f4\u63a5\u5728\u6811\u4e2d\u63d2\u5165\u952e\u503c\u5bf9\uff0c\u5426\u5219\u8f6c\u5411 \u2464\uff1b<br \/>\n5\u3001\u904d\u5386 table[i]\uff0c\u5224\u65ad\u94fe\u8868\u957f\u5ea6\u662f\u5426\u5927\u4e8e 8\uff0c\u5927\u4e8e 8 \u7684\u8bdd\u628a\u94fe\u8868\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\uff0c\u5728\u7ea2\u9ed1\u6811\u4e2d\u6267\u884c\u63d2\u5165\u64cd\u4f5c\uff0c\u5426\u5219\u8fdb\u884c\u94fe\u8868\u7684\u63d2\u5165\u64cd\u4f5c\uff1b\u904d\u5386\u8fc7\u7a0b\u4e2d\u82e5\u53d1\u73b0key \u5df2\u7ecf\u5b58\u5728\u76f4\u63a5\u8986\u76d6 value \u5373\u53ef\uff1b<br \/>\n6\u3001\u63d2\u5165\u6210\u529f\u540e\uff0c\u5224\u65ad\u5b9e\u9645\u5b58\u5728\u7684\u952e\u503c\u5bf9\u6570\u91cf size \u662f\u5426\u8d85\u8fc7\u4e86\u6700\u5927\u5bb9\u91cfthreshold\uff0c\u5982\u679c\u8d85\u8fc7\uff0c\u8fdb\u884c\u6269\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u6211\u4eec put \u7684\u65f6\u5019\uff0c\u9996\u5148\u8ba1\u7b97 key \u7684 hash \u503c\uff0c\u8fd9\u91cc\u8c03\u7528\u4e86 hash \u65b9\u6cd5\uff0chash\u65b9\u6cd5\u5b9e\u9645\u662f\u8ba9  [&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-9827","post","type-post","status-publish","format-standard","hentry","category-javabase"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9827","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=9827"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9827\/revisions"}],"predecessor-version":[{"id":9828,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9827\/revisions\/9828"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=9827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=9827"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=9827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}