{"id":632,"date":"2022-01-18T15:58:01","date_gmt":"2022-01-18T07:58:01","guid":{"rendered":"https:\/\/bigwer.cn\/?p=632"},"modified":"2022-03-12T18:59:46","modified_gmt":"2022-03-12T10:59:46","slug":"jdk1-8-map","status":"publish","type":"post","link":"https:\/\/bigwer.cn\/index.php\/2022\/01\/18\/jdk1-8-map\/","title":{"rendered":"JDK1.8-Map"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<h3>\u4e00.Map<\/h3>\n<h4>1.\u7279\u70b9<\/h4>\n<ol>\n<li>Map \u548c Collection \u6ca1\u6709\u7ee7\u627f\u5173\u7cfb<\/li>\n<li>Map \u4ee5(key,value)\u7684\u5f62\u5f0f\u5b58\u50a8\u6570\u636e\uff1a(\u952e\u503c\u5bf9),key\u548cvalue\u5b58\u50a8\u7684\u90fd\u662f\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740(\u5f15\u7528)<\/li>\n<\/ol>\n<h4>2.\u904d\u5386\u65b9\u5f0f<\/h4>\n<h5>(1).\u83b7\u53d6map\u7684KeySet\uff0c\u7136\u540e\u53d6\u51faKey\u5bf9\u5e94\u7684Value<\/h5>\n<p>\u8fd9\u79cd\u904d\u5386\u65b9\u5f0f\u6548\u7387\u76f8\u5bf9\u8f83\u4f4e\uff0c\u56e0\u4e3a\u4ed6\u7684\u904d\u5386\u89c4\u5219\u662f\u6839\u636ekey\u4e00\u4e2a\u4e00\u4e2a\u7684\u4ece\u54c8\u5e0c\u8868\u4e2d\u67e5\u627evalue\u5b9e\u73b0\u7684\uff0c\u6709\u4e24\u79cd\u5199\u6cd5\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<ul>\n<li>\u5199\u6cd51\uff1a\u901a\u8fc7\u5faa\u73af\u904d\u5386map.keySet(),\u53d6\u51fa\u5bf9\u5e94\u7684value\n<pre><code class=\"language-java\">public static void printMap(Map&lt;String,String&gt; map){\n    if(map.size()==0){\n        System.out.println(&quot;empty map&quot;);\n    }\n    for (String key : map.keySet()) {\n        System.out.println(key+&quot;:&quot;+map.get(key));\n    }\n}<\/code><\/pre>\n<\/li>\n<li>\u5199\u6cd52\uff1a\u901a\u8fc7\u8fed\u4ee3\u5668\u8fed\u4ee3map.keySet()\uff0c\u6765\u53d6\u51fa\u5bf9\u5e94\u7684value\n<pre><code class=\"language-java\">public static void printMap(Map&lt;String,String&gt; map){\n    Set&lt;String&gt; keySet = map.keySet();\n    Iterator&lt;String&gt; iterator = keySet.iterator();\n    while (iterator.hasNext()){\n        String key = iterator.next();\n        System.out.println(key+&quot;:&quot;+map.get(key));\n    }\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<h5>(2).\u8c03\u7528map.entrySet()\u65b9\u6cd5\uff0c\u83b7\u53d6entrySet\uff0c\u7136\u540e\u76f4\u63a5\u4eceentrySet\u4e2d\u83b7\u53d6Key\u548cvalue<\/h5>\n<p>\u8fd9\u79cd\u904d\u5386\u65b9\u5f0f\u662f\u76f4\u63a5\u4ecenode\u4e2d\u83b7\u53d6key\uff0cvalue\u6240\u4ee5\u6548\u7387\u8f83\u9ad8\uff0c\u6bd4\u8f83\u9002\u7528\u4e8e\u5927\u6570\u91cfmap\u7684\u904d\u5386<\/p>\n<ul>\n<li>\u5199\u6cd51\uff1a\u8c03\u7528map.entrySet()\uff0c\u7136\u540e\u901a\u8fc7\u5faa\u73af\u904d\u5386entrySet\n<pre><code class=\"language-java\">public static void printMap(Map&lt;String,String&gt; map){\n    Set&lt;Map.Entry&lt;String, String&gt;&gt; entrySet = map.entrySet();\n    for (Map.Entry&lt;String, String&gt; entry : entrySet) {\n        System.out.println(entry.getKey()+&quot;:&quot;+entry.getValue());\n    }\n}<\/code><\/pre>\n<\/li>\n<li>\u5199\u6cd52\uff1a\u8c03\u7528map.entrySet()\uff0c\u901a\u8fc7\u8fed\u4ee3\u5668\u904d\u5386entrySet\n<pre><code class=\"language-java\">public static void printMap(Map&lt;String,String&gt; map){\n    Set&lt;Map.Entry&lt;String, String&gt;&gt; entrySet = map.entrySet();\n    Iterator&lt;Map.Entry&lt;String, String&gt;&gt; iterator = entrySet.iterator();\n    while (iterator.hasNext()){\n        Map.Entry&lt;String, String&gt; node = iterator.next();\n        System.out.println(node.getKey() + &quot;:&quot; + node.getValue());\n    }\n}<\/code><\/pre>\n<\/li>\n<li>\u5199\u6cd53\uff1a\u76f4\u63a5\u4f7f\u7528foreach\u904d\u5386\n<pre><code class=\"language-java\">public static void printMap(Map&lt;String,String&gt; map){\n    if(map.size()==0){\n        System.out.println(&quot;empty map&quot;);\n    }\n    map.forEach((key,value)-&gt;{\n        System.out.println(key+&quot;:&quot;+value);\n    });\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>\u4e8c.HashMap<\/h3>\n<h4>1.\u6982\u8ff0<\/h4>\n<ol>\n<li>HashMap\u7684\u5e95\u5c42\u5176\u5b9e\u662f\u4e00\u4e2a\u6570\u7ec4<\/li>\n<li>\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u662f\u4e00\u4e2a\u5355\u5411\u94fe\u8868\uff0c\u91c7\u7528\u62c9\u94fe\u6cd5\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\uff0c\u5355\u94fe\u8868\u7684\u6bcf\u4e2a\u7ed3\u70b9\u662fNode&lt;K,V&gt;\u7c7b\u578b<\/li>\n<li>\u540c\u4e00\u4e2a\u5355\u94fe\u8868\u4e2d\u6240\u6709Node\u7684hash\u503c\u4e0d\u4e00\u5b9a\u4e00\u6837\uff0c\u4f46\u662f\u4ed6\u4eec\u6240\u5bf9\u5e94\u7684\u6570\u7ec4\u4e0b\u6807\u4e00\u5b9a\u4e00\u6837(\u6570\u7ec4\u7684\u4e0b\u6807\u662f\u5229\u7528 \u54c8\u5e0c\u51fd\u6570\/\u54c8\u5e0c\u7b97\u6cd5\u6839\u636ehash\u503c\u8ba1\u7b97\u5f97\u5230\u7684)<\/li>\n<li>\u5728JDK1.8\u4e4b\u524dHashMap\u662f\u6570\u7ec4\u548c\u5355\u94fe\u8868\u7684\u7ed3\u5408\u4f53\uff0c\u5728JDK1.8\u4e2d\uff0c\u52a0\u5165\u4e86\u7ea2\u9ed1\u6811\n<ul>\n<li>\u6570\u7ec4\u7684\u67e5\u8be2\u6548\u7387\u9ad8\uff0c\u4f46\u662f\u589e\u5220\u5143\u7d20\u6548\u7387\u8f83\u4f4e<\/li>\n<li>\u5355\u94fe\u8868\u5728\u968f\u673a\u589e\u5220\u5143\u7d20\u65b9\u9762\u6548\u7387\u8f83\u9ad8\uff0c\u4f46\u662f\u67e5\u8be2\u6548\u7387\u8f83\u4f4e<\/li>\n<li>\u94fe\u8868\u8fc7\u957f\u4f1a\u4e25\u91cd\u5f71\u54cdHashMap\u7684\u6027\u80fd\uff0c\u7ea2\u9ed1\u6811\u7684\u7ed3\u6784\u53ef\u4ee5\u5feb\u901f\u589e\u5220\u6539\u67e5<\/li>\n<li>HashMap\u5c06\u5b83\u4eec\u76f8\u7ed3\u5408\uff0c\u5145\u5206\u5229\u7528\u4e86\u5b83\u4eec\u5404\u81ea\u7684\u4f18\u70b9<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h4>2.\u7279\u70b9<\/h4>\n<ol>\n<li>HashMap\u662f\u65e0\u5e8f\u4e14\u4e0d\u53ef\u91cd\u590d\u7684<\/li>\n<li>HashMap\u53ea\u5141\u8bb8\u5b58\u50a8\u4e00\u6761\u8bb0\u5f55\u4e3anull\u7684\u952e\uff0c\u5141\u8bb8\u591a\u6761\u8bb0\u5f55\u7684\u503c\u4e3anull\u3002<\/li>\n<li>HashMap\u662f\u975e\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u5982\u679c\u8981\u6ee1\u8db3\u7ebf\u7a0b\u5b89\u5168\uff0c\u53ef\u4ee5\u7528Collections\u7684synchronizedMap\u4f7fHashMap\u5177\u6709\u7ebf\u7a0b\u5b89\u5168\u7684\u80fd\u529b\uff0c\u6216\u8005\u4f7f\u7528ConcurrentHashMap<\/li>\n<li>HashMap\u5728JDK1.8\u4e4b\u524d\u5e95\u5c42\u6b21\u91c7\u7528<strong>\u6570\u7ec4+\u94fe\u8868<\/strong>\u7684\u5f62\u5f0f\uff0c\u5728JDK1.8\u65f6\u65b0\u589e\u4e86<strong>\u7ea2\u9ed1\u6811<\/strong>\u90e8\u5206<\/li>\n<\/ol>\n<h4>3.\u53c2\u6570<\/h4>\n<pre><code class=\"language-java\">\/\/\u9ed8\u8ba4\u521d\u59cb\u5316\u5bb9\u91cf\u4e3a16\nstatic final int DEFAULT_INITIAL_CAPACITY = 1 &lt;&lt; 4;\n\/\/\u6700\u5927\u5bb9\u91cf2^30\nstatic final int MAXIMUM_CAPACITY = 1 &lt;&lt; 30\n\/\/\u52a0\u8f7d\u56e0\u5b50\u4e3a0.75\nstatic final float DEFAULT_LOAD_FACTOR = 0.75f;\n\/\/\u94fe\u8868\u8f6c\u4e3a\u7ea2\u9ed1\u6811\u7684\u9608\u503c\u4e3a8\nstatic final int TREEIFY_THRESHOLD = 8;\n\/\/\u7ea2\u9ed1\u6811\u8f6c\u4e3a\u94fe\u8868\u7684\u9608\u503c\u4e3a6\nstatic final int UNTREEIFY_THRESHOLD = 6;\n\/\/\u6700\u5c0f\u6811\u578b\u5316\u9608\u503c\u4e3a64\nstatic final int MIN_TREEIFY_CAPACITY = 64;\n\/\/\u5b58\u50a8\u5143\u7d20\u7684\u5bb9\u5668\ntransient Node&lt;K,V&gt;[] table;\n\/\/key-value\u7684\u4e2a\u6570\ntransient int size;\n\/\/\u64cd\u4f5c\u6b21\u6570\ntransient int modCount;\n\/\/\u80fd\u5bb9\u7eb3\u6700\u591aKey-value\u7684\u4e2a\u6570\nint threshold;<\/code><\/pre>\n<h5>(1).DEFAULT_INITIAL_CAPACITY(\u9ed8\u8ba4\u521d\u59cb\u5316\u5bb9\u91cf)<\/h5>\n<h6>\u2460.\u5bb9\u91cf\u4e3a\u4ec0\u4e48\u662f2\u7684n\u6b21\u5e42\u5462<\/h6>\n<ol>\n<li>HashMap\u7684\u6570\u7ec4\u662fhash\u8868\uff0c\u8ba1\u7b97\u6563\u5217\u503c\u7684\u65f6\u5019\u4f1a\u6709<strong>\u53d6\u6a21<\/strong>\u8fd0\u7b97(<strong>hash%length<\/strong>)\uff0c\u4f46\u662f\u53d6\u6a21\u8fd0\u7b97\u76f8\u5bf9\u6bd4\u8f83\u8017\u65f6\uff0c\u6240\u4ee5\u91c7\u7528\u7684\u662f<strong>hash&amp;(length-1)<\/strong>\u7684\u65b9\u5f0f\uff0c\u800c<strong>hash%length==hash&amp;(length-1)<\/strong>\u7684\u524d\u63d0\u662flength\u662f2\u7684n\u6b21\u5e42\u3002<\/li>\n<li>\u5f53length\u4e3a2\u7684n\u6b21\u5e42\u65f6\uff0c\u53d1\u751fhash\u51b2\u7a81\u7684\u6982\u7387\u4f1a\u5c0f\u5f88\u591a\uff0c\u7a7a\u95f4\u5229\u7528\u7387\u4e5f\u4f1a\u53d8\u9ad8\uff0c\u4f8b\u5982\uff1a\n<pre><code class=\"language-text\">\u5f53\u957f\u5ea6\u4e3a9\u65f6\uff1a\n3&amp;(9-1)=0 2&amp;(9-1)=0 \u90fd\u57280\u4e0a\u53d1\u751fhash\u51b2\u7a81\n\u5f53\u957f\u5ea6\u4e3a8\u65f6\uff1a\n3&amp;(8-1)=3 2&amp;(8-1)=2 \u4e0d\u540c\u4f4d\u7f6e\u4e0a\uff0c\u4e0d\u53d1\u751fhash\u51b2\u7a81<\/code><\/pre>\n<\/li>\n<\/ol>\n<h6>\u2461.\u5982\u679c\u5b9e\u4f8b\u5316\u4f20\u5165\u7684\u503c\u4e0d\u662f2\u7684n\u6b21\u5e42\u4f1a\u600e\u4e48\u6837<\/h6>\n<p>\u5982\u679c\u521b\u5efaHashMap\u65f6\uff0c\u7ed9\u5b9a\u7684number\u4e0d\u662f2\u7684n\u6b21\u5e42\uff0c\u90a3\u4e48\u4f1a\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\uff0c\u8ba1\u7b97\u627e\u5230\u5927\u4e8e\u7b49\u4e8enumber\u7684\u6700\u5c0f2\u6b21\u5e42\u7684\u6570<\/p>\n<pre><code class=\"language-java\">static final int tableSizeFor(int cap) {\n        \/\/\u4f7f\u7528cap-1\u662f\u4e3a\u4e86\u907f\u514d\u4f20\u5165\u7684cap\u503c\u672c\u8eab\u5c31\u662f2\u7684n\u6b21\u5e42\u800c\u505a\u7684\u5904\u7406\n        int n = cap - 1;\n        n |= n &gt;&gt;&gt; 1;\n        n |= n &gt;&gt;&gt; 2;\n        n |= n &gt;&gt;&gt; 4;\n        n |= n &gt;&gt;&gt; 8;\n        \/\/\u5bb9\u91cf\u6700\u5927\u4e5f\u5c31\u662f32bit\u7684\u6b63\u6570\uff0c\u56e0\u6b64\u6700\u540e\u53ea\u9700\u8981n|=n&gt;&gt;&gt;16\u5373\u53ef\uff0c\u6700\u591a\u4e5f\u5c3132\u4e2a1\uff0c\u5728\u6784\u9020\u51fd\u6570\u8c03\u7528\u65b9\u6cd5\u7684\u65f6\u5019\u5176\u5b9e\u5df2\u7ecf\u505a\u4e86\u5224\u65ad\u4e86\u3002\n        n |= n &gt;&gt;&gt; 16;\n        return (n &lt; 0) ? 1 : (n &gt;= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;\n    }\n\/\/\u4ee3\u7801\u5206\u6790\ncap = 13;     \/\/     \u8ba1\u7b97\u8fc7\u7a0b\uff08\u4e8c\u8fdb\u5236\uff09\n        n = cap-1;            0000 1100\n        \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n        n |= n&gt;&gt;&gt;1;           0000 1100 | 0000 0110\n        \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n        0000 1110\n        \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n        n |= n&gt;&gt;&gt;2;           0000 1110 | 0000 0011\n        \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n        0000 1111\n        \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n        n |= n&gt;&gt;&gt;4;           0000 1111 | 0000 0000\n        \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n        0000 1111\n        \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n        n |= n&gt;&gt;&gt;16;          0000 1111 | 0000 0000\n        \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n        0000 1111\n        \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n        \/\/n\u7b49\u4e8e15\uff0c\u65e2\u4e0d\u5c0f\u4e8e0\uff0c\u4e5f\u4e0d\u5927\u4e8e\u6700\u5927\u5bb9\u91cf\u6240\u4ee5\u8fd4\u56den+1\n        n = n + 1             0000 1111 + 0000 0001\n        \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n        0001 0000    =    16<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4e00\u7cfb\u5217\u8fd0\u7b97\u7684\u76ee\u7684\uff0c\u5c31\u662f\u5c06cap-1\u7684\u4e8c\u8fdb\u5236\u6709\u6548\u4f4d\u5168\u90e8\u53d8\u62101\uff0c\u6700\u540e\u518d\u7528\u8fd9\u4e2a\u4e9b\u6709\u6548\u4f4d\u4e3a1\u7684\u4e8c\u8fdb\u5236\u6570+1\u5c31\u53d8\u6210\u4e86\u5927\u4e8e\u7b49\u4e8e\u4f20\u5165\u5bb9\u91cf\u6700\u5c0f2\u7684n\u6b21\u5e42\u7684\u6570\u3002<\/p>\n<h5>(2).MAXIMUM_CAPACITY(\u6700\u5927\u5bb9\u91cf)<\/h5>\n<h6>\u2460\u4e3a\u4ec0\u4e48\u6700\u5927\u5bb9\u91cf\u662f2^30<\/h6>\n<ol>\n<li>\u6ee1\u8db3\u5bb9\u91cf\u4e3a2\u7684n\u6b21\u5e42\u7684\u7279\u70b9<\/li>\n<li>int\u7c7b\u578b\u662f32bit\uff0c\u53604\u4e2a\u5b57\u8282\u3002<\/li>\n<li>Java\u7684\u539f\u59cb\u7c7b\u578b\u91cc\u6ca1\u6709\u65e0\u7b26\u53f7\u7c7b\u578b\uff0c\u6240\u4ee5\u9996\u4f4d\u662f\u7b26\u53f7\u4f4d \u6b63\u6570\u4e3a0\uff0c\u8d1f\u6570\u4e3a1\u3002<\/li>\n<li>Java\u4e2d\u5b58\u653e\u7684\u662f\u8865\u7801\uff0c1\u5de6\u79fb31\u4f4d\u4ee3\u8868\u7684\u662f-2147483648\uff0c\u6240\u4ee5\u6700\u5927\u53ea\u80fd\u523030\u3002 <\/li>\n<\/ol>\n<h5>(3).DEFAULT_LOAD_FACTOR(\u9ed8\u8ba4\u52a0\u8f7d\u56e0\u5b50)<\/h5>\n<h6>\u2460\u52a0\u8f7d\u56e0\u5b50\u662f\u4ec0\u4e48<\/h6>\n<p>\u8fd9\u4e2a\u52a0\u8f7d\u56e0\u5b50\u8868\u793a\u7684\u662fhash\u8868\u4e2d\u5143\u7d20\u586b\u6ee1\u7684\u7a0b\u5ea6\uff0c\u52a0\u8f7d\u56e0\u5b50\u8d8a\u5927\uff0c\u80fd\u586b\u5145\u7684\u5143\u7d20\u5c31\u8d8a\u591a\u3002<\/p>\n<ul>\n<li>\u52a0\u8f7d\u56e0\u5b50\u8d8a\u5927\uff0c\u7a7a\u95f4\u5229\u7528\u7387\u5c31\u8d8a<strong>\u9ad8<\/strong>\uff0c\u4ea7\u751fhash\u51b2\u7a81\u7684\u673a\u4f1a\u4e5f\u4f1a<strong>\u968f\u4e4b\u52a0\u5927<\/strong>\u3002<\/li>\n<li>\u52a0\u8f7d\u56e0\u5b50\u8d8a\u5c0f\uff0c\u7a7a\u95f4\u5229\u7528\u7387\u5c31\u8d8a<strong>\u4f4e<\/strong>\uff0c\u4ea7\u751fhash\u51b2\u7a81\u7684\u673a\u4f1a\u4e5f\u4f1a<strong>\u968f\u4e4b\u51cf\u5c0f<\/strong>\u3002<\/li>\n<\/ul>\n<h6>\u2461\u52a0\u8f7d\u56e0\u5b50\u7684\u4f5c\u7528<\/h6>\n<p>\u5728\u5f53\u524d\u6570\u7ec4\u957f\u5ea6\u8fbe\u5230\u5bb9\u91cf\u768475%\u65f6\uff0c\u5c31\u4f1a\u89e6\u53d1\u6269\u5bb9\u64cd\u4f5c<\/p>\n<h6>\u2462\u4e3a\u4ec0\u4e48\u9700\u8981\u52a0\u8f7d\u56e0\u5b50<\/h6>\n<p>\u5728JDK1.8\u4e4b\u524d\uff0c\u4f7f\u7528\u7684<strong>\u6570\u7ec4<\/strong>+<strong>\u94fe\u8868<\/strong>\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u901a\u8fc7\u8ba1\u7b97hash\uff0c\u627e\u5230\u6570\u7ec4\u4e2d\u5bf9\u5e94\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u5df2\u7ecf\u5b58\u5728\u5143\u7d20\uff0c\u5c31\u52a0\u5230\u8fd9\u4e2a\u4f4d\u7f6e\u7684\u94fe\u8868\u4e0a\u3002<br \/>\n\u5728JDK1.8\u4e4b\u540e\uff0c\u94fe\u8868\u8fc7\u957f\u8fd8\u4f1a\u8f6c\u4e3a\u7ea2\u9ed1\u6811\uff0c\u7ea2\u9ed1\u6811\u76f8\u8f83\u4e8e\u539f\u6765\u7684\u94fe\u8868\uff0c\u5360\u7528\u7684\u7a7a\u95f4\u4f1a\u591a\u51fa\u8fd1\u4e00\u500d\uff0c\u4f46\u662f\u67e5\u8be2\u901f\u5ea6\u786e\u5feb\u4e86\u975e\u5e38\u591a\uff0c\u5c5e\u4e8e\u662f\u7528<strong>\u7a7a\u95f4\u6362\u65f6\u95f4<\/strong>\uff0c\u5c06\u94fe\u8868\u8f6c\u7ea2\u9ed1\u6811\uff0c\u662f\u4e00\u4e2a\u8017\u65f6\u7684\u64cd\u4f5c\uff0c\u4f46\u662f\u4e00\u4e2a\u9ad8\u6548\u7387\u7684hash\u8868\u5185\u90e8\u7684\u94fe\u8868\u4e0d\u5e94\u8be5\u8fc7\u957f\u3002<br \/>\n\u5982\u679c\u6570\u7ec4\u7684\u5f88\u591a\u5143\u7d20\u4e0a\u9762\u90fd\u6709\u503c\u4e86\uff0c\u90a3\u4e48\u9700\u8981\u5bf9\u8fd9\u4e2a\u6570\u7ec4\u6269\u5bb9\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u5c31\u662f<strong>resize<\/strong>\uff0c\u8fd9\u4e2a<strong>resize<\/strong>\u662f\u4e00\u4e2a\u975e\u5e38\u8017\u65f6\u7684\u64cd\u4f5c\uff0c\u4e3a\u4e86\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u65f6\u673a\u6765\u89e6\u53d1\u3002<\/p>\n<h6>\u2463\u4e3a\u4ec0\u4e48\u662f0.75<\/h6>\n<p>\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\uff0c\u8003\u8651\u7684\u65b9\u9762\u4e0d\u540c\uff0c\u4e0d\u540c\u7684\u8bed\u8a00\u7684defaultLoadFactor\u90fd\u4e0d\u4e00\u6837\uff0c\u6bd4\u5982java\u7684\u662f0.75\uff0c\u800cGo\u662f0.65\uff0cPython\u4e2d\u786e\u662f0.762\uff0c\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b<a href=\"https:\/\/www.jianshu.com\/p\/f1142c24ccf3\" target=\"_blank\"  rel=\"nofollow\" >\u300aHashMap\u7684loadFactor\u4e3a\u4ec0\u4e48\u662f0.75\u300b<\/a><br \/>\n\u53d60.75\u7684\u771f\u6b63\u539f\u56e0\u5728\u6e90\u7801\u4e2d\u5176\u5b9e\u662f\u6709\u6ce8\u91ca\u8bf4\u660e\u7684<\/p>\n<pre><code class=\"language-text\">&lt;p&gt;As a general rule, the default load factor (.75) offers a good\ntradeoff between time and space costs.  Higher values decrease the\nspace overhead but increase the lookup cost (reflected in most of\nthe operations of the &lt;tt&gt;HashMap&lt;\/tt&gt; class, including\n&lt;tt&gt;get&lt;\/tt&gt; and &lt;tt&gt;put&lt;\/tt&gt;).  The expected number of entries in\nthe map and its load factor should be taken into account when\nsetting its initial capacity, so as to minimize the number of\nrehash operations.  If the initial capacity is greater than the\nmaximum number of entries divided by the load factor, no rehash\noperations will ever occur.<\/code><\/pre>\n<p>\u5927\u6982\u610f\u601d\u5c31\u662f\uff1a\u52a0\u8f7d\u56e0\u5b50\u592a\u5c0f\u6d6a\u8d39\u7a7a\u95f4\u4e14\u4f1a\u53d1\u751f\u66f4\u591a\u6b21\u6570\u7684rehash\uff0c\u592a\u5927\u7684\u8bdd\u4f1a\u589e\u52a0hash\u51b2\u7a81\uff0c\u6240\u4ee50.75\u662f\u4e00\u4e2a\u6298\u4e2d\u7684\u9009\u62e9\u3002<\/p>\n<h5>(4).TREEIFY_THRESHOLD(\u94fe\u8868\u8f6c\u4e3a\u7ea2\u9ed1\u6811\u9608\u503c)<\/h5>\n<h6>\u2460.\u4e3a\u4ec0\u4e48Map\u6876\u4e2d\u4e2a\u6570\u8d85\u8fc78\u624d\u8f6c\u4e3a\u7ea2\u9ed1\u6811<\/h6>\n<p>\u6e90\u7801\u4e2d\u4e5f\u7ed9\u51fa\u4e86\u89e3\u91ca<\/p>\n<pre><code class=\"language-text\">Because TreeNodes are about twice the size of regular nodes, we\nuse them only when bins contain enough nodes to warrant use\n(see TREEIFY_THRESHOLD). And when they become too small (due to\nremoval or resizing) they are converted back to plain bins.  In\nusages with well-distributed user hashCodes, tree bins are\nrarely used.  Ideally, under random hashCodes, the frequency of\nnodes in bins follows a Poisson distribution\n(http:\/\/en.wikipedia.org\/wiki\/Poisson_distribution) with a\nparameter of about 0.5 on average for the default resizing\nthreshold of 0.75, although with a large variance because of\nresizing granularity. Ignoring variance, the expected\noccurrences of list size k are (exp(-0.5) * pow(0.5, k) \/\nfactorial(k)). The first values are:\n0:    0.60653066\n1:    0.30326533\n2:    0.07581633\n3:    0.01263606\n4:    0.00157952\n5:    0.00015795\n6:    0.00001316\n7:    0.00000094\n8:    0.00000006\nmore: less than 1 in ten million<\/code><\/pre>\n<p>\u8fd9\u6bb5\u5185\u5bb9\u8bf4\u5230\uff0chashcode\u79bb\u6563\u6027\u5f88\u597d\u7684\u65f6\u5019\uff0c\u6811\u578bbin\u7528\u5230\u7684\u6982\u7387\u975e\u5e38\u5c0f\uff0c\u56e0\u4e3a\u6570\u636e\u5747\u5300\u5206\u5e03\u5728\u6bcf\u4e2abin\u4e2d\uff0c\u51e0\u4e4e\u4e0d\u4f1a\u6709bin\u4e2d\u94fe\u8868\u957f\u5ea6\u4f1a\u8fbe\u5230\u9608\u503c\u3002<br \/>\n\u4f46\u662f\u5728\u968f\u673ahashcode\u4e0b\uff0c\u79bb\u6563\u6027\u53ef\u80fd\u4f1a\u53d8\u5dee\uff0c\u800cJDK\u53c8\u4e0d\u80fd\u963b\u6b62\u7528\u6237\u5b9e\u73b0\u8fd9\u79cd\u4e0d\u597d\u7684hash\u7b97\u6cd5\uff0c\u56e0\u6b64\u53ef\u80fd\u5bfc\u81f4\u4e0d\u5747\u5300\u7684\u6570\u636e\u5206\u5e03\u3002<br \/>\n\u4e0d\u8fc7\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u968f\u673a\u7684hashcode\u7b97\u6cd5\u4e0b\u6240\u6709\u7684bin\u4e2d\u7684\u7ed3\u70b9\u5206\u5e03\u9891\u7387\u4f1a\u9075\u5faa\u6cca\u677e\u5206\u5e03\uff0c\u6ce8\u91ca\u4e2d\u4e5f\u5217\u51fa\u6765\u4e86\uff0c\u94fe\u8868\u957f\u5ea6\u5230\u8fbe8\u7684\u6982\u7387\u4e3a0.00000006\uff0c\u51e0\u4e4e\u662f\u4e0d\u53ef\u80fd\u4e8b\u4ef6\uff0c\u6240\u4ee5\u624d\u9009\u62e98\u4f5c\u4e3a\u9608\u503c\u3002<\/p>\n<h5>(5).UNTREEIFY_THRESHOLD(\u7ea2\u9ed1\u6811\u8f6c\u4e3a\u94fe\u8868\u9608\u503c)<\/h5>\n<h6>\u2460.\u4e3a\u4ec0\u4e48\u9608\u503c\u4e3a6<\/h6>\n<p>\u5982\u679c\u9608\u503c\u4e3a7\uff0c\u5f53\u6211\u4eec\u9891\u7e41\u7684\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u65f6\uff0c\u4f1a\u9020\u6210\u7ea2\u9ed1\u6811\u548c\u94fe\u8868\u7684\u9891\u7e41\u8f6c\u6362\uff0c\u5f71\u54cd\u6027\u80fd\u3002<\/p>\n<h5>(6).MIN_TREEIFY_CAPACITY(\u6700\u5c0f\u6811\u578b\u5316\u9608\u503c)<\/h5>\n<h6>\u2460.\u4f5c\u7528<\/h6>\n<p>\u5f53\u6570\u7ec4\u4e2d\u7ed3\u70b9\u5927\u4e8e<strong>TREEIFY_THRESHOLD<\/strong>\uff0c\u4f46\u662f\u6574\u4e2a\u54c8\u5e0c\u8868\u7684\u5bb9\u91cf\u5c0f\u4e8e<strong>MIN_TREEIFY_CAPACITY<\/strong>\u65f6\uff0c\u53ea\u4f1a\u6269\u5bb9\uff0c\u4e0d\u4f1a\u53d8\u6210\u7ea2\u9ed1\u6811\uff0c\u53ea\u6709\u5728\u54c8\u5e0c\u8868\u7684\u5bb9\u91cf\u5927\u4e8e<strong>MIN_TREEIFY_CAPACITY<\/strong>\u5e76\u4e14\u6570\u7ec4\u4e2d\u7ed3\u70b9\u5927\u4e8e<strong>TREEIFY_THRESHOLD<\/strong>\u65f6\uff0c\u624d\u4f1a\u8f6c\u4e3a\u7ea2\u9ed1\u6811\u3002<\/p>\n<h6>\u2461.\u4e3a\u4ec0\u4e48\u53d664<\/h6>\n<pre><code class=\"language-text\">The smallest table capacity for which bins may be treeified.\n(Otherwise the table is resized if too many nodes in a bin.)\nShould be at least 4 * TREEIFY_THRESHOLD to avoid conflicts\nbetween resizing and treeification thresholds.<\/code><\/pre>\n<p>\u6ce8\u91ca\u4e2d\u6709\u63d0\u5230\uff0cMIN_TREEIFY_CAPACITY\u7684\u503c\u81f3\u5c11\u662fTREEIFY_THRESHOLD\u76844\u500d\uff0c\u4f46\u662f\u8f6c\u4e3a\u7ea2\u9ed1\u6811\u7684\u9608\u503c\u662f8\uff0c\u4e5f\u5c31\u8bf4MIN_TREEIFY_CAPACITY\u7684\u503c\u6700\u5c11\u5e94\u8be5\u4e3a32\uff0c\u8fd9\u91cc\u8bbe\u7f6e\u621064\u7684\u539f\u56e0\u53ef\u80fd\u662f\u7b49\u5230\u6570\u7ec4\u6269\u5bb9\u5230\u4e8632\u540e\u518d\u6269\u5bb9\u4e00\u6b21\u540e\u624d\u4f1a\u5f00\u59cb\u89e6\u53d1\u6811\u578b\u5316\u3002<\/p>\n<h4>4.\u6784\u9020\u5668<\/h4>\n<p>HashMap\u4e00\u5171\u67094\u4e2a\u6784\u9020\u5668\u3002<\/p>\n<h5>(1).\u5e38\u7528\u6784\u9020\u5668<\/h5>\n<p>\u4f7f\u7528\u8be5\u6784\u9020\u5668\u5982\u679c\u7528\u6237\u6ca1\u6709\u4f20\u5165initialCapacity \u548cloadFactor\u8fd9\u4e24\u4e2a\u53c2\u6570\uff0c\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002<br \/>\ninitialCapacity\u9ed8\u8ba4\u4e3a16\uff0cloadFactory\u9ed8\u8ba4\u4e3a0.75<\/p>\n<pre><code class=\"language-java\">public HashMap(int initialCapacity, float loadFactor) {\n        if (initialCapacity &lt; 0)\n            throw new IllegalArgumentException(&quot;Illegal initial capacity: &quot; +initialCapacity);\n        if (initialCapacity &gt; MAXIMUM_CAPACITY)\n            \/\/\u5982\u679c\u6307\u5b9a\u7684\u957f\u5ea6\u5927\u4e8e\u4e86\u6700\u5927\u6570\u7ec4\u5bb9\u91cf\uff0c\u90a3\u5c31\u4f1a\u4f7f\u7528\u6700\u5927\u6570\u7ec4\u5bb9\u91cf\u4f5c\u4e3a\u5bb9\u91cf\n            initialCapacity = MAXIMUM_CAPACITY;\n        if (loadFactor &lt;= 0 || Float.isNaN(loadFactor))\n            throw new IllegalArgumentException(&quot;Illegal load factor: &quot; +loadFactor);\n        this.loadFactor = loadFactor;\n            \/\/\u521d\u59cb\u5316\u5bb9\u91cf\u4e5f\u4f1a\u6839\u636e\u4f20\u5165\u7684\u503c\uff0c\u9009\u53d6\u4e00\u4e2a\u5927\u4e8e\u7b49\u4e8e\u4f20\u5165\u503c\u76842\u7684n\u6b21\u5e42\u7684\u6570\u66ff\u4ee3\n        this.threshold = tableSizeFor(initialCapacity);\n    }<\/code><\/pre>\n<h5>(2).\u6307\u5b9a\u5bb9\u91cf\u7684\u6784\u9020\u5668<\/h5>\n<p>\u53ef\u4ee5\u6307\u5b9a\u957f\u5ea6\uff0c\u52a0\u8f7d\u56e0\u5b50\u4f7f\u7528\u9ed8\u8ba4\u76840.75\uff1b<\/p>\n<pre><code class=\"language-java\"> public HashMap(int initialCapacity) {\n        this(initialCapacity, DEFAULT_LOAD_FACTOR);\n    }<\/code><\/pre>\n<h5>(3).\u91c7\u7528\u9ed8\u8ba4\u53c2\u6570\u7684\u6784\u9020\u5668<\/h5>\n<pre><code class=\"language-java\">public HashMap() {\n        this.loadFactor = DEFAULT_LOAD_FACTOR; \/\/ all other fields defaulted\n    }<\/code><\/pre>\n<h5>(4).\u62f7\u8d1d\u6784\u9020\u5668<\/h5>\n<pre><code class=\"language-java\">public HashMap(Map&lt;? extends K, ? extends V&gt; m) {\n        this.loadFactor = DEFAULT_LOAD_FACTOR;\n        putMapEntries(m, false);\n    }<\/code><\/pre>\n<h4>5.\u5185\u90e8\u65b9\u6cd5<\/h4>\n<h5>(1).putMapEntries\u65b9\u6cd5<\/h5>\n<pre><code class=\"language-java\">final void putMapEntries(Map&lt;? extends K, ? extends V&gt; m, boolean evict) {\n        int s = m.size();\n        \/\/\u4f20\u5165\u7684map\u5927\u5c0f\u4e0d\u80fd\u662f0\n        if (s &gt; 0) {\n            \/\/\u5982\u679c\u8fd8\u6ca1\u6709\u5b58\u50a8\u8fc7\u4efb\u4f55\u5143\u7d20\uff0c\u624d\u80fd\u8fdb\u5165\uff0c\u5982\u679c\u5df2\u7ecf\u5b58\u50a8\u8fc7\u5143\u7d20\u4f1a\u76f4\u63a5\u89e6\u53d1resize\n            if (table == null) { \/\/ pre-size\n            \/*\n            \u5148\u4e0d\u8003\u8651\u5bb9\u91cf\u662f\u5426\u4e3a2\u7684\u5e42\uff0c\u5148\u7b97\u51fa\u5bb9\u5668\u7684\u5bb9\u91cf\n            \u56e0\u4e3a\u8f6c\u4e3a\u4e86float\uff0c\u7b97\u51fa\u6765\u7684\u6570\u4f1a\u6709\u5c0f\u6570\uff0c\u76f4\u63a5\u4e0a\u53d6\u6574\uff0c\u6240\u4ee5\u505a\u4e86\u4e2a+1\u7684\u64cd\u4f5c\n            *\/\n                float ft = ((float)s \/ loadFactor) + 1.0F;\n                \/*\n                \u5982\u679c\u5c0f\u4e8e\u6700\u5927\u5bb9\u91cf\uff0c\u90a3\u4e48\u76f4\u63a5\u5c06\u8be5\u503c\u53d6\u4e3a\u6574\u578b\uff0c\u7136\u540e\u4f7f\u7528\n                \u5982\u679c\u5927\u4e8e\u6700\u5927\u5bb9\u91cf\uff0c\u5c31\u8d4b\u503c\u4e3a\u6700\u5927\u5bb9\u91cf\n                *\/\n                int t = ((ft &lt; (float)MAXIMUM_CAPACITY) ?\n                         (int)ft : MAXIMUM_CAPACITY);\n                \/*\n                \u4e0a\u9762\u7684\u64cd\u4f5c\u5df2\u7ecf\u786e\u5b9a\u597d\u4e86\u5bb9\u91cf\u7684\u5927\u6982\u6570\u503c\n                \u56e0\u4e3a\u5bb9\u91cf\u8981\u6c42\u662f2\u7684\u6b21\u5e42\uff0c\u8fd9\u91cc\u518d\u6839\u636e\u521a\u521a\u5f97\u5230\u7684\u5927\u5c0f\uff0c\n                \u627e\u5230\u4e00\u4e2a\u5927\u4e8e\u7b49\u4e8e\u8fd9\u4e2a\u5bb9\u91cf\u76842\u6b21\u5e42\u4f5c\u4e3a\u771f\u6b63\u7684\u5bb9\u91cf\n                *\/\n                if (t &gt; threshold)\n                    threshold = tableSizeFor(t);\n            }\n            \/*\n            \u5982\u679c\u80fd\u5230\u8fd9\u91cc\uff0c\u8bf4\u660e\u5df2\u7ecf\u521d\u59cb\u5316\u8fc7\u4e86\uff0c\n            \u8fd9\u91cc\u5224\u65ad\u4f20\u5165\u7684map\u5927\u5c0f\uff0c\u662f\u5426\u5df2\u7ecf\u5927\u4e8e\u5f53\u524d\u5b58\u50a8\u7684\u6700\u5927\u503c\u4e86\n            *\/\n            else if (s &gt; threshold)\n            \/\/\u5982\u679c\u4f20\u5165map\u7684\u5927\u5c0f\u5927\u4e8e\u4e86\u5b58\u50a8\u6700\u5927\u503c\uff0c\u90a3\u4e48\u5fc5\u987b\u5148\u8fdb\u884c\u6269\u5bb9\n                resize();\n            \/\/\u5faa\u73af\u8c03\u7528putVal\u5b58\u5165\u5143\u7d20\uff0cputVal\u65b9\u6cd5\u91cc\u9762\u4e5f\u53ef\u4ee5\u8fdb\u884c\u6269\u5bb9\n            for (Map.Entry&lt;? extends K, ? extends V&gt; e : m.entrySet()) {\n                K key = e.getKey();\n                V value = e.getValue();\n                putVal(hash(key), key, value, false, evict);\n            }\n        }\n    }<\/code><\/pre>\n<p>\u603b\u7ed3\uff1a<\/p>\n<ol>\n<li>\u5982\u679c\u5f53\u524d\u7684HashMap\u662f\u7a7a\u7684\uff0c\u90a3\u4e48\u4f1a\u5148\u786e\u5b9a\u63d2\u5165\u540eHashMap\u7684\u5bb9\u91cf<\/li>\n<li>\u5982\u679c\u5f53\u524d\u7684HashMap\u4e0d\u662f\u7a7a\u7684\uff0c\u4f1a\u5148\u5224\u65ad\u662f\u5426\u9700\u8981resize<\/li>\n<li>\u63d2\u5165\u5143\u7d20\u5c31\u662f\u83b7\u53d6\u5230\u6240\u6709\u7684key-value\u5faa\u73af\u8c03\u7528putVal\u65b9\u6cd5<\/li>\n<\/ol>\n<h5>(2).putVal\u65b9\u6cd5<\/h5>\n<pre><code class=\"language-java\">final V putVal(int hash, K key, V value, boolean onlyIfAbsent,\n                   boolean evict) {\n        \/*\n        tab\u662f\u5f53\u524dHashMap\u7684\u6570\u7ec4\n        p\u662f\u8981\u64cd\u4f5c\u7684Node\n        n\u662f\u5f53\u524dHashMap\u7684Node\u6570\u7ec4\u957f\u5ea6\n        i\u662f\u64cd\u4f5c\u7684\u6570\u7ec4\u4e0b\u6807\n        *\/\n        Node&lt;K,V&gt;[] tab; Node&lt;K,V&gt; p; int n, i;\n        \/*\n        \u8fd9\u91cc\u7684\u64cd\u4f5c\u662f\u5224\u65adHashMap\u662f\u5426\u662f\u7a7a\u7684\n        \u540c\u65f6\uff0c\u628aHashMap\u7684table\u8d4b\u503c\u7ed9\u4e86tab\n        \u5c06HashMap\u7684\u957f\u5ea6\u8d4b\u503c\u7ed9\u4e86n\n        *\/\n        if ((tab = table) == null || (n = tab.length) == 0)\n            \/\/\u8c03\u7528resize\uff0c\u751f\u6210\u4e86\u4e00\u4e2atab\u6570\u7ec4\uff0c\u5e76\u4e14\u83b7\u53d6\u4e86\u4ed6\u7684\u957f\u5ea6\n            n = (tab = resize()).length;\n        \/*\n        (n-1)&amp;hash \u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u53d6\u6a21\u8fd0\u7b97\u76f8\u5f53\u4e8ehash%n\n        \u6839\u636ehash\u627e\u5230\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u4f4d\u7f6e\n        *\/\n        if ((p = tab[i = (n - 1) &amp; hash]) == null)\n            \/*\n            \u5f53\u6570\u7ec4\u4e2d\u5f53\u524d\u4f4d\u7f6e\u6ca1\u6709\u5143\u7d20\u7684\u65f6\u5019\n            \u4f1a\u5728\u8fd9\u4e2a\u4f4d\u7f6e\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u8fd9\u4e2a\u4f4d\u7f6e\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\n            *\/\n            tab[i] = newNode(hash, key, value, null);\n        else {\n            \/\/\u8fdb\u5165\u5230\u8fd9\u91cc\u8bf4\u660e\u6570\u7ec4\u4e2d\u6709\u5143\u7d20\u4e86\uff0c\u9700\u8981\u5904\u7406hash\u51b2\u7a81\n            Node&lt;K,V&gt; e; K k;\n            \/\/\u5982\u679c\u6570\u7ec4\u4e2d\u5f53\u524d\u4f4d\u7f6e\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684key\u4e0e\u65b0\u63d2\u5165\u7684\u5143\u7d20\u7684key\u76f8\u540c\n            if (p.hash == hash &amp;&amp;\n                ((k = p.key) == key || (key != null &amp;&amp; key.equals(k))))\n                \/\/\u5c06e\u6307\u5411p\n                e = p;\n            else if (p instanceof TreeNode)\n                \/\/\u5982\u679c\u5df2\u7ecf\u6811\u5316\uff0c\u8c03\u7528putTreeVal\u63d2\u5165\u65b9\u6cd5\u63d2\u5165\n                e = ((TreeNode&lt;K,V&gt;)p).putTreeVal(this, tab, hash, key, value);\n            \/\/\u5982\u679c\u8fd8\u6ca1\u6709\u88ab\u6811\u5316\uff0c\u8fd8\u662f\u94fe\u8868\n            else {\n                for (int binCount = 0; ; ++binCount) {\n                \/*\n                \u904d\u5386\u5230\u94fe\u8868\u5c3e\u90e8\uff0c\u5982\u679c\u6ca1\u6709\u53d1\u73b0\u76f8\u540c\u7684key\u7ed3\u70b9\uff0c\u91c7\u7528\u5c3e\u63d2\u6cd5\u63d2\u5165\u5143\u7d20\n                \u56e0\u4e3a\u63d2\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u5143\u7d20\uff0c\u6240\u4ee5\u8981\u5224\u65ad\u4e00\u4e0b\u5f53\u524d\u94fe\u8868\u7684\u957f\u5ea6\n                \u5982\u679c\u8d85\u8fc7\u9608\u503c\uff0c\u5c31\u4f1a\u8f6c\u4e3a\u7ea2\u9ed1\u6811\n                *\/\n                    if ((e = p.next) == null) {\n                        p.next = newNode(hash, key, value, null);\n                        if (binCount &gt;= TREEIFY_THRESHOLD - 1) \/\/ -1 for 1st\n                            treeifyBin(tab, hash);\n                        break;\n                    }\n                    \/\/\u5982\u679c\u6570\u7ec4\u4e2d\u6709\u5143\u7d20\u7684key\u4e0e\u65b0\u63d2\u5165\u7684key\u5b8c\u5168\u76f8\u540c\uff0c\u7528e\u6307\u5411p\n                    if (e.hash == hash &amp;&amp;((k = e.key) == key || (key != null &amp;&amp; key.equals(k))))\n                        break;\n                    p = e;\n                }\n            }\n            \/*\n            \u901a\u8fc7\u4e0a\u9762\u7684\u64cd\u4f5c\uff0c\u5982\u679cmap\u4e2d\u6709\u76f8\u540c\u7684key\n            \u6211\u4eec\u5c06e\u6307\u5411\u8be5\u7ed3\u70b9\n            \u5982\u679ce\u662f\u7a7a\u7684\uff0c\u8bf4\u660e\u6210\u529f\u63d2\u5165\u4e86\n            \u5982\u679ce\u4e0d\u662f\u7a7a\u7684\uff0c\u5c31\u628ae\u7684value\u6700\u4e3a\u65e7\u503c\u8fd4\u56de\uff0c\u5c06\u65b0\u63d2\u5165\u7684value\u4fdd\u5b58\u8fdb\u53bb\n            *\/\n            if (e != null) { \/\/ existing mapping for key\n                V oldValue = e.value;\n                if (!onlyIfAbsent || oldValue == null)\n                    e.value = value;\n                \/\/hashmap\u7684afterNodeAccess\u662f\u4e2a\u7a7a\u51fd\u6570\uff0cLinkedHashMap\u5b9e\u73b0\u4e86\u4ed6\n                afterNodeAccess(e);\n                return oldValue;\n            }\n        }\n        \/\/\u64cd\u4f5c\u6b21\u6570++\n        ++modCount;\n        \/\/\u63d2\u5165\u540esize\u4f1a+1\uff0c\u5e76\u4e14\u5224\u65ad\u6570\u7ec4\u957f\u5ea6\uff0c\u662f\u5426\u9700\u8981resize\n        if (++size &gt; threshold)\n            resize();\n        afterNodeInsertion(evict);\n        return null;\n    }<\/code><\/pre>\n<p>\u603b\u7ed3\uff1a<\/p>\n<ol>\n<li>\u5982\u679c\u5f53\u524d\u7684HashMap\u662f\u7a7a\u7684\uff0c\u4f1a\u8fdb\u884c\u4e00\u6b21resize<\/li>\n<li>\u5982\u679c\u4e24\u4e2akey\u76f8\u540c\uff0c\u4f1a\u7528\u65b0\u503c\u8986\u76d6\u65e7\u503c<\/li>\n<li>\u65b0\u589e\u5143\u7d20\u5728\u94fe\u8868\u4e2d\u91c7\u7528\u7684\u662f\u5c3e\u63d2\u6cd5<\/li>\n<li>\u5728\u65b0\u589e\u4e86\u4e00\u4e2a\u5143\u7d20\u540e\uff0c\u90fd\u4f1a\u901a\u8fc7size\u8fdb\u884c\u4e00\u6b21resize\u5224\u65ad<\/li>\n<\/ol>\n<h5>(3).resize\u65b9\u6cd5<\/h5>\n<pre><code class=\"language-java\">final Node&lt;K,V&gt;[] resize() {\n        \/\/\u9996\u5148\u4fdd\u5b58\u65e7\u7684\u6570\u7ec4\n        Node&lt;K,V&gt;[] oldTab = table;\n        \/\/\u83b7\u53d6\u65e7\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u5982\u679c\u662f\u7a7a\u7684\u5c31\u4e3a0\n        int oldCap = (oldTab == null) ? 0 : oldTab.length;\n        \/\/\u83b7\u53d6\u6269\u5bb9\u524dHashMap\u7684\u5bb9\u91cf\n        int oldThr = threshold;\n        \/*\n        newCap\uff1a\u65b0\u6570\u7ec4\u7684\u957f\u5ea6\n        newThr\uff1a\u65b0HashMap\u7684\u6700\u5927\u5bb9\u91cf\n        *\/\n        int newCap, newThr = 0;\n        \/\/\u5224\u65ad\u65e7HashMap\u662f\u5426\u5b58\u5165\u4e86\u5143\u7d20\n        if (oldCap &gt; 0) {\n        \/\/\u65e7\u6570\u7ec4\u7684\u957f\u5ea6\u5927\u4e8e\u7b49\u4e8e\u6700\u5927\u5bb9\u91cf\uff0c\u9608\u503c\u5c31\u4f1a\u8bbe\u7f6e\u4e3aInteger\u7684\u6700\u5927\u503c\n            if (oldCap &gt;= MAXIMUM_CAPACITY) {\n                threshold = Integer.MAX_VALUE;\n                return oldTab;\n            }\n            \/*\n            \u9996\u5148\uff0c\u65b0\u5bb9\u91cf\u4f1a\u8bbe\u7f6e\u4e3a\u65e7\u5bb9\u91cf\u7684\u4e24\u500d\n            \u7136\u540e\uff0c\u5224\u65ad\u6269\u5bb9\u540e\u7684\u65b0\u5bb9\u91cf\u662f\u5426&lt;2^30\n            \u5e76\u4e14\u65e7\u5bb9\u91cf\u662f\u5426&gt;=16\n            *\/\n            else if ((newCap = oldCap &lt;&lt; 1) &lt; MAXIMUM_CAPACITY &amp;&amp;\n                     oldCap &gt;= DEFAULT_INITIAL_CAPACITY)\n                \/\/\u4ee5\u4e0a\u6761\u4ef6\u90fd\u6ee1\u8db3\u7684\u8bdd\uff0c\u9608\u503c\u4e5f\u4f1a\u7ffb\u500d\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        @SuppressWarnings({&quot;rawtypes&quot;,&quot;unchecked&quot;})\n        \/*\n        \u901a\u8fc7\u4e0a\u9762\u7684\u64cd\u4f5c\uff0c\u5df2\u7ecf\u786e\u5b9a\u597d\u4e86\u5bb9\u91cf\u548c\u6269\u5bb9\u9608\u503c\n        1.\u5982\u679c\u6269\u5bb9\u524d\u7684HashMap\u6ca1\u6709\u5b58\u5165\u5143\u7d20\uff0c\u5c31\u6309\u7167\u9ed8\u8ba4\u503c\u8fdb\u884c\u6269\u5bb9\n        2.\u5982\u679c\u6269\u5bb9\u524d\u7684HashMap\u5df2\u7ecf\u5b58\u5165\u4e86\u5143\u7d20\uff0c\u76f4\u63a5\u6269\u5bb9\u4e24\u500d\uff0c\u5982\u679c\u65e7\u6570\u7ec4\u7684\u957f\u5ea6\u5927\u4e8e\u4e8616\uff0c\u90a3\u4e48threshold\u4e5f\u6269\u5927\u4e24\u500d\n        3.\u5982\u679c\u6b64\u65f6\u7684\u6570\u7ec4\u957f\u5ea6\u5df2\u7ecf\u5230\u4e86HashMap\u7684\u6700\u5927\u5bb9\u91cf\u65f6\uff0c\u4f1a\u5c06threshol\u8bbe\u7f6e\u4e3aInteger\u7684\u6700\u5927\u503c\n        *\/\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                \/\/\u5c06\u539f\u6765map\u4e2d\u4e0d\u662fnull\u7684\u5143\u7d20rehash\u540e\u518d\u653e\u5230\u65b0\u7684\u6570\u7ec4\u4e2d\n                if ((e = oldTab[j]) != null) {\n                    oldTab[j] = null;\n                    \/\/\u5982\u679c\u5f53\u524d\u4f4d\u7f6e\u5c31\u4e00\u4e2a\u5143\u7d20\uff0c\u90a3\u4e48\u76f4\u63a5\u653e\u5230\u65b0\u6570\u7ec4\u91cc\u9762\n                    if (e.next == null)\n                        newTab[e.hash &amp; (newCap - 1)] = e;\n                    \/*\n                    \u8fd9\u91cc\u8868\u793a\u8fd9\u4e2a\u4f4d\u7f6e\u4e0d\u6b62\u4e00\u4e2a\u7ed3\u70b9\n                    \u5224\u65ad\u8fd9\u4e2a\u7ed3\u70b9\u662f\u5426\u5df2\u7ecf\u6811\u578b\u5316\u4e86\n                    \u5982\u679c\u5df2\u7ecf\u6811\u578b\u5316\uff0c\u5c06\u8fd9\u4e2a\u6811\u4e0a\u7684\u7ed3\u70b9rehash\u540e\u5b58\u5165\n                    *\/\n                    else if (e instanceof TreeNode)\n                    ((TreeNode&lt;K,V&gt;)e).split(this, newTab, j, oldCap);\n                    else { \/\/ preserve order\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    \/*\n    \u5982\u679c\u662ftrue,\u8868\u793a(e.hash&amp;(newCap-1))\u8fd8\u4f1a\u548c(e.hash&amp;(oldCap-1))\u4e00\u6837\n    \u56e0\u4e3a\u65b0\u5bb9\u91cf\u662f\u65e7\u5bb9\u91cf\u7684\u4e24\u500d\uff0c\u800c\u5bb9\u91cf\u90fd\u662f2\u7684\u6b21\u5e42\n    \u6240\u4ee5\u8bf4\u65b0\u5bb9\u91cf\u5c31\u662f\u65e7\u5bb9\u91cf\u7684\u4e8c\u8fdb\u5236\u4f4d\u9ad8\u4f4d\u5de6\u79fb\u4e86\u4e00\u4f4d\n    \u90a3(e.hash &amp; oldCap) == 0 \u5c31\u4ee3\u8868(e.hash &amp; (newCap-1)) = (e.hash &amp; (oldCap-1))\n    \u8fd9\u5c31\u610f\u5473\u7740\u8be5\u5143\u7d20\u5728\u65b0\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\u4e0d\u4f1a\u53d8\uff0c\u56e0\u6b64\uff0c\u5c06\u5176\u7ec4\u6210\u4e00\u4e2a\u94fe\u8868\u5373\u53ef\n    *\/\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    \/*\n    \u800c\u5bf9\u4e8e\u975e0\u7684key\uff0c\u90a3\u4ed6\u7684\u4f4d\u7f6e\u5728\u65b0\u6570\u7ec4\u4e2d\u9700\u8981\u66f4\u65b0\uff0c\u5c31\u9700\u8981\u5728\u65b0\u7684\u4f4d\u7f6e\u518d\u7ec4\u5408\u94fe\u8868\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                        \/\/\u4e0d\u9700\u8981\u4fee\u6539\u7ed3\u70b9\u4f4d\u7f6e\uff0c\u76f4\u63a5\u4fdd\u5b58\u5373\u53ef\n                            loTail.next = null;\n                            newTab[j] = loHead;\n                        }\n                        if (hiTail != null) {\n                            hiTail.next = null;\n                            \/\/\u9700\u8981\u91cd\u65b0\u8ba1\u7b97\u4f4d\u7f6e\uff0c\u518d\u4fdd\u5b58\n                            newTab[j + oldCap] = hiHead;\n                        }\n                    }\n                }\n            }\n        }\n        return newTab;\n    }<\/code><\/pre>\n<h5>(4).put\u65b9\u6cd5<\/h5>\n<p>HashMap\u7684put\u65b9\u6cd5\u5b9e\u73b0\u4e86Map\u7684put\u63a5\u53e3\uff0c\u5176\u5b9e\u73b0\u4e5f\u662f\u8c03\u7528\u7684HashMap\u7684putVal\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">public V put(K key, V value) {\n        return putVal(hash(key), key, value, false, true);\n    }<\/code><\/pre>\n<h5>(5).containsKey\u65b9\u6cd5<\/h5>\n<p>\u6b64\u65b9\u6cd5\u7528\u6765\u5224\u65adkey\u662f\u5426\u5b58\u5728\uff0c\u5728\u67e5\u627e\u524d\u5fc5\u987b\u6ee1\u8db3\u4e09\u4e2a\u6761\u4ef6<\/p>\n<ol>\n<li>(tab=table)!=null \u6570\u7ec4\u4e0d\u80fd\u4e3a\u7a7a<\/li>\n<li>(n=tab.length)&gt;0 \u6570\u7ec4\u957f\u5ea6\u5927\u4e8e0<\/li>\n<li>\uff08first = tab[(n-1)&amp;hash]\uff09!=null \u5934\u7ed3\u70b9\u4e0d\u4e3a\u7a7a<\/li>\n<\/ol>\n<p>\u53ea\u6709\u4ee5\u4e0a\u4e09\u4e2a\u6761\u4ef6\u90fd\u6ee1\u8db3\u4e86\u624d\u53ef\u80fd\u5b58\u5728\u5f85\u67e5\u627e\u7684\u5143\u7d20\uff0c\u5426\u5219\u8fd4\u56denull<\/p>\n<pre><code class=\"language-java\">public boolean containsKey(Object key) {\n        return getNode(hash(key), key) != null;\n    }\n\nfinal Node&lt;K,V&gt; getNode(int hash, Object key) {\n        Node&lt;K,V&gt;[] tab; Node&lt;K,V&gt; first, e; int n; K k;\n        if ((tab = table) != null &amp;&amp; (n = tab.length) &gt; 0 &amp;&amp;\n            (first = tab[(n - 1) &amp; hash]) != null) {\n            if (first.hash == hash &amp;&amp; \/\/ always check first node\n                ((k = first.key) == key || (key != null &amp;&amp; key.equals(k))))\n                return first;\n            if ((e = first.next) != null) {\n                if (first instanceof TreeNode)\n                    return ((TreeNode&lt;K,V&gt;)first).getTreeNode(hash, key);\n                do {\n                    if (e.hash == hash &amp;&amp;\n                        ((k = e.key) == key || (key != null &amp;&amp; key.equals(k))))\n                        return e;\n                } while ((e = e.next) != null);\n            }\n        }\n        return null;\n    }<\/code><\/pre>\n<h3>\u4e09.TreeMap<\/h3>\n<h4>1.\u6982\u8ff0<\/h4>\n<ol>\n<li>TreeMap\u7684\u5e95\u5c42\u901a\u8fc7\u7ea2\u9ed1\u6811\u5b9e\u73b0<\/li>\n<li>TreeMap\u7684\u8fed\u4ee3\u5668\u91c7\u7528\u7684\u662f\u4e2d\u5e8f\u904d\u5386\u7684\u65b9\u5f0f<\/li>\n<\/ol>\n<h4>2.\u7ea2\u9ed1\u6811\u7b80\u4ecb<\/h4>\n<p>\u7ea2\u9ed1\u6811\u53c8\u79f0<strong>\u7ea2-\u9ed1\u4e8c\u53c9\u6811<\/strong>\uff0c\u5b83\u9996\u5148\u662f\u4e00\u9897\u4e8c\u53c9\u6811\uff0c\u5b83\u5177\u6709\u4e8c\u53c9\u6811\u7684\u6240\u6709\u7279\u6027\uff0c\u540c\u65f6\u7ea2\u9ed1\u6811\u66f4\u662f\u4e00\u9897\u81ea\u5e73\u8861\u7684\u6392\u5e8f\u4e8c\u53c9\u6811<\/p>\n<h5>(1).\u5e73\u8861\u4e8c\u53c9\u6811\u7684\u7279\u6027\uff1a<\/h5>\n<p>1.\u662f\u4e00\u9897\u7a7a\u6811\u6216\u5b83\u7684\u5de6\u53f3\u4e24\u4e2a\u5b50\u6811\u7684\u9ad8\u5ea6\u5dee\u7edd\u5bf9\u503c\u4e0d\u8d85\u8fc71\u3002<br \/>\n2.\u5de6\u53f3\u4e24\u4e2a\u5b50\u6811\u90fd\u662f\u5e73\u8861\u4e8c\u53c9\u6811\u3002<\/p>\n<h5>(2).\u7ea2\u9ed1\u6811\u7684\u89c4\u5219\uff1a<\/h5>\n<ol>\n<li>\u6bcf\u4e2a\u8282\u70b9\u90fd\u53ea\u80fd\u662f\u7ea2\u8272\u6216\u8005\u9ed1\u8272\u3002<\/li>\n<li>\u6839\u7ed3\u70b9\u662f\u9ed1\u8272\u3002<\/li>\n<li>\u6bcf\u4e2a\u53f6\u7ed3\u70b9(NIL\u7ed3\u70b9\uff0c\u7a7a\u7ed3\u70b9)\u662f\u9ed1\u8272\u7684\u3002<\/li>\n<li>\u5982\u679c\u4e00\u4e2a\u7ed3\u70b9\u662f\u7ea2\u8272\u7684\uff0c\u90a3\u4e2a\u5b83\u7684\u4e24\u4e2a\u5b50\u7ed3\u70b9\u90fd\u662f\u9ed1\u8272\u7684\uff0c\u4e00\u6761\u8def\u5f84\u4e0a\u4e0d\u80fd\u51fa\u73b0\u76f8\u90bb\u7684\u4e24\u4e2a\u7ea2\u8272\u7ed3\u70b9\u3002<\/li>\n<li>\u4ece\u4efb\u4e00\u7ed3\u70b9\u5230\u5176\u6bcf\u4e2a\u53f6\u5b50\u7684\u6240\u6709\u8def\u5f84\u90fd\u5305\u542b\u76f8\u540c\u6570\u76ee\u7684\u9ed1\u8272\u7ed3\u70b9\u3002<\/li>\n<\/ol>\n<h4>3.\u7279\u70b9<\/h4>\n<ol>\n<li>TreeMap\u5b58\u50a8\u662f\u65e0\u5e8f\u7684<\/li>\n<li>TreeMap\u4e0d\u5141\u8bb8\u91cd\u590d\u5b58\u5165\u76f8\u540c\u7684\u5143\u7d20<\/li>\n<li>TreeMap\u53ef\u4ee5\u6392\u5e8f<\/li>\n<\/ol>\n<h4>4.\u6392\u5e8f\u89c4\u5219<\/h4>\n<ol>\n<li>TreeMap\u4e2dkey\u53ef\u4ee5\u81ea\u52a8\u5bf9String\u7c7b\u578b\u62168\u5927\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5305\u88c5\u7c7b\u8fdb\u884c\u6392\u5e8f<\/li>\n<li>TreeMap\u65e0\u6cd5\u76f4\u63a5\u5bf9\u81ea\u5b9a\u4e49\u7c7b\u578b\u8fdb\u884c\u6392\u5e8f<\/li>\n<\/ol>\n<h4>5.\u81ea\u5b9a\u4e49\u7c7b\u578b\u5982\u4f55\u6392\u5e8f<\/h4>\n<p>\u5982\u679c\u76f4\u63a5\u5c06\u81ea\u5b9a\u4e49\u7c7b\u578b\u6dfb\u52a0\u5230TreeMap\u4e2d\uff0ckey\u4f1a\u62a5 java.lang.ClassCastException<br \/>\n<strong>\u539f\u56e0\uff1a<\/strong>\u81ea\u5b9a\u4e49\u6ca1\u6709\u5b9e\u73b0 java.lang.Comparable \u63a5\u53e3(\u6b64\u65f6\uff0c\u4f7f\u7528\u7684\u662fTreeMap\u7684\u65e0\u53c2\u6784\u9020\u5668)\uff0c\u5982\u679c\u60f3\u8981\u6392\u5e8f\uff0c\u5fc5\u987b\u8981\u6307\u5b9a\u6392\u5e8f\u89c4\u5219\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u4e24\u79cd\u89e3\u51b3\u529e\u6cd5<\/p>\n<h5>(1).\u65b9\u6cd5\u4e00<\/h5>\n<p>\u653e\u5728\u96c6\u5408\u4e2d\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\u5b9e\u73b0 java.lang.Comparable \u63a5\u53e3\uff0c\u91cd\u5199 compareTo \u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"language-java\">public class cat implements Comparable&lt;cat&gt;{\n    int age ;\n\n    public cat(int age){\n        this.age = age;\n    }\n\n    @Override\n    public int compareTo(cat c) {\n        \/\/\u6309\u7167\u5e74\u9f84\u5347\u5e8f\u6392\u5e8f\n        return this.age-c.age;\n    }\n\n}<\/code><\/pre>\n<h5>(2).\u65b9\u6cd5\u4e8c<\/h5>\n<p>\u9009\u62e9\u5e26\u6bd4\u8f83\u5668\u53c2\u6570\u7684\u6784\u9020\u5668\uff0c\u5e76\u91cd\u5199\u6bd4\u8f83\u5668\u4e2d\u7684 compare\u65b9\u6cd5\u3002<br \/>\n\u5982\u679c\u91c7\u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5728\u4f20\u9012\u6bd4\u8f83\u5668\u53c2\u6570\u7ed9\u6784\u9020\u5668\u65f6\u6709\u4ee5\u4e0b3\u79cd\u5199\u6cd5<\/p>\n<ol>\n<li>\n<p>\u5b9a\u4e49\u4e00\u4e2a Comparator \u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b<\/p>\n<pre><code class=\"language-java\">\/\/\u81ea\u5b9a\u4e49Cat\u7c7b\npublic class cat{\nint age ;\n\npublic cat(int age){\n    this.age = age;\n}\n}\n\/\/\u63a5\u53e3\u5b9e\u73b0\u7c7b\nclass MyCat implements Comparator&lt;Cat&gt;{\n    @Override\n    public int compare(Cat o1, Cat o2) {\n        return o1.age-o2.age;\n    }\n}\n\/\/\u521b\u5efa\u53ef\u6392\u5e8f\u7684TreeMap\npublic static void main(String[] args) {\n    TreeMap&lt;Cat, Object&gt; TreeMap = new TreeMap&lt;&gt;(new MyCat());\n}<\/code><\/pre>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u533f\u540d\u5185\u90e8\u7c7b<\/p>\n<pre><code class=\"language-java\">\/\/\u81ea\u5b9a\u4e49Cat\u7c7b\npublic class cat{\nint age ;\n\npublic cat(int age){\n    this.age = age;\n}\n}\n\/\/\u521b\u5efaTreeMap\npublic static void main(String[] args) {\n    TreeMap&lt;Cat, Object&gt; TreeMap = new TreeMap&lt;&gt;(new Comparator&lt;Cat&gt;() {\n        @Override\n        public int compare(Cat o1, Cat o2) {\n            return o1.age - o2.age;\n        }\n    });\n}<\/code><\/pre>\n<\/li>\n<li>\n<p>\u4f7f\u7528lambda\u8868\u8fbe\u5f0f(Comparator\u662f\u51fd\u6570\u5f0f\u63a5\u53e3)<\/p>\n<pre><code class=\"language-java\">\/\/\u81ea\u5b9a\u4e49Cat\u7c7b\npublic class cat{\nint age ;\n\npublic cat(int age){\n    this.age = age;\n}\n}\n\/\/\u521b\u5efaTreeMap\npublic static void main(String[] args) {\n    TreeMap&lt;Cat, Object&gt; TreeMap = new TreeMap&lt;&gt;((o1,o2)-&gt;o1.age-o2.age);\n}\n\/\/\u6216\u8005\u662f\npublic static void main(String[] args) {\n    TreeMap&lt;Cat, Object&gt; TreeMap = new TreeMap&lt;&gt;(Comparator.comparingInt(o -&gt; o.age));\n}<\/code><\/pre>\n<\/li>\n<\/ol>\n<h5>(3).\u5982\u4f55\u9009\u62e9\u89e3\u51b3\u65b9\u6848<\/h5>\n<ol>\n<li>\u5f53\u6bd4\u8f83\u89c4\u5219\u4e0d\u4f1a\u53d1\u751f\u6539\u53d8\uff0c\u6216\u8005\u6bd4\u8f83\u89c4\u5219\u53ea\u6709\u4e00\u4e2a\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u65b9\u6cd5\u4e00<\/li>\n<li>\u5f53\u6bd4\u8f83\u89c4\u5219\u6709\u591a\u4e2a\uff0c\u5e76\u4e14\u9700\u8981\u5728\u591a\u4e2a\u89c4\u5219\u4e2d\u5207\u6362\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u65b9\u6cd5\u4e8c<\/li>\n<\/ol>\n<h4>6.\u5185\u90e8\u53c2\u6570<\/h4>\n<pre><code class=\"language-java\">\/\/\u6392\u5e8f\u7528\u7684\u6bd4\u8f83\u5668\nprivate final Comparator&lt;? super K&gt; comparator;\n\/\/\u5934\u7ed3\u70b9\nprivate transient Entry&lt;K,V&gt; root;\n\/\/\u5927\u5c0f\nprivate transient int size = 0;\n\/\/\u4fee\u6539\u6b21\u6570\nprivate transient int modCount = 0;<\/code><\/pre>\n<h4>7.put\u65b9\u6cd5<\/h4>\n<pre><code class=\"language-java\">public V put(K key, V value) {\n        \/\/\u9996\u5148\u83b7\u53d6\u5230\u5934\u7ed3\u70b9\n        Entry&lt;K,V&gt; t = root;\n        \/\/\u5982\u679c\u5934\u7ed3\u70b9\u662f\u7a7a\u7684\uff0c\u8c03\u7528\u6bd4\u8f83\u5668\uff0c\u68c0\u67e5key\u662f\u4e0d\u662f\u7a7a\u7684\n        if (t == null) {\n            compare(key, key); \/\/ type (and possibly null) check\n            \/\/\u56e0\u4e3a\u5934\u7ed3\u70b9\u662f\u7a7a\u7684\uff0c\u6240\u4ee5\u628a\u8fd9\u4e2a\u7ed3\u70b9\u4fdd\u5b58\u4e3a\u5934\u7ed3\u70b9\n            \/\/\u56e0\u4e3a\u6ca1\u6709\u7236\u7ed3\u70b9\uff0c\u6240\u4ee5\u4f20\u5165\u4e86\u4e00\u4e2anull\n            root = new Entry&lt;&gt;(key, value, null);\n            \/\/\u957f\u5ea6\u589e\u52a0\u5230\u4e861\n            size = 1;\n            \/\/\u64cd\u4f5c\u6b21\u6570++\n            modCount++;\n            return null;\n        }\n        int cmp;\n        Entry&lt;K,V&gt; parent;\n        \/\/ split comparator and comparable paths\n        \/\/\u83b7\u53d6\u5185\u90e8\u7684\u6bd4\u8f83\u5668\n        Comparator&lt;? super K&gt; cpr = comparator;\n        \/\/\u5982\u679c\u6bd4\u8f83\u5668\u4e0d\u662f\u7a7a\uff0c\u6309\u7740\u6bd4\u8f83\u5668\u7684\u89c4\u5219\u6267\u884c\n        if (cpr != null) {\n            do {\n                \/\/\u5c06\u6839\u8282\u70b9\u8d4b\u503c\u7ed9parent\n                parent = t;\n                \/\/\u6bd4\u8f83key\u4e0e\u6839\u8282\u70b9\u7684\u5927\u5c0f\n                cmp = cpr.compare(key, t.key);\n                if (cmp &lt; 0)\n                    \/\/\u5982\u679ckey&lt;\u6839\u8282\u70b9\u7684key\uff0c\u6307\u5411\u5de6\u5b50\u6811\n                    t = t.left;\n                else if (cmp &gt; 0)\n                    \/\/\u5982\u679ckey&gt;\u6839\u8282\u70b9\u7684key\uff0c\u6307\u5411\u53f3\u5b50\u6811\n                    t = t.right;\n                else\n                \/\/\u5982\u679c\u76f8\u540c\u8986\u76d6value\n                    return t.setValue(value);\n            } while (t != null);\n        }\n        \/\/\u6ca1\u6709\u8bbe\u7f6e\u6bd4\u8f83\u5668\uff0c\u91c7\u7528\u81ea\u7136\u6392\u5e8f\u89c4\u5219\n        else {\n            if (key == null)\n                throw new NullPointerException();\n            @SuppressWarnings(&quot;unchecked&quot;)\n            \/\/\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\n                Comparable&lt;? super K&gt; k = (Comparable&lt;? super K&gt;) key;\n            do {\n                \/\/\u6b64\u5904\u4e0e\u4e0a\u65b9\u903b\u8f91\u4e00\u81f4\n                parent = t;\n                cmp = k.compareTo(t.key);\n                if (cmp &lt; 0)\n                    t = t.left;\n                else if (cmp &gt; 0)\n                    t = t.right;\n                else\n                    return t.setValue(value);\n            } while (t != null);\n        }\n        \/\/\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ed3\u70b9\uff0c\u5e76\u6307\u5b9a\u7236\u8282\u70b9\n        Entry&lt;K,V&gt; e = new Entry&lt;&gt;(key, value, parent);\n        \/\/\u6839\u636e\u6bd4\u8f83\u7ed3\u679c\uff0c\u51b3\u5b9a\u65b0\u7ed3\u70b9\u4e3a\u7236\u8282\u70b9\u7684\u5de6\u5b69\u5b50\u8fd8\u662f\u53f3\u5b69\u5b50\n        if (cmp &lt; 0)\n            parent.left = e;\n        else\n            parent.right = e;\n        \/\/\u65b0\u63d2\u5165\u7ed3\u70b9\u540e\u9700\u8981\u91cd\u65b0\u8c03\u6574\u7ea2\u9ed1\u6811\n        fixAfterInsertion(e);\n        size++;\n        modCount++;\n        return null;\n    }<\/code><\/pre>\n<p>\u603b\u7ed3\uff1a<\/p>\n<ol>\n<li>TreeMap\u7684put\u65b9\u6cd5\u4e3b\u8981\u5206\u4e3a<strong>\u6784\u5efa\u6392\u5e8f\u4e8c\u53c9\u6811<\/strong>\u548c<strong>\u5e73\u8861\u4e8c\u53c9\u6811\u4e24\u4e2a\u6b65\u9aa4<\/strong><\/li>\n<li>\u6dfb\u52a0\u7ed3\u70b9\u7684\u8fc7\u7a0b\u5982\u4e0b\n<ol>\n<li>\u4ee5\u6839\u8282\u70b9\u4e3a\u521d\u59cb\u7ed3\u70b9\u8fdb\u884c\u68c0\u7d22<\/li>\n<li>\u901a\u8fc7\u6bd4\u8f83\u5668\uff0c\u4e0e\u5f53\u524d\u7ed3\u70b9\u8fdb\u884c\u6bd4\u8f83\uff0c\u5982\u679c\u65b0\u589e\u7ed3\u70b9\u503c\u6bd4\u8f83\u5927\uff0c\u90a3\u4e48\u5c31\u4ee5\u5f53\u524d\u7ed3\u70b9\u7684\u53f3\u5b50\u7ed3\u70b9\u4f5c\u4e3a\u65b0\u7684\u5f53\u524d\u7ed3\u70b9\uff0c\u5426\u5219\u4ee5\u5de6\u5b50\u7ed3\u70b9\u4f5c\u4e3a\u65b0\u7684\u5f53\u524d\u7ed3\u70b9<\/li>\n<li>\u627e\u5230\u5408\u9002\u7684\u53f6\u5b50\u7ed3\u70b9\u540e\uff0c\u5c06\u65b0\u589e\u7ed3\u70b9\u4e0e\u8be5\u7ed3\u70b9\u6bd4\u8f83\uff0c\u5982\u679c\u65b0\u589e\u7ed3\u70b9\u5927\uff0c\u5219\u6dfb\u52a0\u4e3a\u53f3\u5b50\u7ed3\u70b9\uff0c\u5426\u5219\u4e3a\u5de6\u5b50\u7ed3\u70b9<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<h3>\u56db.LinkedHashMap<\/h3>\n<h4>1.\u6982\u8ff0<\/h4>\n<p>HashMap\u548c\u53cc\u5411\u94fe\u8868\u7ed3\u5408\u5728\u4e00\u8d77\u5c31\u662fLinkedHashMap,\u5b83\u662f\u4e00\u4e2a\u5c06\u6240\u6709\u7ed3\u70b9\u94fe\u5165\u4e00\u4e2a\u53cc\u5411\u94fe\u8868HashMap,\u56e0\u5176\u662fHashMap\u7684\u5b50\u7c7b\uff0c\u6240\u4ee5\u5b83\u81ea\u7136\u62e5\u6709HashMap\u7684\u6240\u6709\u7279\u6027\u3002<\/p>\n<h4>2.\u7279\u70b9<\/h4>\n<p>LinkedHashMap\u589e\u52a0\u4e86\u65f6\u95f4\u548c\u7a7a\u95f4\u4e0a\u7684\u5f00\u9500\uff0c\u4f46\u662f\u5b83\u901a\u8fc7\u7ef4\u62a4\u4e00\u4e2a\u989d\u5916\u7684\u53cc\u5411\u94fe\u8868\u4fdd\u8bc1\u4e86\u6570\u636e\u7684\u63d2\u5165\u987a\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00.Map 1.\u7279\u70b9 Map \u548c Collection \u6ca1\u6709\u7ee7\u627f\u5173\u7cfb Map \u4ee5(key,value)\u7684\u5f62\u5f0f\u5b58\u50a8\u6570\u636e\uff1a(\u952e\u503c\u5bf9), &#8230;<\/p>","protected":false},"author":1,"featured_media":639,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[51,55,54,52,53],"_links":{"self":[{"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/posts\/632"}],"collection":[{"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/comments?post=632"}],"version-history":[{"count":19,"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/posts\/632\/revisions"}],"predecessor-version":[{"id":780,"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/posts\/632\/revisions\/780"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/media\/639"}],"wp:attachment":[{"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/media?parent=632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/categories?post=632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bigwer.cn\/index.php\/wp-json\/wp\/v2\/tags?post=632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}