{"id":11518,"date":"2023-06-29T16:28:15","date_gmt":"2023-06-29T08:28:15","guid":{"rendered":"http:\/\/123.57.164.21\/?p=11518"},"modified":"2023-06-29T16:28:15","modified_gmt":"2023-06-29T08:28:15","slug":"hashmap%e4%b8%ba%e4%bb%80%e4%b9%88%e6%98%af%e7%ba%bf%e7%a8%8b%e4%b8%8d%e5%ae%89%e5%85%a8%e7%9a%84","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=11518","title":{"rendered":"HashMap\u4e3a\u4ec0\u4e48\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684"},"content":{"rendered":"\n<p>\u5927\u5bb6\u90fd\u77e5\u9053HasnMap\u5728\u591a\u7ebf\u7a0b\u7684\u573a\u666f\u4e2d\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u4e3a\u4ec0\u4e48\u5462\uff1f\u9996\u5148\u8981\u4ece\u5b83\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u8bf4\u8d77\u2026\u2026<\/p>\n\n\n\n<p><strong>JDK7\u4e2d\u7684HashMap<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>HashMap\u5e95\u5c42\u7ef4\u62a4\u4e00\u4e2a\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u9879\u90fd\u662f\u4e00\u4e2aEntry<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">transient Entry&lt;K,V>[] table;<\/pre>\n\n\n\n<p>\u6211\u4eec\u5411 HashMap \u4e2d\u6240\u653e\u7f6e\u7684\u5bf9\u8c61\u5b9e\u9645\u4e0a\u662f\u5b58\u50a8\u5728\u8be5\u6570\u7ec4\u5f53\u4e2d\uff1b \u800cMap\u4e2d\u7684key\uff0cvalue\u5219\u4ee5Entry\u7684\u5f62\u5f0f\u5b58\u653e\u5728\u6570\u7ec4\u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">static class Entry&lt;K,V> implements Map.Entry&lt;K,V> {\n        final K key;\n        V value;\n        Entry&lt;K,V> next;\n        int hash;<\/pre>\n\n\n\n<p>\u800c\u8fd9\u4e2aEntry\u5e94\u8be5\u653e\u5728\u6570\u7ec4\u7684\u54ea\u4e00\u4e2a\u4f4d\u7f6e\u4e0a\uff08\u8fd9\u4e2a\u4f4d\u7f6e\u901a\u5e38\u79f0\u4e3a\u4f4d\u6876\u6216\u8005hash\u6876\uff0c\u5373hash\u503c\u76f8\u540c\u7684Entry\u4f1a\u653e\u5728\u540c\u4e00\u4f4d\u7f6e\uff0c\u7528\u94fe\u8868\u76f8\u8fde\uff09\uff0c\u662f\u901a\u8fc7key\u7684hashCode\u6765\u8ba1\u7b97\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">final int hash(Object k) {\n        int h = 0;\n        h ^= k.hashCode();\n\n        h ^= (h >>> 20) ^ (h >>> 12);\n        return h ^ (h >>> 7) ^ (h >>> 4);\n    }\n<\/pre>\n\n\n\n<p>\u901a\u8fc7hash\u8ba1\u7b97\u51fa\u6765\u7684\u503c\u5c06\u4f1a\u4f7f\u7528indexFor\u65b9\u6cd5\u627e\u5230\u5b83\u5e94\u8be5\u6240\u5728\u7684table\u4e0b\u6807\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">static int indexFor(int h, int length) {\n        return h &amp; (length-1);\n    }<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u5176\u5b9e\u76f8\u5f53\u4e8e\u5bf9table.length\u53d6\u6a21\u3002<\/p>\n\n\n\n<p>\u5f53\u4e24\u4e2akey\u901a\u8fc7hashCode\u8ba1\u7b97\u76f8\u540c\u65f6\uff0c\u5219\u53d1\u751f\u4e86hash\u51b2\u7a81(\u78b0\u649e)\uff0cHashMap\u89e3\u51b3hash\u51b2\u7a81\u7684\u65b9\u5f0f\u662f\u7528\u94fe\u8868\u3002<\/p>\n\n\n\n<p>\u5f53\u53d1\u751fhash\u51b2\u7a81\u65f6\uff0c\u5219\u5c06\u5b58\u653e\u5728\u6570\u7ec4\u4e2d\u7684Entry\u8bbe\u7f6e\u4e3a\u65b0\u503c\u7684next\uff08\u8fd9\u91cc\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6bd4\u5982A\u548cB\u90fdhash\u540e\u90fd\u6620\u5c04\u5230\u4e0b\u6807i\u4e2d\uff0c\u4e4b\u524d\u5df2\u7ecf\u6709A\u4e86\uff0c\u5f53map.put(B)\u65f6\uff0c\u5c06B\u653e\u5230\u4e0b\u6807i\u4e2d\uff0cA\u5219\u4e3aB\u7684next\uff0c\u6240\u4ee5\u65b0\u503c\u5b58\u653e\u5728\u6570\u7ec4\u4e2d\uff0c\u65e7\u503c\u5728\u65b0\u503c\u7684\u94fe\u8868\u4e0a\uff09<\/p>\n\n\n\n<p>\u793a\u610f\u56fe\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-49-1024x559.png\" alt=\"\" class=\"wp-image-11520\" width=\"440\" height=\"239\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-49-1024x559.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-49-300x164.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-49-768x419.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-49-830x453.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-49-230x126.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-49-350x191.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-49-480x262.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-49.png 1048w\" sizes=\"(max-width: 440px) 100vw, 440px\" \/><\/figure><\/div>\n\n\n\n<p>\u6240\u4ee5\u5f53hash\u51b2\u7a81\u5f88\u591a\u65f6\uff0cHashMap\u9000\u5316\u6210\u94fe\u8868\u3002<\/p>\n\n\n\n<p><strong>\u603b\u7ed3\u4e00\u4e0bmap.put\u540e\u7684\u8fc7\u7a0b\uff1a<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5f53\u5411 HashMap \u4e2d put \u4e00\u5bf9\u952e\u503c\u65f6\uff0c\u5b83\u4f1a\u6839\u636e key\u7684 hashCode \u503c\u8ba1\u7b97\u51fa\u4e00\u4e2a\u4f4d\u7f6e\uff0c \u8be5\u4f4d\u7f6e\u5c31\u662f\u6b64\u5bf9\u8c61\u51c6\u5907\u5f80\u6570\u7ec4\u4e2d\u5b58\u653e\u7684\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u8be5\u4f4d\u7f6e\u6ca1\u6709\u5bf9\u8c61\u5b58\u5728\uff0c\u5c31\u5c06\u6b64\u5bf9\u8c61\u76f4\u63a5\u653e\u8fdb\u6570\u7ec4\u5f53\u4e2d\uff1b\u5982\u679c\u8be5\u4f4d\u7f6e\u5df2\u7ecf\u6709\u5bf9\u8c61\u5b58\u5728\u4e86\uff0c\u5219\u987a\u7740\u6b64\u5b58\u5728\u7684\u5bf9\u8c61\u7684\u94fe\u5f00\u59cb\u5bfb\u627e(\u4e3a\u4e86\u5224\u65ad\u662f\u5426\u503c\u76f8\u540c\uff0cmap\u4e0d\u5141\u8bb8&lt;key,value&gt;\u952e\u503c\u5bf9\u91cd\u590d)\uff0c \u5982\u679c\u6b64\u94fe\u4e0a\u6709\u5bf9\u8c61\u7684\u8bdd\uff0c\u518d\u53bb\u4f7f\u7528 equals\u65b9\u6cd5\u8fdb\u884c\u6bd4\u8f83\uff0c\u5982\u679c\u5bf9\u6b64\u94fe\u4e0a\u7684\u6bcf\u4e2a\u5bf9\u8c61\u7684 equals \u65b9\u6cd5\u6bd4\u8f83\u90fd\u4e3a false\uff0c\u5219\u5c06\u8be5\u5bf9\u8c61\u653e\u5230\u6570\u7ec4\u5f53\u4e2d\uff0c\u7136\u540e\u5c06\u6570\u7ec4\u4e2d\u8be5\u4f4d\u7f6e\u4ee5\u524d\u5b58\u5728\u7684\u90a3\u4e2a\u5bf9\u8c61\u94fe\u63a5\u5230\u6b64\u5bf9\u8c61\u7684\u540e\u9762\u3002<\/p>\n\n\n\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5f53key\u4e3anull\u65f6\uff0c\u90fd\u653e\u5230table[0]\u4e2d<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">private V putForNullKey(V value) {\n        for (Entry&lt;K,V> e = table[0]; e != null; e = e.next) {\n            if (e.key == null) {\n                V oldValue = e.value;\n                e.value = value;\n                e.recordAccess(this);\n                return oldValue;\n            }\n        }\n        modCount++;\n        addEntry(0, null, value, 0);\n        return null;\n    }<\/pre>\n\n\n\n<p>\u5f53size\u5927\u4e8ethreshold\u65f6\uff0c\u4f1a\u53d1\u751f\u6269\u5bb9\u3002<\/p>\n\n\n\n<p><strong>threshold=capacity*loadFactor<\/strong><\/p>\n\n\n\n<ul><li>capacity\uff1a\u5f53\u524d\u6570\u7ec4\u5bb9\u91cf\uff0c\u59cb\u7ec8\u4fdd\u6301 2^n\uff0c\u53ef\u4ee5\u6269\u5bb9\uff0c\u6269\u5bb9\u540e\u6570\u7ec4\u5927\u5c0f\u4e3a\u5f53\u524d\u7684 2 \u500d\u3002<\/li><li>loadFactor\uff1a\u8d1f\u8f7d\u56e0\u5b50\uff0c\u9ed8\u8ba4\u4e3a 0.75\u3002<\/li><li>threshold\uff1a\u6269\u5bb9\u7684\u9608\u503c\uff0c\u7b49\u4e8e capacity * loadFactor<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void addEntry(int hash, K key, V value, int bucketIndex) {\n        if ((size >= threshold) &amp;&amp; (null != table[bucketIndex])) {\n            resize(2 * table.length);\n            hash = (null != key) ? hash(key) : 0;\n            bucketIndex = indexFor(hash, table.length);\n        }\n\n        createEntry(hash, key, value, bucketIndex);\n    }<\/pre>\n\n\n\n<p>jdk7\u4e2dresize\uff0c\u53ea\u6709\u5f53 size>=threshold\u5e76\u4e14 table\u4e2d\u7684\u90a3\u4e2a\u69fd\u4e2d\u5df2\u7ecf\u6709Entry\u65f6\uff0c\u624d\u4f1a\u53d1\u751fresize\u3002\u5373\u6709\u53ef\u80fd\u867d\u7136size>=threshold\uff0c\u4f46\u662f\u5fc5\u987b\u7b49\u5230\u6bcf\u4e2a\u69fd\u90fd\u81f3\u5c11\u6709\u4e00\u4e2aEntry\u65f6\uff0c\u624d\u4f1a\u6269\u5bb9\u3002\u8fd8\u6709\u6ce8\u610f\u6bcf\u6b21resize\u90fd\u4f1a\u6269\u5927\u4e00\u500d\u5bb9\u91cf<\/p>\n\n\n\n<p><strong>JDK8\u4e2d\u7684HashMap<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e00\u76f4\u5230JDK7\u4e3a\u6b62\uff0cHashMap\u7684\u7ed3\u6784\u90fd\u662f\u8fd9\u4e48\u7b80\u5355\uff0c\u57fa\u4e8e\u4e00\u4e2a\u6570\u7ec4\u4ee5\u53ca\u591a\u4e2a\u94fe\u8868\u7684\u5b9e\u73b0\uff0chash\u503c\u51b2\u7a81\u7684\u65f6\u5019\uff0c\u5c31\u5c06\u5bf9\u5e94\u8282\u70b9\u4ee5\u94fe\u8868\u7684\u5f62\u5f0f\u5b58\u50a8\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\u5b50\u7684HashMap\u6027\u80fd\u4e0a\u5c31\u62b1\u6709\u4e00\u5b9a\u7591\u95ee\uff0c\u5982\u679c\u8bf4\u6210\u767e\u4e0a\u5343\u4e2a\u8282\u70b9\u5728hash\u65f6\u53d1\u751f\u78b0\u649e\uff0c\u5b58\u50a8\u4e00\u4e2a\u94fe\u8868\u4e2d\uff0c\u90a3\u4e48\u5982\u679c\u8981\u67e5\u627e\u5176\u4e2d\u4e00\u4e2a\u8282\u70b9\uff0c\u90a3\u5c31\u4e0d\u53ef\u907f\u514d\u7684\u82b1\u8d39O(N)\u7684\u67e5\u627e\u65f6\u95f4\uff0c\u8fd9\u5c06\u662f\u591a\u4e48\u5927\u7684\u6027\u80fd\u635f\u5931\u3002\u8fd9\u4e2a\u95ee\u9898\u7ec8\u4e8e\u5728JDK8\u4e2d\u5f97\u5230\u4e86\u89e3\u51b3\u3002\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u94fe\u8868\u67e5\u627e\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n),\u800c\u7ea2\u9ed1\u6811\u4e00\u76f4\u662fO(logn),\u8fd9\u6837\u4f1a\u63d0\u9ad8HashMap\u7684\u6548\u7387\u3002<\/p>\n\n\n\n<p>JDK7\u4e2dHashMap\u91c7\u7528\u7684\u662f\u4f4d\u6876+\u94fe\u8868\u7684\u65b9\u5f0f\uff0c\u5373\u6211\u4eec\u5e38\u8bf4\u7684\u6563\u5217\u94fe\u8868\u7684\u65b9\u5f0f\uff0c\u800cJDK8\u4e2d\u91c7\u7528\u7684\u662f\u4f4d\u6876+\u94fe\u8868\/\u7ea2\u9ed1\u6811\uff08\u6709\u5173\u7ea2\u9ed1\u6811\u8bf7\u67e5\u770b<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/37470948\">\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff08\u5341\u4e00\uff09\uff1a\u7ea2\u9ed1\u6811<\/a>\uff09\u7684\u65b9\u5f0f\uff0c\u4e5f\u662f\u975e\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u5f53\u67d0\u4e2a\u4f4d\u6876\u7684\u94fe\u8868\u7684\u957f\u5ea6\u8fbe\u5230\u67d0\u4e2a\u9600\u503c\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u94fe\u8868\u5c31\u5c06\u8f6c\u6362\u6210\u7ea2\u9ed1\u6811\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-50.png\" alt=\"\" class=\"wp-image-11521\" width=\"205\" height=\"234\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-50.png 688w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-50-263x300.png 263w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-50-230x263.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-50-350x400.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-50-480x548.png 480w\" sizes=\"(max-width: 205px) 100vw, 205px\" \/><\/figure><\/div>\n\n\n\n<p>JDK8\u4e2d\uff0c\u5f53\u540c\u4e00\u4e2ahash\u503c\u7684\u8282\u70b9\u6570\u5927\u4e8e8\u65f6\uff0c\u5c06\u4e0d\u518d\u4ee5\u5355\u94fe\u8868\u7684\u5f62\u5f0f\u5b58\u50a8\u4e86\uff0c\u4f1a\u88ab\u8c03\u6574\u6210\u4e00\u9897\u7ea2\u9ed1\u6811\uff08\u4e0a\u56fe\u4e2dnull\u8282\u70b9\u6ca1\u753b\uff09\u3002\u8fd9\u5c31\u662fJDK7\u4e0eJDK8\u4e2dHashMap\u5b9e\u73b0\u7684\u6700\u5927\u533a\u522b\u3002<\/p>\n\n\n\n<p><strong>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u6765\u770b\u4e0bJDK8\u4e2dHashMap\u7684\u6e90\u7801\u5b9e\u73b0<\/strong>\u3002<\/p>\n\n\n\n<p>JDK\u4e2dEntry\u7684\u540d\u5b57\u53d8\u6210\u4e86Node\uff0c\u539f\u56e0\u662f\u548c\u7ea2\u9ed1\u6811\u7684\u5b9e\u73b0TreeNode\u76f8\u5173\u8054\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">transient Node&lt;K,V>[] table;<\/pre>\n\n\n\n<p>\u5f53\u51b2\u7a81\u8282\u70b9\u6570\u5927\u4e8e8-1\u65f6\uff0c\u8f6c\u6362\u6210\u7ea2\u9ed1\u6811\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">static final int TREEIFY_THRESHOLD = 8;<\/pre>\n\n\n\n<p>\u4ee5put\u65b9\u6cd5\u5728JDK8\u4e2d\u6709\u4e86\u5f88\u5927\u7684\u6539\u53d8<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public V put(K key, V value) {\n        return putVal(hash(key), key, value, false, true);\n }\n\n\nfinal V putVal(int hash, K key, V value, boolean onlyIfAbsent,\n                   boolean evict) {\n        Node&lt;K,V>[] tab;\n\tNode&lt;K,V> p; \n\tint n, i;\n\t\/\/\u5982\u679c\u5f53\u524dmap\u4e2d\u65e0\u6570\u636e\uff0c\u6267\u884cresize\u65b9\u6cd5\u3002\u5e76\u4e14\u8fd4\u56den\n        if ((tab = table) == null || (n = tab.length) == 0)\n            n = (tab = resize()).length;\n\t \/\/\u5982\u679c\u8981\u63d2\u5165\u7684\u952e\u503c\u5bf9\u8981\u5b58\u653e\u7684\u8fd9\u4e2a\u4f4d\u7f6e\u521a\u597d\u6ca1\u6709\u5143\u7d20\uff0c\u90a3\u4e48\u628a\u4ed6\u5c01\u88c5\u6210Node\u5bf9\u8c61\uff0c\u653e\u5728\u8fd9\u4e2a\u4f4d\u7f6e\u4e0a\u5c31\u5b8c\u4e8b\u4e86\n        if ((p = tab[i = (n - 1) &amp; hash]) == null)\n            tab[i] = newNode(hash, key, value, null);\n\t\/\/\u5426\u5219\u7684\u8bdd\uff0c\u8bf4\u660e\u8fd9\u4e0a\u9762\u6709\u5143\u7d20\n        else {\n            Node&lt;K,V> e; K k;\n\t    \/\/\u5982\u679c\u8fd9\u4e2a\u5143\u7d20\u7684key\u4e0e\u8981\u63d2\u5165\u7684\u4e00\u6837\uff0c\u90a3\u4e48\u5c31\u66ff\u6362\u4e00\u4e0b\uff0c\u4e5f\u5b8c\u4e8b\u3002\n            if (p.hash == hash &amp;&amp;\n                ((k = p.key) == key || (key != null &amp;&amp; key.equals(k))))\n                e = p;\n\t    \/\/1.\u5982\u679c\u5f53\u524d\u8282\u70b9\u662fTreeNode\u7c7b\u578b\u7684\u6570\u636e\uff0c\u6267\u884cputTreeVal\u65b9\u6cd5\n            else if (p instanceof TreeNode)\n                e = ((TreeNode&lt;K,V>)p).putTreeVal(this, tab, hash, key, value);\n            else {\n\t\t\/\/\u8fd8\u662f\u904d\u5386\u8fd9\u6761\u94fe\u5b50\u4e0a\u7684\u6570\u636e\uff0c\u8ddfjdk7\u6ca1\u4ec0\u4e48\u533a\u522b\n                for (int binCount = 0; ; ++binCount) {\n                    if ((e = p.next) == null) {\n                        p.next = newNode(hash, key, value, null);\n\t\t\t\/\/2.\u5b8c\u6210\u4e86\u64cd\u4f5c\u540e\u591a\u505a\u4e86\u4e00\u4ef6\u4e8b\u60c5\uff0c\u5224\u65ad\uff0c\u5e76\u4e14\u53ef\u80fd\u6267\u884ctreeifyBin\u65b9\u6cd5\n                        if (binCount >= TREEIFY_THRESHOLD - 1) \/\/ -1 for 1st\n                            treeifyBin(tab, hash);\n                        break;\n                    }\n                    if (e.hash == hash &amp;&amp;\n                        ((k = e.key) == key || (key != null &amp;&amp; key.equals(k))))\n                        break;\n                    p = e;\n                }\n            }\n            if (e != null) { \/\/ existing mapping for key\n                V oldValue = e.value;\n                if (!onlyIfAbsent || oldValue == null) \/\/true || --\n                    e.value = value;\n\t\t   \/\/3.\n                afterNodeAccess(e);\n                return oldValue;\n            }\n        }\n        ++modCount;\n\t\/\/\u5224\u65ad\u9608\u503c\uff0c\u51b3\u5b9a\u662f\u5426\u6269\u5bb9\n        if (++size > threshold)\n            resize();\n\t    \/\/4.\n        afterNodeInsertion(evict);\n        return null;\n    }<\/pre>\n\n\n\n<p>treeifyBin()\u5c31\u662f\u5c06\u94fe\u8868\u8f6c\u6362\u6210\u7ea2\u9ed1\u6811\u3002<\/p>\n\n\n\n<p>\u4e4b\u524d\u7684indefFor()\u65b9\u6cd5\u6d88\u5931 \u4e86\uff0c\u76f4\u63a5\u7528(tab.length-1)&amp;hash\uff0c\u6240\u4ee5\u770b\u5230\u8fd9\u4e2a\uff0c\u4ee3\u8868\u7684\u5c31\u662f\u6570\u7ec4\u7684\u4e0b\u89d2\u6807\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">static final int hash(Object key) {\n        int h;\n        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);\n    }<\/pre>\n\n\n\n<p><strong>\u4e0b\u9762\u5f00\u59cb\u6b63\u9898\uff1a\u4e3a\u4ec0\u4e48\u8bf4HashMap\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u5462\uff1f\u5b83\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u60c5\u51b5\u5462\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>1. resize\u6b7b\u5faa\u73af<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u90fd\u77e5\u9053HashMap\u521d\u59cb\u5bb9\u91cf\u5927\u5c0f\u4e3a16,\u4e00\u822c\u6765\u8bf4\uff0c\u5f53\u6709\u6570\u636e\u8981\u63d2\u5165\u65f6\uff0c\u90fd\u4f1a\u68c0\u67e5\u5bb9\u91cf\u6709\u6ca1\u6709\u8d85\u8fc7\u8bbe\u5b9a\u7684thredhold\uff0c\u5982\u679c\u8d85\u8fc7\uff0c\u9700\u8981\u589e\u5927Hash\u8868\u7684\u5c3a\u5bf8\uff0c\u4f46\u662f\u8fd9\u6837\u4e00\u6765\uff0c\u6574\u4e2aHash\u8868\u91cc\u7684\u5143\u7d20\u90fd\u9700\u8981\u88ab\u91cd\u7b97\u4e00\u904d\u3002\u8fd9\u53ebrehash\uff0c\u8fd9\u4e2a\u6210\u672c\u76f8\u5f53\u7684\u5927\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void resize(int newCapacity) {\n        Entry[] oldTable = table;\n        int oldCapacity = oldTable.length;\n        if (oldCapacity == MAXIMUM_CAPACITY) {\n            threshold = Integer.MAX_VALUE;\n            return;\n        }\n \n        Entry[] newTable = new Entry[newCapacity];\n        transfer(newTable, initHashSeedAsNeeded(newCapacity));\n        table = newTable;\n        threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);\n}\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void transfer(Entry[] newTable, boolean rehash) {\n        int newCapacity = newTable.length;\n        for (Entry&lt;K,V> e : table) {\n            while(null != e) {\n                Entry&lt;K,V> next = e.next;\n                if (rehash) {\n                    e.hash = null == e.key ? 0 : hash(e.key);\n                }\n                int i = indexFor(e.hash, newCapacity);\n                e.next = newTable[i];\n                newTable[i] = e;\n                e = next;\n            }\n        }\n}<\/pre>\n\n\n\n<p>\u5927\u6982\u770b\u4e0btransfer\uff1a<\/p>\n\n\n\n<ol><li>\u5bf9\u7d22\u5f15\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u904d\u5386<\/li><li>\u5bf9\u94fe\u8868\u4e0a\u7684\u6bcf\u4e00\u4e2a\u8282\u70b9\u904d\u5386\uff1a\u7528 next \u53d6\u5f97\u8981\u8f6c\u79fb\u90a3\u4e2a\u5143\u7d20\u7684\u4e0b\u4e00\u4e2a\uff0c\u5c06 e \u8f6c\u79fb\u5230\u65b0 Hash \u8868\u7684\u5934\u90e8\uff0c\u4f7f\u7528\u5934\u63d2\u6cd5\u63d2\u5165\u8282\u70b9\u3002<\/li><li>\u5faa\u73af2\uff0c\u76f4\u5230\u94fe\u8868\u8282\u70b9\u5168\u90e8\u8f6c\u79fb<\/li><li>\u5faa\u73af1\uff0c\u76f4\u5230\u6240\u6709\u7d22\u5f15\u6570\u7ec4\u5168\u90e8\u8f6c\u79fb<\/li><\/ol>\n\n\n\n<p>\u7ecf\u8fc7\u8fd9\u51e0\u6b65\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u8f6c\u79fb\u7684\u65f6\u5019\u662f\u9006\u5e8f\u7684\u3002\u5047\u5982\u8f6c\u79fb\u524d\u94fe\u8868\u987a\u5e8f\u662f1->2->3\uff0c\u90a3\u4e48\u8f6c\u79fb\u540e\u5c31\u4f1a\u53d8\u62103->2->1\u3002\u8fd9\u65f6\u5019\u5c31\u6709\u70b9\u5934\u7eea\u4e86\uff0c\u6b7b\u9501\u95ee\u9898\u4e0d\u5c31\u662f\u56e0\u4e3a1->2\u7684\u540c\u65f62->1\u9020\u6210\u7684\u5417\uff1f\u6240\u4ee5\uff0cHashMap \u7684\u6b7b\u9501\u95ee\u9898\u5c31\u51fa\u5728\u8fd9\u4e2a<code>transfer()<\/code>\u51fd\u6570\u4e0a\u3002<\/p>\n\n\n\n<p><strong>1.1 \u5355\u7ebf\u7a0b rehash \u8be6\u7ec6\u6f14\u793a<\/strong><\/p>\n\n\n\n<p>\u5355\u7ebf\u7a0b\u60c5\u51b5\u4e0b\uff0crehash \u4e0d\u4f1a\u51fa\u73b0\u4efb\u4f55\u95ee\u9898\uff1a<\/p>\n\n\n\n<ul><li>\u5047\u8bbehash\u7b97\u6cd5\u5c31\u662f\u6700\u7b80\u5355\u7684 key mod table.length\uff08\u4e5f\u5c31\u662f\u6570\u7ec4\u7684\u957f\u5ea6\uff09\u3002<\/li><li>\u6700\u4e0a\u9762\u7684\u662fold hash \u8868\uff0c\u5176\u4e2d\u7684Hash\u8868\u7684 size = 2, \u6240\u4ee5 key = 3, 7, 5\uff0c\u5728 mod 2\u4ee5\u540e\u78b0\u649e\u53d1\u751f\u5728 table[1]<\/li><li>\u63a5\u4e0b\u6765\u7684\u4e09\u4e2a\u6b65\u9aa4\u662f Hash\u8868 resize \u52304\uff0c\u5e76\u5c06\u6240\u6709\u7684 <code>&lt;key,value&gt;<\/code> \u91cd\u65b0rehash\u5230\u65b0 Hash \u8868\u7684\u8fc7\u7a0b<\/li><\/ul>\n\n\n\n<p>\u5982\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-51-1024x714.png\" alt=\"\" class=\"wp-image-11524\" width=\"440\" height=\"306\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-51-1024x714.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-51-300x209.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-51-768x536.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-51-830x579.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-51-230x160.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-51-350x244.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-51-480x335.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-51.png 1204w\" sizes=\"(max-width: 440px) 100vw, 440px\" \/><\/figure><\/div>\n\n\n\n<p><strong>1.2 \u591a\u7ebf\u7a0b rehash \u8be6\u7ec6\u6f14\u793a<\/strong><\/p>\n\n\n\n<p>\u4e3a\u4e86\u601d\u8def\u66f4\u6e05\u6670\uff0c\u6211\u4eec\u53ea\u5c06\u5173\u952e\u4ee3\u7801\u5c55\u793a\u51fa\u6765<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">while(null != e) {\n    Entry&lt;K,V> next = e.next;\n    e.next = newTable[i];\n    newTable[i] = e;\n    e = next;\n}<\/pre>\n\n\n\n<ol><li><code>Entry&lt;K,V&gt; next = e.next;<\/code>\u2014\u2014\u56e0\u4e3a\u662f\u5355\u94fe\u8868\uff0c\u5982\u679c\u8981\u8f6c\u79fb\u5934\u6307\u9488\uff0c\u4e00\u5b9a\u8981\u4fdd\u5b58\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff0c\u4e0d\u7136\u8f6c\u79fb\u540e\u94fe\u8868\u5c31\u4e22\u4e86<\/li><li><code>e.next = newTable[i];<\/code>\u2014\u2014e \u8981\u63d2\u5165\u5230\u94fe\u8868\u7684\u5934\u90e8\uff0c\u6240\u4ee5\u8981\u5148\u7528 e.next \u6307\u5411\u65b0\u7684 Hash \u8868\u7b2c\u4e00\u4e2a\u5143\u7d20\uff08\u4e3a\u4ec0\u4e48\u4e0d\u52a0\u5230\u65b0\u94fe\u8868\u6700\u540e\uff1f\u56e0\u4e3a\u590d\u6742\u5ea6\u662f O\uff08N\uff09\uff09<\/li><li><code>newTable[i] = e;<\/code>\u2014\u2014\u73b0\u5728\u65b0 Hash \u8868\u7684\u5934\u6307\u9488\u4ecd\u7136\u6307\u5411 e \u6ca1\u8f6c\u79fb\u524d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u6240\u4ee5\u9700\u8981\u5c06\u65b0 Hash \u8868\u7684\u5934\u6307\u9488\u6307\u5411 e<\/li><li><code>e = next<\/code>\u2014\u2014\u8f6c\u79fb e \u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9<\/li><\/ol>\n\n\n\n<p>\u5047\u8bbe\u8fd9\u91cc\u6709\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6267\u884c\u4e86<code>put()<\/code>\u64cd\u4f5c\uff0c\u5e76\u8fdb\u5165\u4e86<code>transfer()<\/code>\u73af\u8282<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">while(null != e) {\n    Entry&lt;K,V> next = e.next; \/\/\u7ebf\u7a0b1\u6267\u884c\u5230\u8fd9\u91cc\u88ab\u8c03\u5ea6\u6302\u8d77\u4e86\n    e.next = newTable[i];\n    newTable[i] = e;\n    e = next;\n}<\/pre>\n\n\n\n<p>\u90a3\u4e48\u73b0\u5728\u7684\u72b6\u6001\u4e3a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-52-1024x767.png\" alt=\"\" class=\"wp-image-11525\" width=\"441\" height=\"330\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-52-1024x767.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-52-300x225.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-52-768x575.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-52-830x622.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-52-230x172.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-52-350x262.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-52-480x360.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-52.png 1092w\" sizes=\"(max-width: 441px) 100vw, 441px\" \/><\/figure><\/div>\n\n\n\n<p>\u4ece\u4e0a\u9762\u7684\u56fe\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u56e0\u4e3a\u7ebf\u7a0b1\u7684 e \u6307\u5411\u4e86 key(3)\uff0c\u800c next \u6307\u5411\u4e86 key(7)\uff0c\u5728\u7ebf\u7a0b2 rehash \u540e\uff0c\u5c31\u6307\u5411\u4e86\u7ebf\u7a0b2 rehash \u540e\u7684\u94fe\u8868\u3002<\/p>\n\n\n\n<p>\u7136\u540e\u7ebf\u7a0b1\u88ab\u5524\u9192\u4e86\uff1a<\/p>\n\n\n\n<ol><li>\u6267\u884ce.next = newTable[i]\uff0c\u4e8e\u662f key(3)\u7684 next \u6307\u5411\u4e86\u7ebf\u7a0b1\u7684\u65b0 Hash \u8868\uff0c\u56e0\u4e3a\u65b0 Hash \u8868\u4e3a\u7a7a\uff0c\u6240\u4ee5e.next = null\uff0c<\/li><li>\u6267\u884cnewTable[i] = e\uff0c\u6240\u4ee5\u7ebf\u7a0b1\u7684\u65b0 Hash \u8868\u7b2c\u4e00\u4e2a\u5143\u7d20\u6307\u5411\u4e86\u7ebf\u7a0b2\u65b0 Hash \u8868\u7684 key(3)\u3002\u597d\u4e86\uff0ce \u5904\u7406\u5b8c\u6bd5\u3002<\/li><li>\u6267\u884ce = next\uff0c\u5c06 e \u6307\u5411 next\uff0c\u6240\u4ee5\u65b0\u7684 e \u662f key(7)<\/li><\/ol>\n\n\n\n<p>\u7136\u540e\u8be5\u6267\u884c key(3)\u7684 next \u8282\u70b9 key(7)\u4e86:<\/p>\n\n\n\n<ol><li>\u73b0\u5728\u7684 e \u8282\u70b9\u662f key(7)\uff0c\u9996\u5148\u6267\u884c<code>Entry&lt;K,V&gt; next = e.next<\/code>,\u90a3\u4e48 next \u5c31\u662f key(3)\u4e86<\/li><li>\u6267\u884c<code>e.next = newTable[i]<\/code>\uff0c\u4e8e\u662fkey(7) \u7684 next \u5c31\u6210\u4e86 key(3)<\/li><li>\u6267\u884c<code>newTable[i] = e<\/code>\uff0c\u90a3\u4e48\u7ebf\u7a0b1\u7684\u65b0 Hash \u8868\u7b2c\u4e00\u4e2a\u5143\u7d20\u53d8\u6210\u4e86 key(7)<\/li><li>\u6267\u884c<code>e = next<\/code>\uff0c\u5c06 e \u6307\u5411 next\uff0c\u6240\u4ee5\u65b0\u7684 e \u662f key(3)<\/li><\/ol>\n\n\n\n<p>\u8fd9\u65f6\u5019\u7684\u72b6\u6001\u56fe\u4e3a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-53-1024x715.png\" alt=\"\" class=\"wp-image-11526\" width=\"424\" height=\"295\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-53-1024x715.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-53-300x209.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-53-768x536.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-53-830x580.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-53-230x161.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-53-350x244.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-53-480x335.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-53.png 1054w\" sizes=\"(max-width: 424px) 100vw, 424px\" \/><\/figure><\/div>\n\n\n\n<p>\u7136\u540e\u53c8\u8be5\u6267\u884c key(7)\u7684 next \u8282\u70b9 key(3)\u4e86\uff1a<\/p>\n\n\n\n<ol><li>\u73b0\u5728\u7684 e \u8282\u70b9\u662f key(3)\uff0c\u9996\u5148\u6267\u884c<code>Entry&lt;K,V&gt; next = e.next<\/code>,\u90a3\u4e48 next \u5c31\u662f null<\/li><li>\u6267\u884c<code>e.next = newTable[i]<\/code>\uff0c\u4e8e\u662fkey(3) \u7684 next \u5c31\u6210\u4e86 key(7)<\/li><li>\u6267\u884c<code>newTable[i] = e<\/code>\uff0c\u90a3\u4e48\u7ebf\u7a0b1\u7684\u65b0 Hash \u8868\u7b2c\u4e00\u4e2a\u5143\u7d20\u53d8\u6210\u4e86 key(3)<\/li><li>\u6267\u884c<code>e = next<\/code>\uff0c\u5c06 e \u6307\u5411 next\uff0c\u6240\u4ee5\u65b0\u7684 e \u662f key(7)<\/li><\/ol>\n\n\n\n<p>\u8fd9\u65f6\u5019\u7684\u72b6\u6001\u5982\u56fe\u6240\u793a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-54-1024x701.png\" alt=\"\" class=\"wp-image-11527\" width=\"438\" height=\"300\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-54-1024x701.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-54-300x205.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-54-768x526.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-54-830x568.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-54-230x158.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-54-350x240.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-54-480x329.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/06\/\u56fe\u7247-54.png 1066w\" sizes=\"(max-width: 438px) 100vw, 438px\" \/><\/figure><\/div>\n\n\n\n<p>\u5f88\u660e\u663e\uff0c\u73af\u5f62\u94fe\u8868\u51fa\u73b0\u4e86\uff01\uff01\u5f53\u7136\uff0c\u73b0\u5728\u8fd8\u6ca1\u6709\u4e8b\u60c5\uff0c\u56e0\u4e3a\u4e0b\u4e00\u4e2a\u8282\u70b9\u662f null\uff0c\u6240\u4ee5<code>transfer()<\/code>\u5c31\u5b8c\u6210\u4e86\uff0c\u7b49<code>put()<\/code>\u7684\u5176\u4f59\u8fc7\u7a0b\u641e\u5b9a\u540e\uff0cHashMap \u7684\u5e95\u5c42\u5b9e\u73b0\u5c31\u662f\u7ebf\u7a0b1\u7684\u65b0 Hash \u8868\u4e86\u3002<\/p>\n\n\n\n<p><strong>\u666f\u8c61\u8fd8\u539f<\/strong><\/p>\n\n\n\n<p>\u5047\u8bbe\u73b0\u5728\u6709\u4e09\u4e2a\u7ebf\u7a0b\uff1aT1,T2,T3\uff1b\u5728\u8001\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u6570\u636e\u4e5f\u5c31\u662fe\u5f15\u7528\u7684\u5bf9\u8c61\uff0c\u6211\u4eec\u79f0\u5b83\u4e3aA,\u5728\u65b0\u6570\u7ec4\u4e2d\u7684\u5934\u4e24\u4e2a\u6570\u636e\u5206\u522b\u4e3aB\u548cC\uff0cB.next=C\u3002<\/p>\n\n\n\n<p>\u7ebf\u7a0bT1\u8fd0\u884c\u5230e.next=newTable[i]\u65f6\uff0c\u7ebf\u7a0bT2\u8fd0\u884c\u5230next=e.next;\u7136\u540e\u7ebf\u7a0bt1\u53bb\u7ee7\u7eed\u8fd0\u884c\uff0c\u4f1a\u4ea7\u751f\u4ec0\u4e48\u6548\u679c\u5462\uff1fA.next=B\uff0cB.next=C\u3002e\u6307\u5411\u7684\u5bf9\u8c61\u662fB,newTable[i]=A\u3002\u7136\u540e\u7ee7\u7eed\u8fd0\u884c\uff0ce.next=newTable[i],\u4e5f\u5c31\u662fB.next=A;\u540c\u65f6A.next=B\uff0c\u7ee7\u7eed\u8fd0\u884cnewTable[i] = e,e = next;\u5982\u679c\u6ca1\u6709\u5176\u5b83\u7ebf\u7a0b\u6363\u4e71\u7684\u8bdd\uff0c\u90a3\u4e48\u6b64\u65f6e\u5e94\u8be5\u662fC\u554a\uff0c\u53ef\u60dc\u53ea\u662f\u5982\u679c\uff0c\u5982\u679c\u6709\u7b2c\u4e09\u4e2a\u7ebf\u7a0bT3\u5728\u7ebf\u7a0bT1\u6267\u884ce.next = newTable[i]\u7684\u65f6\u5019\u53bb\u6267\u884cnext = e.next;\u90a3\u4e48\u5c31\u4e2d\u9014\u6539\u53d8\u4e86next\u7684\u503c\uff0c\u672c\u6765\u662f\u4fdd\u5b58C\u7684\uff0c\u4f46\u662f\u73b0\u5728\u6210\u4e86A\u4e86\u3002\u603b\u7ed3\u4e0b\u73b0\u5728\u7684\u60c5\u51b5\uff1ae\u5f15\u7528A\uff0cnextTable[i]\u5f15\u7528B\uff0cA.next=B,B.next=A\u3002\u73b0\u5728\u660e\u767d\u4e86\u5427\u3002\u4f1a\u4e00\u76f4\u8fd9\u4e48\u6b7b\u9501\u4e0b\u53bb\u7684\u3002<strong>\uff08\u8fd9\u91cc\u9762\u786e\u5b9e\u6709\u70b9\u7ed5\uff0c\u4e0d\u8fc7\u5927\u5bb6\u591a\u8bfb\u51e0\u904d\u5c31\u80fd\u7406\u89e3\u4e86^_^\uff09<\/strong><\/p>\n\n\n\n<p>\u987a\u4fbf\u518d\u8bb0\u5f55\u4e00\u4e2aHashMap\u7684\u95ee\u9898\uff1a<\/p>\n\n\n\n<p><strong>\u4e3a\u4ec0\u4e48String, Interger\u8fd9\u6837\u7684wrapper\u7c7b\u9002\u5408\u4f5c\u4e3a\u952e\uff1f<\/strong><\/p>\n\n\n\n<p>String, Interger\u8fd9\u6837\u7684wrapper\u7c7b\u4f5c\u4e3aHashMap\u7684\u952e\u662f\u518d\u9002\u5408\u4e0d\u8fc7\u4e86\uff0c\u800c\u4e14String\u6700\u4e3a\u5e38\u7528\u3002\u56e0\u4e3aString\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4e5f\u662ffinal\u7684\uff0c\u800c\u4e14\u5df2\u7ecf\u91cd\u5199\u4e86equals()\u548chashCode()\u65b9\u6cd5\u4e86\u3002\u5176\u4ed6\u7684wrapper\u7c7b\u4e5f\u6709\u8fd9\u4e2a\u7279\u70b9\u3002\u4e0d\u53ef\u53d8\u6027\u662f\u5fc5\u8981\u7684\uff0c\u56e0\u4e3a\u4e3a\u4e86\u8981\u8ba1\u7b97hashCode()\uff0c\u5c31\u8981\u9632\u6b62\u952e\u503c\u6539\u53d8\uff0c\u5982\u679c\u952e\u503c\u5728\u653e\u5165\u65f6\u548c\u83b7\u53d6\u65f6\u8fd4\u56de\u4e0d\u540c\u7684hashcode\u7684\u8bdd\uff0c\u90a3\u4e48\u5c31\u4e0d\u80fd\u4eceHashMap\u4e2d\u627e\u5230\u4f60\u60f3\u8981\u7684\u5bf9\u8c61\u3002\u4e0d\u53ef\u53d8\u6027\u8fd8\u6709\u5176\u4ed6\u7684\u4f18\u70b9\u5982\u7ebf\u7a0b\u5b89\u5168\u3002\u5982\u679c\u4f60\u53ef\u4ee5\u4ec5\u4ec5\u901a\u8fc7\u5c06\u67d0\u4e2afield\u58f0\u660e\u6210final\u5c31\u80fd\u4fdd\u8bc1hashCode\u662f\u4e0d\u53d8\u7684\uff0c\u90a3\u4e48\u8bf7\u8fd9\u4e48\u505a\u5427\u3002\u56e0\u4e3a\u83b7\u53d6\u5bf9\u8c61\u7684\u65f6\u5019\u8981\u7528\u5230equals()\u548chashCode()\u65b9\u6cd5\uff0c\u90a3\u4e48\u952e\u5bf9\u8c61\u6b63\u786e\u7684\u91cd\u5199\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5982\u679c\u4e24\u4e2a\u4e0d\u76f8\u7b49\u7684\u5bf9\u8c61\u8fd4\u56de\u4e0d\u540c\u7684hashcode\u7684\u8bdd\uff0c\u90a3\u4e48\u78b0\u649e\u7684\u51e0\u7387\u5c31\u4f1a\u5c0f\u4e9b\uff0c\u8fd9\u6837\u5c31\u80fd\u63d0\u9ad8HashMap\u7684\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u90fd\u77e5\u9053HasnMap\u5728\u591a\u7ebf\u7a0b\u7684\u573a\u666f\u4e2d\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u4e3a\u4ec0\u4e48\u5462\uff1f\u9996\u5148\u8981\u4ece\u5b83\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u8bf4\u8d77\u2026\u2026 JDK7\u4e2d\u7684H [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/11518"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11518"}],"version-history":[{"count":4,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/11518\/revisions"}],"predecessor-version":[{"id":11528,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/11518\/revisions\/11528"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}