{"id":2159,"date":"2021-06-07T21:31:45","date_gmt":"2021-06-07T13:31:45","guid":{"rendered":"https:\/\/coderbee.net\/?p=2159"},"modified":"2021-06-07T21:31:56","modified_gmt":"2021-06-07T13:31:56","slug":"hashmap","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/java\/20210607\/2159","title":{"rendered":"HashMap"},"content":{"rendered":"<p>\u672c\u6587\u6e90\u7801\u57fa\u4e8e JDK 1.8.0_101 \u3002<\/p>\n<p>\u5728 JDK 1.8 \u4e4b\u524d\uff0cHashMap \u91c7\u7528 \u69fd\u6570\u7ec4 + \u5355\u94fe\u8868 \u6765\u5b9e\u73b0\uff1b<\/p>\n<p>\u5728 JDK 1.8 \u5f00\u59cb\u91c7\u7528 \u69fd\u6570\u7ec4 + \u5355\u94fe\u8868 + \u7ea2\u9ed1\u6811 \u6765\u5b9e\u73b0\u3002<\/p>\n<h1>1. \u4e3a\u4ec0\u4e48\u5f15\u5165\u7ea2\u9ed1\u6811\uff1f<\/h1>\n<p>\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u65f6\u3001\u94fe\u8868\u8fc7\u957f\u5bfc\u81f4\u8bbf\u95ee\u6548\u7387\u4f4e\u4e0b\u7684\u95ee\u9898\u3002<\/p>\n<blockquote><p>\n  \u4e3a\u4ec0\u4e48\u662f\u7ea2\u9ed1\u6811\u4e0d\u662f\u5176\u4ed6\u6811\uff1a<br \/>\n   \u4e8c\u53c9\u6392\u5e8f\u6811\u5728\u6781\u7aef\u60c5\u51b5\u4e0b\u4f1a\u9000\u5316\u6210\u7ebf\u6027\u7ed3\u6784\u3002<br \/>\n  \u5e73\u8861\u4e8c\u53c9\u6811\uff08AVL\u6811\uff09\u662f\u4e25\u683c\u5e73\u8861\u6811\uff0c\u5728\u589e\u52a0\u6216\u5220\u9664\u8282\u70b9\u65f6\uff0c\u65cb\u8f6c\u6b21\u6570\u6bd4\u7ea2\u9ed1\u6811\u8981\u591a\u3002\u7ea2\u9ed1\u6811\u7684\u7edf\u8ba1\u6027\u80fd\u9ad8\u4e8e AVL \u6811\u3002<\/p>\n<p>  \u7ea2\u9ed1\u6811\u7279\u6027\uff0cRBT\u6811\u4e0a\u7684\u6bcf\u4e2a\u8282\u70b9\uff0c\u90fd\u8981\u9075\u5faa\u4e0b\u9762\u7684\u89c4\u5219\uff1a<br \/>\n  \u2460 \u6bcf\u4e2a\u8282\u70b9\u90fd\u662f\u7ea2\u8272\u6216\u8005\u9ed1\u8272\uff1b<br \/>\n  \u2461 \u6839\u8282\u70b9\u5fc5\u987b\u59cb\u7ec8\u662f\u9ed1\u8272\uff1b<br \/>\n  \u2462 \u6ca1\u6709\u4e24\u4e2a\u76f8\u90bb\u7684\u7ea2\u8272\u8282\u70b9\uff1b<br \/>\n  \u2463 \u5bf9\u6bcf\u4e2a\u7ed3\u70b9\uff0c\u4ece\u8be5\u7ed3\u70b9\u5230\u5176\u5b50\u5b59\u8282\u70b9\u7684\u6240\u6709\u8def\u5f84\u4e0a\u5305\u542b\u76f8\u540c\u6570\u76ee\u7684\u9ed1\u7ed3\u70b9\u3002\n<\/p><\/blockquote>\n<p><!--more--><\/p>\n<h1>2. \u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u4f7f\u7528\u7ea2\u9ed1\u6811\uff1f<\/h1>\n<ul>\n<li>\u7ea2\u9ed1\u6811\u8282\u70b9\u7684\u5185\u5b58\u5360\u7528\u5927\u7ea6\u662f\u94fe\u8868\u8282\u70b9\u7684\u4e24\u500d\uff0c\u5c3d\u91cf\u4f7f\u7528\u94fe\u8868\u53ef\u4ee5\u8282\u7ea6\u5185\u5b58\uff1b<\/li>\n<li>\u7ea2\u9ed1\u6811\u7684\u7ef4\u62a4\u6bd4\u94fe\u8868\u590d\u6742\u5f88\u591a\uff0c\u5982\u679c\u94fe\u8868\u5f88\u77ed\uff0c\u4f7f\u7528\u7ea2\u9ed1\u6811\u5bf9\u8bbf\u95ee\u6548\u7387\u4e5f\u4e0d\u4f1a\u6709\u5927\u7684\u63d0\u5347\u3002<\/li>\n<\/ul>\n<h1>3. hash \u7b97\u6cd5<\/h1>\n<pre><code class=\"java\">static final int hash(Object key) {\n    int h;\n    return (key == null) ? 0 : (h = key.hashCode()) ^ (h &gt;&gt;&gt; 16);\n}\n<\/code><\/pre>\n<p>\u8be5\u7b97\u6cd5\u57fa\u4e8e\u4e00\u4e2a\u5047\u8bbe\uff1a\u5927\u90e8\u5206\u5143\u7d20\u7684\u54c8\u5e0c\u503c\u5728\u4f4e\u4f4d\u662f\u76f8\u540c\u7684\uff0c\u901a\u8fc7\u4e0e\u9ad8\u4f4d\u8fdb\u884c\u5f02\u6216\u8fd0\u7b97\u53ef\u4ee5\u5f15\u5165\u66f4\u591a\u7684\u968f\u673a\u6027\u3002<\/p>\n<p>\u901a\u8fc7 <code>hash &amp; (table.length - 1)<\/code> \u8ba1\u7b97\u51fa\u76ee\u6807\u69fd\u4f4d\uff0c\u56e0\u4e3a\u6570\u7ec4\u7684\u957f\u5ea6\u603b\u662f 2 \u7684\u5e42\u3002<\/p>\n<h1>4. \u94fe\u8868\u4e0e\u7ea2\u9ed1\u6811\u4e4b\u95f4\u7684\u8f6c\u6362\u673a\u5236<\/h1>\n<ul>\n<li>\u5f53\u94fe\u8868\u7684\u957f\u5ea6\u8fbe\u5230 TREEIFY_THRESHOLD\uff08\u9ed8\u8ba4\u503c 8\uff09\u3001\u4e14\u69fd\u6570\u7ec4\u7684\u957f\u5ea6 \u5927\u4e8e\u7b49\u4e8e MIN_TREEIFY_CAPACITY(\u9ed8\u8ba4\u503c 64) \u65f6\uff0c\u6269\u5bb9\u65f6\u628a\u94fe\u8868\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\uff1b<\/li>\n<li>\u5f53\u7ea2\u9ed1\u6811\u4e2d\u7684\u8282\u70b9\u7531\u4e8e\u79fb\u9664\u6216 resize \u51cf\u5c11\u81f3 UNTREEIFY_THRESHOLD(\u9ed8\u8ba4\u503c 6)\uff0c\u53d8\u56de\u666e\u901a\u94fe\u8868\u3002<br \/>\n> \u5728\u7406\u60f3\u60c5\u51b5\u4e0b\uff0chashCode \u5206\u5e03\u826f\u597d\uff0c\u94fe\u8868\u957f\u5ea6\u7b26\u5408\u6cca\u677e\u5206\u5e03\uff0c\u5404\u4e2a\u957f\u5ea6\u7684\u547d\u4e2d\u7387\u4f9d\u6b21\u9012\u51cf\uff0c\u5f53\u957f\u5ea6\u4e3a 8 \u65f6\uff0c\u6982\u7387\u5c0f\u4e8e\u5343\u4e07\u5206\u4e4b\u4e00\uff0c\u901a\u5e38 Map \u91cc\u4e0d\u4f1a\u5b58\u8fd9\u4e48\u591a\u6570\u636e\uff0c\u4e5f\u5c31\u4e0d\u4f1a\u53d1\u751f\u94fe\u8868\u5230\u7ea2\u9ed1\u6811\u7684\u8f6c\u6362\u3002<br \/>\n> UNTREEIFY_THRESHOLD \u9ed8\u8ba4\u53d6 6 \u662f\u56e0\u4e3a\u957f\u5ea6\u9002\u4e2d\uff0c\u53d6 7 \u5219\u4f1a\u9891\u7e41\u5730\u53d1\u751f\u7ea2\u9ed1\u6811\u8f6c\u5316\u4e0e\u9006\u8f6c\u5316\u3002<\/li>\n<\/ul>\n<pre><code class=\"java\">final void treeifyBin(Node&lt;K,V&gt;[] tab, int hash) {\n    int n, index; Node&lt;K,V&gt; e;\n    if (tab == null || (n = tab.length) &lt; MIN_TREEIFY_CAPACITY)\n        \/\/ \u5982\u679c\u6570\u7ec4\u5f88\u5c0f\uff0c\u8fdb\u884c\u6269\u5bb9\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\n        resize();\n    else if ((e = tab[index = (n - 1) &amp; hash]) != null) {\n        TreeNode&lt;K,V&gt; hd = null, tl = null;\n        do {\n            \/\/ \u628a\u8282\u70b9\u5305\u88c5\u6210\u7ea2\u9ed1\u6811\u8282\u70b9\n            TreeNode&lt;K,V&gt; p = replacementTreeNode(e, null);\n            if (tl == null)\n                hd = p;\n            else {\n                \/\/ \u628a\u6811\u8282\u70b9\u94fe\u6210\u4e00\u4e2a\u94fe\u8868\n                p.prev = tl;\n                tl.next = p;\n            }\n            tl = p;\n        } while ((e = e.next) != null);\n\n        if ((tab[index] = hd) != null)\n            hd.treeify(tab);    \/\/ \u5c06\u94fe\u8868\u8fdb\u884c\u6811\u5316\n    }\n}\n<\/code><\/pre>\n<h1>5. put \u903b\u8f91<\/h1>\n<ol>\n<li>\u5982\u679c\u69fd\u6570\u7ec4\u4e3a\u7a7a\u6216\u957f\u5ea6\u4e3a 0\uff0c\u901a\u8fc7 resize \u65b9\u6cd5\u8fdb\u884c\u521d\u59cb\u5316\u6216\u6269\u5bb9\u3002<\/li>\n<li>\u901a\u8fc7 <code>hash &amp; (table.length - 1)<\/code> \u8ba1\u7b97\u51fa\u76ee\u6807\u69fd\u4f4d\u3002<\/li>\n<li>\u5982\u679c\u76ee\u6807\u69fd\u4f4d\u4e3a\u7a7a\uff0c\u521b\u5efa\u7ed3\u70b9\u5e76\u8bbe\u7f6e\u5230\u69fd\u4f4d\u4e0a\u3002<\/li>\n<li>\u69fd\u4f4d\u975e\u7a7a\uff0c\u6267\u884c\u5982\u4e0b\u67e5\u627e\u4e0e key \u76f8\u540c\u7684\u8282\u70b9\uff1a<\/li>\n<\/ol>\n<ul>\n<li>4.1 \u5982\u679c\u69fd\u4e0a\u7b2c\u4e00\u4e2a\u8282\u70b9\u7684 key \u76f8\u540c\uff0c\u627e\u5230\u3002<\/li>\n<li>4.2 \u5982\u679c\u69fd\u4e0a\u7b2c\u4e00\u4e2a\u8282\u70b9\u662f\u7ea2\u9ed1\u6811\uff0c\u5728\u7ea2\u9ed1\u6811\u4e0a\u63d2\u5165\uff0c\u8fd4\u56de\u539f\u6765\u7684\u4e0e key \u7cfb\u7edf\u76f8\u540c\u7684\u8282\u70b9\u3002<\/li>\n<li>4.3 \u69fd\u4e0a\u7b2c\u4e00\u4e2a\u8282\u70b9\u662f\u94fe\u8868\uff0c\u904d\u5386\uff1a\u5982\u679c key \u76f8\u540c\uff0c\u4e2d\u65ad\u5faa\u73af\uff0c\u627e\u4e0d\u5230\u5219\u63d2\u5165\u5230\u94fe\u8868\u672b\u5c3e\uff0c\u63d2\u5165\u540e\u5982\u679c\u8fbe\u5230\u8f6c\u4e3a\u7ea2\u9ed1\u6811\u7684\u9608\u503c TREEIFY_THRESHOLD\uff0c\u5219\u5c1d\u8bd5\u8f6c\u6362\u3002<\/li>\n<\/ul>\n<ol>\n<li>\u5982\u679c\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u6839\u636e\u53c2\u6570\u51b3\u5b9a\u662f\u5426\u66ff\u6362\u4e3a\u65b0\u503c\uff0c\u5e76\u8fd4\u56de\u65e7\u503c\u3002<\/li>\n<li>\u5143\u7d20\u8ba1\u6570\u52a0 1\uff0c\u5982\u679c\u5143\u7d20\u6570\u91cf\u8d85\u8fc7 \u6269\u5bb9\u7684\u9608\u503c\uff0c\u8fdb\u884c resize \u3002<\/li>\n<\/ol>\n<pre><code class=\"java\">final V putVal(int hash, K key, V value, boolean onlyIfAbsent,\n               boolean evict) {\n    Node&lt;K,V&gt;[] tab; Node&lt;K,V&gt; p; int n, i;\n    \/\/ \u521d\u59cb\u5316\u6216\u6269\u5bb9\n    if ((tab = table) == null || (n = tab.length) == 0)\n        n = (tab = resize()).length;\n\n    \/\/ \u76ee\u6807\u69fd\u4e3a\u7a7a\uff0c\u76f4\u63a5\u5b58\u5165\n    if ((p = tab[i = (n - 1) &amp; hash]) == null)\n        tab[i] = newNode(hash, key, value, null);\n    else {\n        Node&lt;K,V&gt; e; K k;\n        \/\/ \u76ee\u6807\u69fd\u4f4d\u7b2c\u4e00\u4e2a\u8282\u70b9\u5c31\u662f\u5339\u914d\u7684 key\n        if (p.hash == hash &amp;&amp;\n            ((k = p.key) == key || (key != null &amp;&amp; key.equals(k))))\n            e = p;\n\n        \/\/ \u662f\u7ea2\u9ed1\u6811\uff0c\u8f6c\u4e3a\u6811\u63d2\u5165\u64cd\u4f5c\n        else if (p instanceof TreeNode)\n            e = ((TreeNode&lt;K,V&gt;)p).putTreeVal(this, tab, hash, key, value);\n        else {\n        \/\/ \u94fe\u8868\uff0c\u67e5\u627e\u6216\u63d2\u5165\u5230\u94fe\u8868\u672b\u5c3e\n            for (int binCount = 0; ; ++binCount) {\n                if ((e = p.next) == null) {\n                    p.next = newNode(hash, key, value, null);\n                    \/\/ \u63d2\u5165\u540e\u8fbe\u5230\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u7684\u9608\u503c\uff0c\u5c1d\u8bd5\u8f6c\u6362\n                    if (binCount &gt;= TREEIFY_THRESHOLD - 1) \/\/ -1 for 1st\n                        treeifyBin(tab, hash);\n                    break;\n                }\n                \/\/ \u4e0e\u5df2\u6709\u8282\u70b9\u7684key\u5339\u914d\uff0c\u8df3\u51fa\u5faa\u73af\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)\n                e.value = value;\n            afterNodeAccess(e);\n            return oldValue;\n        }\n    }\n    ++modCount;\n    if (++size &gt; threshold)\n        resize();\n    afterNodeInsertion(evict);\n    return null;\n}\n<\/code><\/pre>\n<h1>6. resize \u903b\u8f91<\/h1>\n<ol>\n<li>\u6839\u636e\u65e7\u69fd\u6570\u7ec4\u8ba1\u7b97\u65b0\u7684\u69fd\u6570\u7ec4\u957f\u5ea6\u3001\u6269\u5bb9\u9608\u503c\uff0c\u9ed8\u8ba4\u6269\u5bb9\u4e3a\u539f\u6765\u7684\u4e24\u500d\u3002<\/li>\n<li>\u521b\u5efa\u65b0\u7684\u6570\u7ec4\u5e76\u66ff\u6362\u65e7\u6570\u7ec4\u3002<\/li>\n<li>\u628a\u65e7\u6570\u7ec4\u4e0a\u7684\u5143\u7d20\u8f6c\u79fb\u5230\u65b0\u6570\u636e\u4e0a\uff1a\u904d\u5386\u69fd\u6570\u7ec4\uff0c\u5bf9\u975e\u7a7a\u7684\u6570\u7ec4\u5143\u7d20 e \u8fdb\u884c\u5982\u4e0b\u5904\u7406\uff1a<\/li>\n<\/ol>\n<ul>\n<li>3.1 \u5982\u679c e.next \u4e3a\u7a7a\uff0c\u8868\u793a\u53ea\u6709\u4e00\u4e2a\u8282\u70b9\uff0c\u76f4\u63a5\u8f6c\u79fb\u5230\u65b0\u7684\u69fd\u4f4d\u4e0a\u3002<\/li>\n<li>3.2 \u5982\u679c e \u662f\u7ea2\u9ed1\u6811\uff0c\u628a\u7ea2\u9ed1\u6811\u9006\u8f6c\u3001\u62c6\u5206\u5230\u9ad8\u4f4e\u4e24\u68f5\u6811\uff0c\u5206\u522b\u8ffd\u52a0\u5230 table[index], table[index+oldCap] \u69fd\u4e0a\u3002<\/li>\n<li>3.3 \u5982\u679c\u8282\u70b9\u662f\u94fe\u8868\uff0c\u628a\u94fe\u8868\u62c6\u5206\u4e3a\u9ad8\u4f4e\u4e24\u4e2a\u94fe\u8868\uff0c\u5206\u522b\u8ffd\u52a0\u5230 table[index], table[index+oldCap] \u69fd\u4e0a\u3002<\/li>\n<\/ul>\n<pre><code class=\"java\">final Node&lt;K,V&gt;[] resize() {\n    Node&lt;K,V&gt;[] oldTab = table;\n    int oldCap = (oldTab == null) ? 0 : oldTab.length;\n    int oldThr = threshold;\n    int newCap, newThr = 0;\n    if (oldCap &gt; 0) {\n        if (oldCap &gt;= MAXIMUM_CAPACITY) {\n            threshold = Integer.MAX_VALUE;\n            return oldTab;\n        }\n        else if ((newCap = oldCap &lt;&lt; 1) &lt; MAXIMUM_CAPACITY &amp;&amp;\n                 oldCap &gt;= DEFAULT_INITIAL_CAPACITY)\n            newThr = oldThr &lt;&lt; 1; \/\/ double threshold\n    }\n    else if (oldThr &gt; 0) \/\/ initial capacity was placed in threshold\n        newCap = oldThr;\n    else {               \/\/ zero initial threshold signifies using defaults\n        newCap = DEFAULT_INITIAL_CAPACITY;\n        newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);\n    }\n    if (newThr == 0) {\n        float ft = (float)newCap * loadFactor;\n        newThr = (newCap &lt; MAXIMUM_CAPACITY &amp;&amp; ft &lt; (float)MAXIMUM_CAPACITY ?\n                  (int)ft : Integer.MAX_VALUE);\n    }\n    threshold = newThr;\n    \/\/ \u4ee5\u4e0a\u5747\u662f\u8ba1\u7b97\u65b0\u6570\u7ec4\u957f\u5ea6\u3001\u6269\u5bb9\u9608\u503c\n\n    @SuppressWarnings({\"rawtypes\",\"unchecked\"})\n        Node&lt;K,V&gt;[] newTab = (Node&lt;K,V&gt;[])new Node[newCap];\n    table = newTab;\n    if (oldTab != null) {\n        for (int j = 0; j &lt; oldCap; ++j) {\n            Node&lt;K,V&gt; e;\n            if ((e = oldTab[j]) != null) {\n                oldTab[j] = null;\n                if (e.next == null)\n                    newTab[e.hash &amp; (newCap - 1)] = e;\n                else if (e instanceof TreeNode)\n                    ((TreeNode&lt;K,V&gt;)e).split(this, newTab, j, oldCap); \/\/ \u62c6\u5206\u7ea2\u9ed1\u6811\n                else { \/\/ preserve order\n                    \/\/ \u8fd9\u91cc\u4fdd\u6301\u539f\u6765\u7684\u987a\u5e8f\u53ef\u4ee5\u907f\u514d\u5e76\u53d1 resize \u65f6\u51fa\u73b0\u6b7b\u5faa\u73af\u3002\n                    \/\/ \u4e4b\u524d\u7684\u7248\u672c\u4ee5\u9006\u5e8f\u7684\u65b9\u5f0f\u91cd\u65b0\u63d2\u5165\uff0c\u5bb9\u6613\u51fa\u73b0\u6b7b\u5faa\u73af\n                    Node&lt;K,V&gt; loHead = null, loTail = null;\n                    Node&lt;K,V&gt; hiHead = null, hiTail = null;\n                    Node&lt;K,V&gt; next;\n                    do {\n                        next = e.next;\n                        \/\/ j &lt; oldCap \u65f6 == 0\uff0c\u662f\u4f4e\u4f4d\u7684\n                        \/\/ 14 &amp; 16 == 0, 17&amp;16=1 \n                        \/\/ \u628a\u539f\u6765\u7684\u5355\u94fe\u8868\u62c6\u5206\u4e3a\u4e24\u4e2a\u94fe\u8868\n                        \/\/ \u54c8\u5e0c\u503c\u5c0f\u4e8e oldCap \u7684\u5728\u4f4e\u4f4d\u94fe\u8868\uff0c\u5176\u4ed6\u5728\u9ad8\u4f4d\u94fe\u8868\n                        if ((e.hash &amp; oldCap) == 0) {\n                            if (loTail == null)\n                                loHead = e;\n                            else\n                                loTail.next = e;\n                            loTail = e;\n                        }\n                        else {\n                            if (hiTail == null)\n                                hiHead = e;\n                            else\n                                hiTail.next = e;\n                            hiTail = e;\n                        }\n                    } while ((e = next) != null);\n                    if (loTail != null) {\n                        loTail.next = null;\n                        newTab[j] = loHead;\n                    }\n                    if (hiTail != null) {\n                        hiTail.next = null;\n                        newTab[j + oldCap] = hiHead;\n                    }\n                }\n            }\n        }\n    }\n    return newTab;\n}\n<\/code><\/pre>\n<p>\u6269\u5bb9\u903b\u8f91\u4e0e\u4e4b\u524d\u5b9e\u73b0\u5f88\u91cd\u8981\u7684\u4e00\u4e2a\u4e0d\u540c\u70b9\u662f\uff1a\u6563\u5217\u5230\u540c\u4e00\u4e2a\u69fd\u4f4d\u7684\u7ed3\u70b9\u6784\u6210\u94fe\u8868\u7684\u987a\u5e8f\uff0c\u5728\u6269\u5bb9\u4e4b\u540e\uff0c\u5982\u679c\u8fd9\u4e9b\u8282\u70b9\u4ecd\u7136\u6563\u5217\u5230\u540c\u4e00\u4e2a\u69fd\u4f4d\uff0c\u90a3\u4e48\u5b83\u4eec\u7684\u987a\u5e8f\u4f1a\u4fdd\u7559\uff0c\u800c\u4e4b\u524d\u7248\u672c\u662f\u9006\u5e8f\u7684\uff0c\u8fd9\u89e3\u51b3\u4e86\u4e4b\u524d\u7248\u672c\u5728\u51fa\u73b0\u5e76\u53d1\u6269\u5bb9\u65f6\u51fa\u73b0\u6b7b\u5faa\u73af\u7684\u95ee\u9898\u3002<\/p>\n<p>resize \u91cd\u65b0\u5206\u914d\u69fd\u4f4d\u65f6\u8981\u4fdd\u8bc1\uff1a\u65b0\u7684\u69fd\u4f4d\u4e0e <code>put\/get<\/code> \u65b9\u6cd5\u4f7f\u7528\u7684\u69fd\u4f4d\u8ba1\u7b97\u903b\u8f91\u751f\u6210\u5230\u540c\u4e00\u4e2a\u69fd\u4f4d\uff0c\u5426\u5219\u5143\u7d20\u5c31\u627e\u4e0d\u5230\u4e86\u3002<\/p>\n<p><code>put\/get<\/code> \u65b9\u6cd5\u7684\u69fd\u4f4d\u7b97\u6cd5\u662f\uff1a<code>index = (table.length - 1) &amp; hash<\/code> \u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u6570\u7ec4\u957f\u5ea6 n<\/th>\n<th>n-1 \u7684\u6bd4\u7279\u8868\u793a<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>8<\/td>\n<td>00111<\/td>\n<\/tr>\n<tr>\n<td>16<\/td>\n<td>01111<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5bf9\u4e8ehash \u503c 25\uff0c\u5176\u4e8c\u8fdb\u5236\u8868\u793a\u4e3a 11001\uff0c<br \/>\n\u6570\u7ec4\u957f\u5ea6\u4e3a 8\uff0c\u5b83\u7684\u69fd\u4f4d\u662f <code>25 &amp; (8 - 1) == 11001 &amp; 00111 = 1<\/code>\uff0c<br \/>\n\u5982\u679c\u6570\u7ec4\u957f\u5ea6\u4e3a 16\uff0c\u69fd\u4f4d\u662f <code>25 &amp; (16 - 1) = 11001 &amp; 01111 = 9<\/code>\uff0c<\/p>\n<p>\u5f53\u6570\u7ec4\u4ece\u957f\u5ea6 8 \u6269\u5bb9\u5230 16\u65f6\uff0c\u5b83\u7684\u65e7\u69fd\u4f4d\u662f 1\uff0c<code>oldCap = 8<\/code>, <code>25 &amp; oldCap<\/code> \u4e0d\u4e3a 0\uff0c\u65b0\u7684\u69fd\u4f4d\u4e3a <code>\u65e7\u69fd\u4f4d+oldCap = 1+8 = 9<\/code>\uff0c\u4e0e <code>put\/get<\/code> \u65b9\u6cd5\u7b97\u7684\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p>\u5f53resize\u65f6\uff0c\u539f\u6765\u4e00\u4e2a\u69fd\u91cc\u7684\u5143\u7d20\u53ea\u4f1a\u91cd\u65b0\u5206\u914d\u5230\u4e24\u4e2a\u69fd\u4f4d\u4e0a\u53bb\uff1a<code>j<\/code> \uff0c <code>oldCap + j<\/code> \uff0c\u56e0\u6b64\u4e0a\u9762\u7684\u4ee3\u7801\u7ef4\u62a4\u4e86\u5bf9\u4e24\u4e2a\u94fe\u8868\u7684\u9996\u5c3e\u5f15\u7528\u3002<\/p>\n<h1>7. \u4e3a\u4ec0\u4e48 HashMap \u7684 table \u6570\u7ec4\u7528 transient \u4fee\u9970\uff1f<\/h1>\n<ol>\n<li>table \u591a\u6570\u60c5\u51b5\u4e0b\u662f\u672a\u586b\u6ee1\u7684\uff0c\u5e8f\u5217\u5316\u672a\u4f7f\u7528\u90e8\u5206\uff0c\u6d6a\u8d39\u7a7a\u95f4\u3002<\/li>\n<li>\u540c\u4e00\u4e2a\u952e\u5728\u4e0d\u540c\u7684 JVM \u4e0b\u7b97\u51fa\u6765\u7684\u54c8\u5e0c\u503c\u53ef\u80fd\u4e0d\u540c\uff0c\u6240\u5904\u7684\u69fd\u4f4d\u4e5f\u5c31\u4e0d\u540c\uff0c\u5728\u4e0d\u540c\u7684 JVM \u4e0b\u53cd\u5e8f\u5217\u5316 table \u53ef\u80fd\u51fa\u9519\u3002<\/li>\n<li>\u901a\u8fc7\u5b9e\u73b0 <code>readObject\/writeObject<\/code> \u6765\u5b9a\u5236\u5e8f\u5217\u5316\u884c\u4e3a\u3002<\/li>\n<\/ol>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong> \u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6e90\u7801\u57fa\u4e8e JDK 1.8.0_101 \u3002 \u5728 JDK 1.8 \u4e4b\u524d\uff0cHash &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/java\/20210607\/2159\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[356,231,91],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2159"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=2159"}],"version-history":[{"count":3,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2159\/revisions"}],"predecessor-version":[{"id":2162,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2159\/revisions\/2162"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=2159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=2159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=2159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}