{"id":345,"date":"2025-07-01T14:57:58","date_gmt":"2025-07-01T06:57:58","guid":{"rendered":"http:\/\/getzs.online\/?p=345"},"modified":"2025-07-01T15:12:03","modified_gmt":"2025-07-01T07:12:03","slug":"hashmap","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2025\/07\/01\/hashmap\/","title":{"rendered":"HashMap\u7b80\u4ecb"},"content":{"rendered":"\n<p>map\u662f\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9(&lt;key, value>)\u7684\u96c6\u5408\u7c7b\uff0c\u4e5f\u5c31\u662f\u662f\u4e00\u7ec4\u952e\u503c\u5bf9\u7684\u6620\u5c04\uff08\u6570\u5b66\u6982\u5ff5\uff09\u3002\u5728java\u4e2dmap\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u662f\u548ccollection\u63a5\u53e3\u540c\u4e00\u7b49\u7ea7\u7684\u96c6\u5408\u6839\u63a5\u53e3\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u521d\u59cb\u53c2\u6570<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>        \/\/HashMap\u7684\u9ed8\u8ba4\u521d\u59cb\u957f\u5ea616\n\tstatic final int DEFAULT_INITIAL_CAPACITY = 1 &lt;&lt; 4; \n\t\n\t\/\/HashMap\u7684\u6700\u5927\u957f\u5ea62\u768430\u6b21\u5e42\n\tstatic final int MAXIMUM_CAPACITY = 1 &lt;&lt; 30;\n\t\n\t\/\/HashMap\u7684\u9ed8\u8ba4\u52a0\u8f7d\u56e0\u5b500.75\n\tstatic final float DEFAULT_LOAD_FACTOR = 0.75f;\n\t\n\t\/\/HashMap\u94fe\u8868\u5347\u7ea7\u6210\u7ea2\u9ed1\u6811\u7684\u4e34\u754c\u503c\n\tstatic final int TREEIFY_THRESHOLD = 8;\n\t\n\t\/\/HashMap\u7ea2\u9ed1\u6811\u9000\u5316\u6210\u94fe\u8868\u7684\u4e34\u754c\u503c\n\tstatic final int UNTREEIFY_THRESHOLD = 6;\n\t\n\t\/\/HashMap\u94fe\u8868\u5347\u7ea7\u6210\u7ea2\u9ed1\u6811\u7b2c\u4e8c\u4e2a\u6761\u4ef6\uff1aHashMap\u6570\u7ec4(\u6876)\u7684\u957f\u5ea6\u5927\u4e8e\u7b49\u4e8e64\n\tstatic final int MIN_TREEIFY_CAPACITY = 64;\n\t\n\t\/\/HashMap\u5e95\u5c42Node\u6876\u7684\u6570\u7ec4\n\ttransient Node&lt;K,V>&#91;] table;\n\t\n\t\/\/\u6269\u5bb9\u9608\u503c\uff0c\u5f53\u4f60\u7684hashmap\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\u8d85\u8fc7\u8fd9\u4e2a\u9608\u503c\uff0c\u4fbf\u4f1a\u53d1\u751f\u6269\u5bb9\n\t\/\/threshold = capacity * loadFactor\n\tint threshold;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u6570\u636e\u7ed3\u6784<\/h2>\n\n\n\n<p>HashMap\u5e95\u5c42\u662f\u7531\u6570\u7ec4\uff08Node&lt;K, V>[] table\uff09+ \u94fe\u8868 + \u7ea2\u9ed1\u6811 \u7ec4\u6210\u3002<\/p>\n\n\n\n<p>\u7531hash(Object key)\u8ba1\u7b97key\u7684\u54c8\u5e0c\u503c\uff0c\u901a\u8fc7\u8ba1\u7b97\u51fa\u7684\u54c8\u5e0c\u503c\u5b58\u50a8\u5230\u5bf9\u5e94\u5230\u76f8\u5e94\u7684\u6570\u7ec4\u4e2d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u65b0\u589e\u673a\u5236<\/h2>\n\n\n\n<p>\uff081\uff09\u5224\u65ad\u6570\u7ec4\u662f\u5426\u4e3a\u7a7a\u6216\u957f\u5ea6\u4e3a0\uff0c\u505a\u6269\u5bb9\u5904\u7406<\/p>\n\n\n\n<p>\uff082\uff09\u8ba1\u7b97hash\u503c\uff0c\u63d2\u5165\u5bf9\u5e94\u7684\u6570\u7ec4\u7d22\u5f15\u4e2d\uff0c\u5982\u679c\u5bf9\u5e94\u6570\u7ec4\u7d22\u5f15\u4e3anull\uff0c\u5219\u76f4\u63a5\u521b\u5efanode\u8282\u70b9\uff0c\u53cd\u4e4b\u4f9d\u6b21\u5224\u65ad\uff1a<\/p>\n\n\n\n<p>1\uff09\u5f53\u524d\u8282\u70b9\u548chash\u548ckey\u4e0e\u63d2\u5165\u7684key\u76f8\u540c\uff0c\u5219\u76f4\u63a5\u66ff\u6362value\u503c<\/p>\n\n\n\n<p>2\uff09\u5f53\u524d\u8282\u70b9\u4e3a\u6811\u5f62\u7ed3\u6784TreeNode\uff0c\u76f4\u63a5\u4f7f\u7528putTreeVal\u51fd\u6570\u63d2\u5165<\/p>\n\n\n\n<p>3\uff09\u5f53\u524d\u8282\u70b9\u4e3a\u94fe\u8868\uff0c\u4f9d\u6b21\u5faa\u73af\u5224\u65ad\uff0c\u6bcf\u4e2a\u8282\u70b9\u662f\u5426\u548c\u63d2\u5165\u7684key\u76f8\u540c\uff0c\u76f8\u540c\u5219\u66ff\u6362<\/p>\n\n\n\n<p>\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u94fe\u8868\u8282\u70b9\u5927\u4e8e7\uff08\u9608\u503c8\uff09\u7684\u65f6\u5019\u5bf9\u94fe\u8868\u5347\u7ea7\u4e3a\u7ea2\u9ed1\u6811\uff0c\u4fbf\u4e8e\u63d2\u5165\u67e5\u8be2\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public V put(K key, V value) {\n        \/\/ \u8c03\u7528puVal\u65b9\u6cd5\uff0c\u6dfb\u52a0\u5143\u7d20\n        return this.putVal(hash(key), key, value, false, true);\n    }\n\n    final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {\n        Node&#91;] tab;\n        int n;\n        \/\/ \u5982\u679c\u6876\u6570\u7ec4\u4e3a\u7a7a\uff0c\u5219\u8fdb\u884c\u6269\u5bb9\u64cd\u4f5c\n        if ((tab = this.table) == null || (n = tab.length) == 0) {\n            n = (tab = this.resize()).length;\n        }\n\n        Object p;\n        int i;\n        \/\/ \u8ba1\u7b97hash\u503c\uff0c\u5224\u65ad\u6876\u6570\u7ec4\u4e2d\u8be5\u4f4d\u7f6e\u7684\u5143\u7d20\u662f\u5426\u4e3a\u7a7a\uff0c \u5982\u679c\u4e3a\u7a7a\u76f4\u63a5\u63d2\u5165\u65b0\u589e\u63d2\u5165node\u8282\u70b9\n        if ((p = tab&#91;i = n - 1 &amp; hash]) == null) {\n            tab&#91;i] = this.newNode(hash, key, value, (Node)null);\n        } else {\n            Object e;\n            Object k;\n            \/\/ \u5224\u65ad\u5f53\u524d\u8282\u70b9\u7684key\u662f\u5426\u548c\u63d2\u5165\u7684key\u4e00\u81f4\uff0c \u5982\u679c\u4e00\u81f4\u5219\u76f4\u63a5\u66ff\u6362\n            if (((Node)p).hash == hash &amp;&amp; ((k = ((Node)p).key) == key || key != null &amp;&amp; key.equals(k))) {\n                e = p;\n            }\n            \/\/ \u5224\u65ad\u5f53\u524d\u8282\u70b9\u662f\u5426\u662f\u6811\u8282\u70b9\uff0c\u5982\u679c\u662f\u6811\u8282\u70b9\u5219\u8c03\u7528putTreeVal\u65b9\u6cd5\u63d2\u5165\u8282\u70b9\n            else if (p instanceof TreeNode) {\n                e = ((TreeNode)p).putTreeVal(this, tab, hash, key, value);\n            } \n            \/\/ \u5426\u5219\u5219\u904d\u5386\u94fe\u8868\uff0c\u5224\u65adkey\u662f\u5426\u4e00\u81f4\uff0c\u4e00\u81f4\u5219\u66ff\u6362\uff0c\u4e0d\u4e00\u81f4\u5219\u63d2\u5165\n            else {\n                int binCount = 0;\n\n                while(true) {\n                    if ((e = ((Node)p).next) == null) {\n                        ((Node)p).next = this.newNode(hash, key, value, (Node)null);\n                        \/\/ \u94fe\u8868\u8282\u70b9\u957f\u5ea6\u5927\u4e8e7\u5219\u8fdb\u5316\u4e3a\u7ea2\u9ed1\u6811\n                        if (binCount >= 7) {\n                            this.treeifyBin(tab, hash);\n                        }\n                        break;\n                    }\n\n                    if (((Node)e).hash == hash &amp;&amp; ((k = ((Node)e).key) == key || key != null &amp;&amp; key.equals(k))) {\n                        break;\n                    }\n\n                    p = e;\n                    ++binCount;\n                }\n            }\n\n            \/\/ \u5224\u65ade\u662f\u5426\u4e3a\u7a7a\uff0c\u4e0d\u4e3a\u7a7a\u5219\u8fd4\u56de\u65e7\u503c\n            if (e != null) {\n                V oldValue = ((Node)e).value;\n                if (!onlyIfAbsent || oldValue == null) {\n                    ((Node)e).value = value;\n                }\n\n                \/\/ \u8c03\u7528afterNodeAccess\u65b9\u6cd5\uff0c\u5c06\u8282\u70b9\u79fb\u52a8\u5230\u94fe\u8868\u5c3e\u90e8\n                this.afterNodeAccess((Node)e);\n                return oldValue;\n            }\n        }\n\n        ++this.modCount;\n        \/\/ \u5224\u65ad\u662f\u5426\u6269\u5bb9\n        if (++this.size > this.threshold) {\n            this.resize();\n        }\n\n        this.afterNodeInsertion(evict);\n        return null;\n    }<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"530\" src=\"http:\/\/hello.getzs.online\/wp-content\/uploads\/2025\/07\/image-1024x530.png\" alt=\"\" class=\"wp-image-346\" srcset=\"https:\/\/hello.getzs.online\/wp-content\/uploads\/2025\/07\/image-1024x530.png 1024w, https:\/\/hello.getzs.online\/wp-content\/uploads\/2025\/07\/image-300x155.png 300w, https:\/\/hello.getzs.online\/wp-content\/uploads\/2025\/07\/image-768x397.png 768w, https:\/\/hello.getzs.online\/wp-content\/uploads\/2025\/07\/image-1536x795.png 1536w, https:\/\/hello.getzs.online\/wp-content\/uploads\/2025\/07\/image.png 1571w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u6269\u5bb9\u673a\u5236<\/h2>\n\n\n\n<p>\u5f53\u63d2\u5165\u6570\u91cf\u5927\u4e8e\u9608\u503c\u65f6\uff08\u6570\u7ec4\u957f\u5ea6 * \u8d1f\u8f7d\u56e0\u5b50\uff09<\/p>\n\n\n\n<p>table\u5bb9\u91cf\u53d8\u4e3a\u4e24\u500d\uff08size &lt;&lt; 1\uff09,\u91cd\u65b0\u8ba1\u7b97\u6bcf\u4e2akey\u7684hash\u503c\u8fdb\u884c\u63d2\u5165<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u590d\u6742\u5ea6<\/h2>\n\n\n\n<p>\u5e73\u5747\u60c5\u51b5\u4e0b\u63d2\u5165\u3001\u67e5\u627e\u3001\u5220\u9664\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)<\/p>\n\n\n\n<p>\u6700\u7cdf\u7cd5\u60c5\u51b5\u4e0b\u63d2\u5165\u3001\u67e5\u627e\u3001\u5220\u9664\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\u6216\u8005O(logn)<\/p>\n\n\n\n<p>\u539f\u56e0\uff1a\u7531\u4e8e\u4e0d\u540c\u7684key\u53ef\u80fd\u6709\u76f8\u540c\u7684\u54c8\u5e0c\u503c\uff0c\u5bfc\u81f4\u90fd\u63d2\u5165\u5230\u540c\u4e00\u4e2a\u6570\u7ec4\u7d22\u5f15\u4e0a\uff0c\u4f7f\u94fe\u8868\uff08\u7ea2\u9ed1\u6811\uff09\u4e2a\u6570\u4e3an\uff0c\u63d2\u5165\u3001\u67e5\u627e\u3001\u5220\u9664\u6548\u7387\u964d\u4f4e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u94fe\u8868\u548c\u7ea2\u9ed1\u6811\u4e92\u8f6c\u6d41\u7a0b\uff08\u9762\u8bd5\u7a81\u51fb\u622a\u53d6\uff09<\/h2>\n\n\n\n<p>\uff081\uff09\u94fe\u8868\u5347\u7ea7\u4e3a\u7ea2\u9ed1\u6811<\/p>\n\n\n\n<p>\u5728 JDK 1.8 \u4e4b\u540e\uff0cHashMap \u9ed8\u8ba4\u662f\u5148\u4f7f\u7528\u6570\u7ec4 + \u94fe\u8868\u5b58\u50a8\u6570\u636e\uff0c\u4f46\u5f53\u6ee1\u8db3\u4ee5\u4e0b\u4e24\u4e2a\u6761\u4ef6\u65f6\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u94fe\u8868\u7684\u6570\u91cf\u5927\u4e8e\u9608\u503c\uff08\u9ed8\u8ba4\u662f 8\uff09<\/li>\n\n\n\n<li>\u5e76\u4e14\u6570\u7ec4\u957f\u5ea6\u5927\u4e8e 64 \u65f6<\/li>\n<\/ol>\n\n\n\n<p>\u4e3a\u4e86\uff08\u67e5\u8be2\uff09\u7684\u6027\u80fd\u8003\u8651\u4f1a\u5c06\u94fe\u8868\u5347\u7ea7\u4e3a\u7ea2\u9ed1\u6811\u8fdb\u884c\u5b58\u50a8\uff0c\u5177\u4f53\u6267\u884c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u521b\u5efa\u65b0\u7684\u7ea2\u9ed1\u6811\u5bf9\u8c61\uff0c\u5e76\u5c06\u94fe\u8868\u5185\u6240\u6709\u7684\u952e\u503c\u5bf9\u5168\u90e8\u6dfb\u52a0\u5230\u7ea2\u9ed1\u6811\u4e2d\u3002<\/li>\n\n\n\n<li>\u5c06\u539f\u6765\u7684\u94fe\u8868\u5f15\u7528\u6307\u5411\u65b0\u521b\u5efa\u7684\u7ea2\u9ed1\u6811\u3002<\/li>\n<\/ol>\n\n\n\n<p>\uff082\uff09\u7ea2\u9ed1\u6811\u9000\u5316\u4e3a\u94fe\u8868<\/p>\n\n\n\n<p>\u5f53\u8fdb\u884c\u4e86\u5220\u9664\u64cd\u4f5c\uff0c\u5bfc\u81f4\u7ea2\u9ed1\u6811\u7684\u8282\u70b9\u5c0f\u4e8e\u7b49\u4e8e 6 \u65f6\uff0c\u4f1a\u53d1\u751f\u9000\u5316\uff0c\u5c06\u7ea2\u9ed1\u6811\u8f6c\u6362\u4e3a\u94fe\u8868\u3002\u8fd9\u662f\u56e0\u4e3a\u5f53\u8282\u70b9\u6570\u91cf\u8f83\u5c11\u65f6\uff0c\u7ea2\u9ed1\u6811\u5bf9\u6027\u80fd\u7684\u63d0\u5347\u5e76\u4e0d\u660e\u663e\uff0c\u53cd\u800c\u5360\u7528\u4e86\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002\u5177\u4f53\u6267\u884c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4ece\u7ea2\u9ed1\u6811\u7684\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u6309\u7167\u4e2d\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u5c06\u6240\u6709\u8282\u70b9\u52a0\u5165\u5230\u4e00\u4e2a\u65b0\u7684\u94fe\u8868\u4e2d\u3002<\/li>\n\n\n\n<li>\u5c06\u539f\u6765\u7684\u7ea2\u9ed1\u6811\u5f15\u7528\u6307\u5411\u65b0\u521b\u5efa\u7684\u94fe\u8868\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>map\u662f\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9(&lt;key, value>)\u7684\u96c6\u5408\u7c7b\uff0c\u4e5f\u5c31\u662f\u662f\u4e00\u7ec4\u952e\u503c\u5bf9\u7684\u6620\u5c04\uff08\u6570\u5b66\u6982\u5ff5\uff09\u3002\u5728ja [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-345","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/comments?post=345"}],"version-history":[{"count":3,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/345\/revisions"}],"predecessor-version":[{"id":349,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/345\/revisions\/349"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}