{"id":233,"date":"2021-09-22T22:41:38","date_gmt":"2021-09-22T14:41:38","guid":{"rendered":"http:\/\/qqiuklele.cn\/?p=233"},"modified":"2025-03-26T15:20:13","modified_gmt":"2025-03-26T07:20:13","slug":"look-hashmap-carefully%ef%bc%81","status":"publish","type":"post","link":"https:\/\/qqiukulele.cn\/?p=233","title":{"rendered":"Look HashMap Carefully\uff01"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">HashMap\u7684\u57fa\u7840\u56de\u987e\uff1a<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>HashMap\u5b9e\u73b0\u4e8eMap\u63a5\u53e3<\/li>\n\n\n\n<li>HashMap\u91c7\u7528key-value\u65b9\u5f0f\u8fdb\u884c\u6570\u636e\u5b58\u50a8<\/li>\n\n\n\n<li>HashMap\u5b58\u50a8\u7684\u6570\u636e\u6ca1\u6709\u987a\u5e8f\u6027<\/li>\n\n\n\n<li>HashMap\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684<\/li>\n\n\n\n<li>JDK1.7\u7248\u672c\u4e2d\uff0cHashMap\u5e95\u5c42\u662f\u201c\u6570\u7ec4+\u94fe\u8868\u201d\uff0c\u901a\u8fc7\u5efa\u7acbpos=key%size\u8fd9\u6837\u7684\u5173\u7cfb\u53bb\u4e3ahashMap\u63d2\u5165\u5b58\u50a8\uff1bpos\u6c42\u4f59\u5c31\u662f\u6570\u7ec4\u4e0b\u6807\u30021.8\u591a\u52a0\u4e86\u7ea2\u9ed1\u6811<\/li>\n\n\n\n<li>HashMap\u91cc\u8fb9\u7684\u6570\u7ec4\u521d\u503c\u662f16\u4e2a\u7a7a\u95f4\u3002\u6570\u7ec4\u91cc\u5b58\u653e\u7684\u5c31\u662f\u94fe\u8868\u7684\u5f15\u7528\u5730\u5740\u3002<\/li>\n\n\n\n<li>\u6570\u7ec4\u4e2d\u67d0\u4e2a\u4f4d\u7f6e\u4e0a\u5173\u8054\u7684\u94fe\u8868\u5b58\u50a8\u7684\u6570\u636e\u4e2a\u6570\u5927\u4e8e\u7b49\u4e8e8\u65f6\uff0cHashMap\u5c31\u628a\u94fe\u8868\u6539\u53d8\u4e3a\u7ea2\u9ed1\u6811\uff0c\u8981\u662f\u6876\u4e2d\u7684\u503c\u6570\u53c8\u7531\u591a\u53d8\u5c11\uff0c\u81f3\u5230\u4e3a6\u4e2a\uff0c\u90a3\u5c31\u53c8\u53d8\u56de\u94fe\u8868\u3002static final int TREEIFY_THRESHOLD = 8;<br>static final int UNTREEIFY_THRESHOLD = 6;<\/li>\n\n\n\n<li>\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u4f5c\u4e3ahashMap\u7684\u952e\uff0c\u4efb\u4f55\u7c7b\u90fd\u53ef\u4ee5\uff0c\u4e3b\u8981\u5b83\u8986\u76d6\u4e86hashCode()\u548cequals()\u65b9\u6cd5\uff0chashCode\u65b9\u6cd5\u7528\u4e8e\u5411HashMap\u4e2d\u63d2\u5165\u952e\uff0c\u800cequals()\u65b9\u6cd5\u7528\u4e8e\u4eceHashMap\u4e2d\u68c0\u9519\u503c\u3002<\/li>\n\n\n\n<li>HashMap\u4e2d\u53ea\u5141\u8bb8\u4e00\u4e2a\u7a7a\u952e\u3002\u5982\u679cHashMap\u7684\u952e\u662f\u7a7a\u7684\uff0c\u90a3\u4e48\u5b83\u5c06\u59cb\u7ec8\u5b58\u5728\u4e8e\u7d22\u5f150\u4e2d\u3002\u5982\u679c\u8bd5\u56fe\u5728\u7a7a\u952e\u4e0a\u8c03\u7528hashCode()\u65b9\u6cd5\uff0c\u5219\u4f1a\u629b\u51faNullPointerException\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u5404\u79cd\u524d\u83dc\uff1a<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">HashMap\u7684\u8fed\u4ee3\u7528\u6cd5<\/h3>\n\n\n\n<p>\u4f7f\u7528keySet()\u548citerator()<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp;Set&lt;String&gt; keySet = map.keySet();<br> &nbsp;Iterator&lt;String&gt; keySetIterator = keySet.iterator();<br> &nbsp; &nbsp; &nbsp; while(keySetIterator.hasNext()) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String key = keySetIterator.next();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"key: \" + key + \" value: \" + &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.get(key));<br>  }<\/pre>\n\n\n\n<p>\u4f7f\u7528entrySet()\u548c\u589e\u5f3a\u7684for\u5faa\u73af<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for (String key : map.keySet()) {<br> &nbsp; &nbsp;System.out.println(\"key: \" + key + \" value: \" +             map.get(key));<br>}<\/pre>\n\n\n\n<p>\u4f7f\u7528entrySet()\u548citerator()<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> Set&lt;Map.Entry&lt;String, Object&gt;&gt; entrySet2 = map.entrySet();<br> Iterator&lt;Map.Entry&lt;String, Object&gt;&gt; entryIterator = &nbsp; &nbsp;     entrySet2.iterator();<br> while (entryIterator.hasNext()) {<br> &nbsp; &nbsp; Map.Entry&lt;String, Object&gt; entry = entryIterator.next();<br> &nbsp; &nbsp; &nbsp;System.out.println(\"key: \" + entry.getKey() + \" value: \" + map.get(entry.getKey()));<br>  }<\/pre>\n\n\n\n<p>\u4f7f\u7528keySet()\u548cget()\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Set&lt;Map.Entry&lt;String, Object&gt;&gt; entrySet = map.entrySet();<br>for (Map.Entry entry: entrySet) {<br> &nbsp; System.out.println(\"key: \" + entry.getKey() + \" value: \" + map.get(entry.getKey()));<br> &nbsp; &nbsp; &nbsp;  }<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">HashMap\u548cHashTable\u7684\u533a\u522b\uff1f<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6700\u5927\u533a\u522b\u5c31\u662fHashMap\u80fd\u591f\u5bb9\u7eb3key\u4e3anull\u6216\u8005value\u4e3anull\u7684\uff0c\u4f46\u662fHashTable\u4e0d\u53ef\u4ee5\u3002<\/li>\n\n\n\n<li>HashTable\u662f\u540c\u6b65\u7684\uff0cHashMap\u4e0d\u662f\u540c\u6b65\u7684\uff0c\u4e5f\u5c31\u610f\u5473\u7740\u5176\u7ebf\u7a0b\u4e0d\u5b89\u5168<\/li>\n\n\n\n<li>HashMap\u4f1a\u6bd4HashTable\u66f4\u5feb\uff0c\u56e0\u4e3aHashMap\u4e0d\u662f\u540c\u6b65\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">HashMap\u548cArrayList\u7684\u533a\u522b\uff1f<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b9e\u73b0:Java\u4e2dHashMap\u5b9e\u73b0Map\u63a5\u53e3\uff0cArrayList\u5b9e\u73b0List\u63a5\u53e3<\/li>\n\n\n\n<li>\u5b58\u50a8\u5bf9\u8c61:HashMap\u5b58\u50a8key\u548cvalue\u4e24\u4e2a\u5bf9\u8c61\uff0cArrayList\u53ea\u5b58\u50a8\u4e00\u4e2a\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li>\u6392\u5e8f:HashMap\u4e0d\u63d0\u4f9b\u6392\u5e8f\u4fdd\u8bc1\uff0c\u800cArrayList\u7ef4\u62a4\u63d2\u5165\u5b83\u4eec\u7684\u5bf9\u8c61\u7684\u987a\u5e8f\u3002<\/li>\n\n\n\n<li>\u91cd\u590d:HashMap\u4e0d\u5141\u8bb8\u91cd\u590d\u952e\uff0c\u5c3d\u7ba1\u5b83\u5141\u8bb8\u91cd\u590d\u503c\uff0c\u800cArrayList\u5141\u8bb8\u91cd\u590d\u3002<\/li>\n\n\n\n<li>Equals\u548cHashcode\u65b9\u6cd5:HashMap\u548cArrayList\u4e4b\u95f4\u7684\u7b2c\u4e94\u4e2a\u533a\u522b\u662f\uff0cHashMap\u7684\u952e\u5fc5\u987b\u5b9e\u73b0Equals()\u548cHashcode()\u65b9\u6cd5\uff0c\u800cArrayList\u5219\u4e0d\u5fc5\u5b9e\u73b0\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li>get()\u65b9\u6cd5\u6027\u80fd:HashMap\u548cArrayList\u7684\u7b2c\u516d\u4e2a\u533a\u522b\u662f\uff0cHashMap get(key)\u65b9\u6cd5\u7684\u6027\u80fd\u5728\u6700\u597d\u7684\u60c5\u51b5\u4e0b\u662fO(1)\uff0c\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\u662fO(n)\uff0c\u800cArrayList get(index)\u65b9\u6cd5\u7684\u6027\u80fd\u603b\u662fO(1)\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u5404\u79cd\u9886\u609f+\u6e90\u7801\uff1a<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">HashMap\u4e2d\u7684\u6570\u7ec4\u662f\u5728\u4f55\u65f6\u8fdb\u884c\u521d\u59cb\u5316\u7684\uff1f<\/h3>\n\n\n\n<p>\u867d\u7136HashMap\u6709\u56db\u79cd\u6784\u9020\u65b9\u6cd5\uff0c\u4f46\u662f\u91cc\u9762\u5e76\u6ca1\u6709\u5b9a\u4e49\u6570\u7ec4\u521d\u59cb\u5316\u7684\u4ee3\u7801\u3002\u5b9e\u9645\u662f\u5728put\u65b9\u6cd5\u91cc\u624d\u4f1a\u521d\u59cb\u5316\u6570\u7ec4\u3002\u751f\u5f53\u7b2c\u4e00\u6b21\u8c03\u7528put\u65f6\uff0c\u4ed6\u9996\u5148\u4f1a\u5224\u65ad\u5e95\u5c42\u6570\u7ec4\u662f\u5426\u88ab\u521d\u59cb\u5316\uff0c\u5982\u679c\u6ca1\u6709\u521d\u59cb\u5316\uff0c\u5219\u5148\u8fdb\u884c\u521d\u59cb\u5316\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/\u6784\u9020\u65b9\u6cd5\u91cc\u7684\u8d4b\u503c\u64cd\u4f5c\uff0c\u6ca1\u6709\u770b\u5230\u5b9a\u4e49\u6570\u7ec4\u7684\u4ee3\u7801<br>public HashMap() {<br> &nbsp; &nbsp;this.loadFactor = DEFAULT_LOAD_FACTOR; \/\/\u8d4b\u503c\u64cd\u4f5c<br>}<br>\/\/put\u65b9\u6cd5\u91cc\u7684\u521d\u59cb\u5316\u6570\u7ec4\u90e8\u5206<br>if ((tab = table) == null || (n = tab.length) == 0)<br>n = (tab = resize()).length;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6211\u4eec\u7684HashMap\u7684\u5de5\u4f5c\u539f\u7406\uff1f<\/h3>\n\n\n\n<p>1.HashMap\u662f\u57fa\u4e8ehash\u7b97\u6cd5\u4ea7\u751f\u4e00\u4e2a\u96c6\u5408\u5de5\u5177\u7c7b\u3002<\/p>\n\n\n\n<p>2.HashMap\u91c7\u7528key-value\u952e\u503c\u5bf9\u65b9\u5f0f\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">static class Node&lt;K,V&gt; implements Map.Entry&lt;K,V&gt; {<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;final int hash;<br> &nbsp; &nbsp; &nbsp; &nbsp;final K key;<br> &nbsp; &nbsp; &nbsp; &nbsp;V value;<br> &nbsp; &nbsp; &nbsp; &nbsp;Node&lt;K,V&gt; next;<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;Node(int hash, K key, V value, Node&lt;K,V&gt; next) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.hash = hash;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.key = key;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.value = value;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.next = next;<br>}<\/pre>\n\n\n\n<p>3.\u5f53\u901a\u8fc7HashMap\u4e2dput\u65b9\u6cd5\u5b58\u5165\u952e\u503c\u5bf9\u65f6\uff0c\u9996\u5148\u901a\u8fc7hash\u51fd\u6570\uff08\u7531Object\u7c7b\u7ee7\u627f\u800c\u6765\uff09\u8ba1\u7b97key\u5173\u8054\u7684hashcode\u503c\u3002\u901a\u8fc7hashcode\u503c\u786e\u5b9a\u6570\u636ebucket\u4e2d\u4f4d\u7f6e\uff08A bucket is used to store key value pairs .bucket\u5c31\u662f\u5b58\u50a8\u8bb8\u591a\u952e\u503c\u5bf9\u7684\uff09\u3002\u5728\u786e\u8ba4\u4f4d\u7f6e\u4e4b\u540e\u5c06key-value\u5c01\u88c5\u7684Entry\u5bf9\u8c61\u5b58\u50a8\u5230\u6b64bucket\u4e2d\uff08\u6216\u8005\u51fa\u73b0\u8986\u76d6\u64cd\u4f5c\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">tab[i] = newNode(hash, key, value, null);<br>e = ((TreeNode&lt;K,V&gt;)p).putTreeVal(this, tab, hash, key, value);<br>afterNodeAccess(e);<\/pre>\n\n\n\n<p>4.\u5f53\u901a\u8fc7hashMap\u7684get(key k)\u65b9\u6cd5\u8bfb\u53d6\u503c\u65f6\uff0c\u9996\u5148\u901a\u8fc7hashCode\u65b9\u6cd5\u5c06key\u8f6c\u6362\u6210hashcode\uff0c\u7136\u540e\u5728bucket\u4e0a\u627e\u5230\u5bf9\u5e94\u4f4d\u7f6e\u4e0a\u7684Entry\u5bf9\u8c61\u8bfb\u53d6\u5e76\u8f93\u51faEntry\u5bf9\u8c61\u91cc\u653e\u7684value\u503c\uff0c\u5426\u5219\u8fd4\u56denull\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">return (e = getNode(hash(key), key)) == null ? null : e.value;<br>\/\/?first node \u4e0d\u662f\u7684\u8bdd\u5c31\u5faa\u73af\u627e\u4e86<br> do {<br> &nbsp; &nbsp; if (e.hash == hash &amp;&amp;<br> &nbsp; &nbsp; ((k = e.key) == key || (key != null &amp;&amp; key.equals(k))))<br> &nbsp; &nbsp; return e;<br> } while ((e = e.next) != null);<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/qqiukulele.cn\/wp-content\/uploads\/2021\/09\/how-hashmap-works-internally-in-java-.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/qqiukulele.cn\/wp-content\/uploads\/2021\/09\/how-hashmap-works-internally-in-java-.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\"\/><\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">hash\u78b0\u649e\u4ee5\u53ca\u89e3\u51b3\u65b9\u6848\uff1f<\/h3>\n\n\n\n<p>hash\u78b0\u649e\u5c31\u662f\u6211\u548c\u4f60\u90fd\u9700\u8981\u5728\u8fd9\u4e2abucket\u91cc\u5b58\u7740\u3002\u4f7f\u7528put\u65b9\u6cd5\u6765\u4fdd\u5b58\u6570\u636e\u65f6\uff0c\u5c06\u6570\u636e\u4e2d\u7684KEY\u8f6c\u6362\u4e3ahash\u503c\uff0c\u518d\u6839\u636ehash\u503c\u5b9a\u4f4d\u6570\u7ec4\u5b58\u653e\u4f4d\u7f6e\uff0c\u82e5\u5f53\u524d\u6570\u7ec4\u7684\u4f4d\u7f6e\u4e0a\u5df2\u7ecf\u5b58\u5728\u94fe\u8868\u5e76\u4e14\u65b0\u6570\u636ekey\u4e0e\u94fe\u8868\u4e0a\u8282\u70b9\u7684key\u4e0d\u540c\uff0c\u6b64\u65f6\u904d\u5386\u94fe\u8868\u8282\u70b9\uff0c\u627e\u5230\u76f8\u540c\u7684\u8986\u76d6\uff0c\u4e0d\u540c\u7684\u5219\u5c06\u65b0\u6570\u636e\u4f5c\u4e3a\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u5b58\u50a8\u5728\u94fe\u8868\u7684\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<p>hash\u78b0\u649e\u5b58\u5728\u7684\u95ee\u9898\u5c31\u662f\u94fe\u8868\u53ef\u80fd\u4f1a\u5f88\u957f\u3002\u6570\u636e\u91cf\u5927\u53ef\u80fd\u8fd8\u8981\u7b49\u5f85\uff0c\u56e0\u4e3a\u8981\u4e0d\u65ad\u53bb\u904d\u5386\u4f1a\u5bfc\u81f4\u5b58\u653e\u901f\u5ea6\u5c31\u4f1a\u53d8\u6162\u3002\u89e3\u51b3\u65b9\u6848\u5462\u5c31\u662f<strong>\u5f00\u653e\u5730\u5740\u65b9\u6848<\/strong>\u548c<strong>\u94fe\u8868\u7b97\u6cd5\u65b9\u6848<\/strong>\u3002<\/p>\n\n\n\n<p>\u5f00\u653e\u5730\u5740\u65b9\u6848\uff08Open Addressing\uff09\u786e\u4fdd\u6570\u7ec4\u5b58\u653e\u5c31\u662fkey-value\uff0c\u800c\u4e0d\u662f\u952e\u503c\u5bf9\uff08\u6563\u5217\u8868\uff09\u3002\u4e5f\u5c31\u662f\u6539\u53d8\u4e4b\u524d\u90a3\u79cd\u201c\u4e8c\u7ef4\u201d\u7684\u611f\u89c9\uff0c\u8fd9\u5c31\u662f\u4e00\u7ef4\u7684\u3002\u5982\u679c\u8981\u5b58\u5165\u6570\u636ekey\u5bf9\u5e94\u7684hash\u503c\u5728\u6570\u7ec4\u4e2d\u4f4d\u7f6e\u4e0a\u5df2\u7ecf\u5b58\u5728\u4e86\u6570\u636e\uff0c\u5219\u901a\u8fc7\u6570\u5b66\u7b97\u6cd5\u5bf9key\u8fdb\u884c\u65b0\u7684\u8fd0\u7b97\u5f97\u5230\u65b0\u7684hash\u503c\uff0c\u5c31\u662f\u65b0\u7684\u6570\u7ec4\u4f4d\u7f6e\uff0c\u5c06\u65b0\u6570\u7ec4\u5b58\u5165\u5230\u6570\u7ec4\u65b0\u4f4d\u7f6e\u3002\u4e0d\u540c\u7684 key \u4f1a\u751f\u6210\u4e0d\u4e00\u6837\u7684\u63a2\u6d4b\u5e8f\u5217\uff0c\u4e5f\u53ef\u4ee5\u60f3\u8c61\u6210\u662f\u4e00\u79cd\u865a\u62df\u94fe\u6761\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">root = hash(key) % m &nbsp; \/\/ \u7b2c\u4e00\u4e2a\u4f4d\u7f6e\uff0cm \u4e3a\u6570\u7ec4\u7684\u957f\u5ea6<br>index_i = (root + p(key, i)) % m &nbsp;\/\/ \u94fe\u6761\u4e2d\u7684\u7b2c i \u4e2a\u4f4d\u7f6e<br>\u200b<br>index_1 = (root + p(key, 1)) % m<br>index_2 = (root + p(key, 2)) % m <br>...<\/pre>\n\n\n\n<p>\u94fe\u8868\u7b97\u6cd5\u65b9\u6848\u5c31\u662f\u53d6\u5355\u5411\u94fe\u8868\u4ee3\u66ff\u6563\u5217\u8868\u3002\u5f53\u5b58\u5165\u6570\u636e\u7684key\u4e0e\u94fe\u8868\u7b2c\u4e00\u4e2a\u6570\u636ekey\u4e0d\u540c\u76f8\u540c\u65f6\uff0c\u5411\u7b2c\u4e00\u4e2a\u6570\u636enext\u6307\u9488\u6307\u5411\u65b0\u6570\u636e\u3002\u5c06\u6570\u636enext\u6307\u9488\u6307\u5411\u7b2c\u4e8c\u4e2a\u6570\u636e\u4f4d\u7f6e\uff0c\u8fd9\u4e2a\u6bd4\u8f83\u597d\u7406\u89e3\uff0c\u5c31\u662f\u7eaf\u7eaf\u7684\u94fe\u8868\uff0c\u6570\u636e\u5143\u7d20\u7684\u903b\u8f91\u987a\u5e8f\u901a\u8fc7\u94fe\u8868\u4e2d\u7684\u6307\u9488\u94fe\u63a5\u6b21\u5e8f\u5b9e\u73b0\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">get()\u65f6\uff0c\u8981\u662f\u4e24\u4e2a\u4e0d\u540ckey\u8ba1\u7b97\u7684hashcode\u76f8\u7b49\u65f6\u600e\u4e48\u529e\uff1f\u600e\u4e48\u8fa8\u522b\u6211\u8981\u53d6\u7684\u662f\u54ea\u4e2avalue\uff1f<\/h3>\n\n\n\n<p><strong>equals()\u65b9\u6cd5\u6765\u6551\u573a\uff01<\/strong>\u867d\u7136\u6211\u4eec\u90fd\u5bf9\u5e94\u7740\u4e00\u4e2abucket\uff0c\u4f46\u662f\u4e0d\u8981\u5fd8\u8bb0\u4e86bucket\u662f\u4e00\u4e2a\u94fe\u8868\u3002\u800c\u4e14\u5b83\u4e0d\u662fjava.util.LinkedList\u4e2d\u7684\u90a3\u4e2aLinkedList\u2014\u2014\u5b83\u662f\u4e00\u4e2a\u5355\u72ec\u7684\u5b9e\u73b0\uff0c\u4ec5\u7528\u4e8e\u6620\u5c04\u7684\u8868\u3002\u6240\u4ee5\u6211\u4eec\u53bb\u904d\u5386\u94fe\u8868\uff0c\u4f7f\u7528keys.equals()\u65b9\u6cd5\u6bd4\u8f83\u5b58\u50a8\u597d\u7684\u952e\u503c\uff0c\u76f4\u5230\u9047\u5230\u76f8\u7b49\u7684\uff0c\u4e5f\u5c31\u662f\u8fd4\u56detrue\u4e86\uff0c\u90a3\u4e48\u8fd9\u65f6\u5019\u518d\u8fd4\u56de\u76f8\u5e94\u7684value\u503c\u5c31\u597d\u5566~<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u73b0\u8c61\u4e5f\u53eb\u505a<strong>\u78b0\u649e\u6548\u5e94<\/strong>\uff1a\u5982\u679c\u4e24\u4e2a\u5bf9\u8c61\u62e5\u6709\u76f8\u7b49\u7684hashcode\u3002\u6b64\u65f6\u5728bucket\u4e0a\u5b58\u50a8\u4f4d\u7f6e\u4e00\u5b9a\u662f\u76f8\u540c\u7684\uff0c\u6b64\u65f6hashMap\u91c7\u7528\u94fe\u8868\u5f62\u5f0f\u5b58\u50a8\u4e24\u4e2a\u5bf9\u8c61\uff0c\u5c31\u597d\u6bd4\u8bf4\u5728\u7b2c\u4e00\u4e2aEntry\u5bf9\u8c61\u540e\u653e\u4e00\u4e2a\u6307\u9488\u3002<\/p>\n\n\n\n<p>\u4e24\u4e2a\u5bf9\u8c61\u7684hashCode\u76f8\u7b49\u65f6\uff0c\u5979\u4eec\u7684key\u672a\u5fc5\u76f8\u7b49\uff0c\u4f46\u662f\u8981\u662f\u90fd\u76f8\u7b49\uff0c\u90a3\u5c31overrides\u5427~\u5982\u679c\u60a8\u8bd5\u56fe\u5b58\u50a8HashMap\u4e2d\u5df2\u7ecf\u5b58\u5728\u7684\u952e\uff0c\u90a3\u4e48\u5b83\u5c06\u7528\u65b0\u503c\u8986\u76d6\u65e7\u503c\uff0cHashMap\u7684\u5927\u5c0f\u4e5f\u4e0d\u53d8\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5f53\u4f60\u5728HashMap\u4e0a\u8c03\u7528keySet()\u65b9\u6cd5\u6765\u83b7\u53d6\u6240\u6709\u952e\u65f6\uff0c\u5b83\u5c06\u8fd4\u56deSet\u800c\u4e0d\u662fCollection\uff0c\u56e0\u4e3aSet\u4e0d\u5141\u8bb8\u91cd\u590d\u3002<\/p>\n\n\n\n<p>\u8fd8\u662f\u603b\u7ed3\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>HashMap\u901a\u8fc7hash\u51fd\u6570\u8ba1\u7b97\u51fakey\u5bf9\u5e94\u7684HashCode<\/li>\n\n\n\n<li>HashMap\u901a\u8fc7HashCode\u5b9a\u4f4dbucket\u4e0a\u5b58\u50a8\u4f4d\u7f6e\uff08\u6570\u7ec4\u4e0b\u6807\uff09<\/li>\n\n\n\n<li>\u5982\u679c\u6570\u7ec4\u4e0b\u6807\u5bf9\u5e94\u94fe\u8868\uff0c\u6b64\u65f6HashMap\u904d\u5386\u94fe\u8868\u4e2d\u6bcf\u4e00\u4e2aEntry\u5bf9\u8c61<\/li>\n\n\n\n<li>\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\uff0cHashMap\u4f7f\u7528equals\u65b9\u6cd5\u68c0\u6d4b\u5f53\u524dEntry\u5bf9\u8c61\u4e2dkey\u53bb\u4f20\u5165\u53ef\u4ee5\u662f\u5426\u76f8\u7b49\uff0c\u5982\u679c\u76f8\u7b49\u8bfb\u53d6Entry\u4e2dvalue\u503c\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u5982\u4f55\u8861\u91cfHashMap\u7684\u6027\u80fd\u5462\uff1f<\/h3>\n\n\n\n<p>\u6839\u636eOracle Java\u6587\u6863\uff0cHashMap\u5b9e\u4f8b\u6709\u4e24\u4e2a\u5f71\u54cd\u5176\u6027\u80fd\u7684\u53c2\u6570:<strong>\u521d\u59cb\u5bb9\u91cf<\/strong>\u548c<strong>\u8d1f\u8f7d\u56e0\u5b50<\/strong>\u3002\u5bb9\u91cf\u662f\u54c8\u5e0c\u8868\u4e2d\u7684\u6876\u6570\uff0c\u521d\u59cb\u5bb9\u91cf\u53ea\u662f\u521b\u5efa\u54c8\u5e0c\u8868\u65f6\u7684\u5bb9\u91cf\u3002<\/p>\n\n\n\n<p>\u8d1f\u8f7d\u56e0\u5b50\u662f\u8861\u91cf\u6563\u5217\u8868\u5bb9\u91cf\u81ea\u52a8\u589e\u52a0\u4e4b\u524d\u5141\u8bb8\u5176\u8fbe\u5230\u7684\u6ee1\u5ea6\u3002\u5f53\u54c8\u5e0c\u8868\u4e2d\u7684\u6761\u76ee\u6570\u91cf\u8d85\u8fc7\u8d1f\u8f7d\u56e0\u5b50\u548c\u5f53\u524d\u5bb9\u91cf\u7684\u4e58\u79ef\u65f6\uff0c\u5c06\u5bf9\u54c8\u5e0c\u8868\u8fdb\u884c\u91cd\u65b0\u54c8\u5e0c(\u5373\u91cd\u5efa\u5185\u90e8\u6570\u636e\u7ed3\u6784)\uff0c\u4ee5\u4fbf\u54c8\u5e0c\u8868\u7684\u6876\u6570\u91cf\u5927\u7ea6\u662f\u5f53\u524d\u7684\u4e24\u500d\u3002<\/p>\n\n\n\n<p>\u5728HashMap\u7c7b\u4e2d\uff0cload factor\u7684\u9ed8\u8ba4\u503c\u662f(.75)\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">static final float DEFAULT_LOAD_FACTOR = 0.75f;\/\/\u88c5\u8f7d\u56e0\u5b50<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">HashMap\u7684\u6269\u5bb9\u673a\u5236<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>HashMap\u9ed8\u8ba4\u5bb9\u91cf\u662f16\uff0c\u5c31\u662f\u521d\u59cb\u5316\u7684\u65f6\u5019\u5f00\u8f9f\u4e8616\u5757\u5185\u5b58\u3002<\/li>\n\n\n\n<li>HashMap\u9ed8\u8ba4\u8d1f\u8f7d\u56e0\u5b50\u662f\u6211\u4eec\u8bf4\u76840.75<\/li>\n\n\n\n<li>HashMap\u5728\u8c03\u7528put\u65b9\u6cd5\u65f6\uff0c\u5982\u679c\u53d1\u73b0HashMap\u4e2dbucket\u5360\u7528\u7387\u523016*0.75\u503c\u5219\u8fdb\u884c\u6269\u5bb9<\/li>\n\n\n\n<li>HashMap\u5728\u6269\u5bb9\u540e\u5176\u5bb9\u91cf\u8fbe\u5230\u539f\u59cb\u5bb9\u91cf2\u500d<\/li>\n\n\n\n<li>HashMap\u5728\u6269\u5bb9\u540e\u901a\u8fc7\u91cd\u65b0\u8ba1\u7b97entry\u5bf9\u8c61\u5728bucket\u4e2d\u5b58\u50a8\u4f4d\u7f6e\/\/hashcode\u662f\u8fd9\u4e48\u8ba1\u7b97\u6765\u7684:\u5f02\u6216\uff0c\u7ecf\u8fc7\u5f02\u6216\uff0c\u53ef\u4ee5\u63d0\u9ad8hash\u503c\u7684\u6563\u5217\u5ea6<br><\/li>\n<\/ol>\n\n\n\n<p><code>return (key == null) ? 0 : (h = key.hashCode()) ^ (h &gt;&gt;&gt; 16);<\/code><\/p>\n\n\n\n<p><code>static final int DEFAULT_INITIAL_CAPACITY = 1 &lt;&lt; 4; \/\/ aka 16<br>&nbsp;newCap = DEFAULT_INITIAL_CAPACITY;\/\/16<br>&nbsp;newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);\/\/12\uff082\u7684\u500d\u6570\u907f\u514d\u5185\u5b58\u788e\u7247\uff0c\u53ef\u4ee5\u51cf\u5c11\u51b2\u7a81\uff0c\u79fb\u4f4d\u64cd\u4f5c\u6548\u7387\u4e5f\u9ad8\uff09<br>&nbsp;threshold = newThr; \/\/12<br>(p = tab[i = (n - 1) &amp; hash]) \/\/\u505a\u4e0e\u8fd0\u7b97<br><br>&nbsp; \/\/\u6269\u5bb9<br>if(--size &gt; threshold) &nbsp;resize();<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">HashMap\u4e3a\u4ec0\u4e48\u8981\u8fdb\u884c\u6269\u5bb9\uff1f<\/h3>\n\n\n\n<p>HashMap\u8fdb\u884c\u6269\u5bb9\u64cd\u4f5c\u65f6\u5e76\u4e0d\u662f\u4e3a\u4e86\u5b58\u50a8\u66f4\u591a\u6570\u636e\u3002HashMap\u8fdb\u884c\u6269\u5bb9\u65f6\u4e3a\u4e86\u51cf\u5c11\u78b0\u649e\u51e0\u7387\uff0c\u6765\u63d0\u4f9bHashMap\u8fd0\u884c\u6548\u7387\u3002\u56e0\u4e3a\u7ecf\u5e38\u53d1\u751f\u78b0\u649e\u51e0\u7387\u7684\u65f6\u5019\uff0c\u662f\u8981\u53bb\u904d\u5386\u94fe\u8868\u7684\uff0c\u8fd9\u6837\u5c31\u589e\u52a0\u4e86\u65f6\u95f4\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">HashMap\u6269\u5bb9\u65f6\u4f1a\u51fa\u73b0\u4ec0\u4e48\u95ee\u9898\uff1f<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u5355\u7ebf\u7a0b\u60c5\u51b5\u4e0b\uff0cHashMap\u6269\u5bb9\u65f6\u5bf9\u8fd0\u884c\u6548\u7387\u4ea7\u751f\u964d\u4f4e\u95ee\u9898\uff0c\u5c31\u4e0d\u4f1a\u6709\u5176\u4ed6\u95ee\u9898\u3002<\/li>\n\n\n\n<li>\u591a\u7ebf\u7a0b\u60c5\u51b5\u4e0b\uff0c\u4ea7\u751f\u7ade\u6761\u4ef6\uff08race condition\uff09\u8fdb\u800c\u5bfc\u81f4\u6b7b\u5faa\u73af\u3002\u5047\u8bbe\u7ebf\u7a0ba\u548c\u7ebf\u7a0bb\u5728\u540c\u4e00\u65f6\u523b\u4e0b\u90fd\u8ba4\u4e3a\u5f53\u524dHashMap\u8fdb\u884c\u6269\u5bb9\uff0c\u6269\u5bb9\u94fe\u8868\u4e2dEntry\u5bf9\u8c61\u5b58\u653e\u987a\u5e8f\u4f1a\u53cd\u8fc7\u6765\uff0c\u6b64\u65f6Entry\u5bf9\u8c61\u65b0\u7684Bucket\u4f4d\u7f6e\u4e0a\u4ece\u94fe\u8868\u4e2d\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u5f00\u59cb\u91cd\u65b0\u5b58\u50a8\u3002HashMap\u8fd9\u6837\u505a\u7684\u672c\u6b21\u9632\u6b62\u51fa\u73b0\u5c3e\u90e8\u904d\u5386\u5730\u573a\u666f\u3002\u5f53\u65f6\u6b64\u65f6\u5982\u679c\u7ebf\u7a0ba\u548c\u7ebf\u7a0bb\u90fd\u8981\u5c06\u81ea\u5df1\u7684\u7ba1\u7406\u7684Entry\u5b58\u5165\u5230\u540c\u4e00\u4e2a\u94fe\u8868\u4e2d\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u6b64\u65f6\u51fa\u73b0\u7ade\u4e89\u6761\u4ef6\u8fdb\u884c\u4ea7\u751f\u4e86\u6b7b\u5faa\u73af\u3002<\/li>\n\n\n\n<li>\u5728\u591a\u7ebf\u7a0b\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0d\u80fd\u4f7f\u7528HashMap<\/li>\n<\/ul>\n\n\n\n<p>\u53c2\u8003\u94fe\u63a5\uff1a<\/p>\n\n\n\n<p>\u2460<a href=\"https:\/\/javahungry.blogspot.com\/2013\/08\/hashing-how-hash-map-works-in-java-or.html\"> Hashing :How HashMap Works In Java Or How Get() Method Works Internally<\/a><\/p>\n\n\n\n<p>\u2461<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/68213110\"> hash\u7ed3\u6784\u7684\u53e6\u4e00\u79cd\u5f62\u5f0f\u2014\u2014\u5f00\u653e\u5730\u5740\u6cd5<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u54c8\u54c8\u54c8\u54c8\u91cd\u7f6e\u670d\u52a1\u5668\u5566\uff0c\u53c8\u628a\u535a\u5ba2\u5f04\u56de\u6765\u4e86\uff0c\u5f00\u7bc7\u5e86\u795d\u4e00\u4e0b~<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[15],"class_list":["post-233","post","type-post","status-publish","format-standard","hentry","category-22","tag-java"],"_links":{"self":[{"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/posts\/233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=233"}],"version-history":[{"count":5,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/posts\/233\/revisions"}],"predecessor-version":[{"id":467,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/posts\/233\/revisions\/467"}],"wp:attachment":[{"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}