{"id":1987,"date":"2018-10-26T20:13:16","date_gmt":"2018-10-26T12:13:16","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=1987"},"modified":"2018-10-26T22:15:16","modified_gmt":"2018-10-26T14:15:16","slug":"map-%e7%bb%bc%e8%bf%b0%ef%bc%88%e4%b8%80%ef%bc%89%ef%bc%9a%e5%bd%bb%e5%a4%b4%e5%bd%bb%e5%b0%be%e7%90%86%e8%a7%a3-hashmap","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/1987","title":{"rendered":"Map \u7efc\u8ff0\uff08\u4e00\uff09\uff1a\u5f7b\u5934\u5f7b\u5c3e\u7406\u89e3 HashMap"},"content":{"rendered":"<div class=\"article-header-box\">\n<div class=\"article-header\">\n<div class=\"article-title-box\"><\/div>\n<\/div>\n<\/div>\n<article>\n<div id=\"article_content\" class=\"article_content clearfix csdn-tracking-statistics\" data-pid=\"blog\" data-mod=\"popu_307\" data-dsm=\"post\">\n<div class=\"article-copyright\"><strong>\u6458\u8981\uff1a<\/strong><\/div>\n<div class=\"markdown_views\">\n<p>\u3000\u3000HashMap\u662fMap\u65cf\u4e2d\u6700\u4e3a\u5e38\u7528\u7684\u4e00\u79cd\uff0c\u4e5f\u662f Java Collection Framework \u7684\u91cd\u8981\u6210\u5458\u3002\u672c\u6587\u9996\u5148\u7ed9\u51fa\u4e86 HashMap \u7684\u5b9e\u8d28\u5e76\u6982\u8ff0\u4e86\u5176\u4e0e Map\u3001HashSet \u7684\u5173\u7cfb\uff0c\u7d27\u63a5\u7740\u7ed9\u51fa\u4e86 HashMap \u5728 JDK \u4e2d\u7684\u5b9a\u4e49\uff0c\u5e76\u7ed3\u5408\u6e90\u7801\u5206\u6790\u4e86\u5176\u56db\u79cd\u6784\u9020\u65b9\u5f0f\u3002\u6700\u540e\uff0c\u901a\u8fc7\u5bf9 HashMap \u7684\u6570\u636e\u7ed3\u6784\u3001\u5b9e\u73b0\u539f\u7406\u3001\u6e90\u7801\u5b9e\u73b0\u4e09\u4e2a\u65b9\u9762\u7684\u5256\u6790\uff0c\u6df1\u5165\u5230\u5b83\u5e95\u5c42 Hash \u5b58\u50a8\u673a\u5236\uff0c\u89e3\u91ca\u4e86\u5176\u5e95\u5c42\u6570\u7ec4\u957f\u5ea6\u603b\u662f 2 \u7684 n \u6b21\u65b9\u7684\u539f\u56e0\uff0c\u4e5f\u63ed\u793a\u4e86\u5176\u5feb\u901f\u5b58\u53d6\u3001\u6269\u5bb9\u53ca\u6269\u5bb9\u540e\u7684\u91cd\u54c8\u5e0c\u7684\u539f\u7406\u4e0e\u5b9e\u73b0\u3002<\/p>\n<hr \/>\n<p><strong>\u53cb\u60c5\u63d0\u793a\uff1a<\/strong><\/p>\n<p>\u672c\u6587\u6240\u6709\u5173\u4e8eHashMap\u7684\u6e90\u7801\u90fd\u662f\u57fa\u4e8e<span style=\"color: red;\"><b>\u00a0JDK 1.6<\/b><\/span>\u00a0\u7684\uff0c\u4e0d\u540c JDK \u7248\u672c\u4e4b\u95f4\u4e5f\u8bb8\u4f1a\u6709\u4e9b\u8bb8\u5dee\u5f02\uff0c\u4f46\u4e0d\u5f71\u54cd\u6211\u4eec\u5bf9 HashMap \u7684\u6570\u636e\u7ed3\u6784\u3001\u539f\u7406\u7b49\u6574\u4f53\u7684\u628a\u63e1\u548c\u4e86\u89e3\u3002<\/p>\n<p><span style=\"color: red;\"><b>HashMap \u7684\u76f4\u63a5\u5b50\u7c7bLinkedHashMap\u7ee7\u627f\u4e86HashMap\u7684\u6240\u7528\u7279\u6027\uff0c\u5e76\u4e14\u8fd8\u901a\u8fc7\u989d\u5916\u7ef4\u62a4\u4e00\u4e2a\u53cc\u5411\u94fe\u8868\u4fdd\u6301\u4e86\u6709\u5e8f\u6027, \u901a\u8fc7\u5bf9\u6bd4LinkedHashMap\u548cHashMap\u7684\u5b9e\u73b0\u6709\u52a9\u4e8e\u66f4\u597d\u7684\u7406\u89e3HashMap\u3002<\/b><\/span>\u5173\u4e8eLinkedHashMap\u7684\u66f4\u591a\u4ecb\u7ecd\uff0c\u8bf7\u53c2\u89c1\u6211\u7684\u53e6\u4e00\u7bc7\u535a\u6587<a href=\"http:\/\/blog.csdn.net\/justloveyou_\/article\/details\/71713781\" target=\"_blank\" rel=\"nofollow noopener\">\u300aMap \u7efc\u8ff0\uff08\u4e8c\uff09\uff1a\u5f7b\u5934\u5f7b\u5c3e\u7406\u89e3 LinkedHashMap\u300b<\/a>\uff0c\u6b22\u8fce\u6307\u6b63~<\/p>\n<hr \/>\n<p><strong>\u7248\u6743\u58f0\u660e\uff1a<\/strong><\/p>\n<p>\u672c\u6587\u539f\u521b\u4f5c\u8005\uff1a<a href=\"http:\/\/my.csdn.net\/justloveyou_\" target=\"_blank\" rel=\"nofollow noopener\">\u4e66\u5446\u5b50Rico<\/a><br \/>\n\u4f5c\u8005\u535a\u5ba2\u5730\u5740\uff1a<a href=\"http:\/\/blog.csdn.net\/justloveyou_\/\" target=\"_blank\" rel=\"nofollow noopener\">http:\/\/blog.csdn.net\/justloveyou_\/<\/a><\/p>\n<hr \/>\n<h2 id=\"\u4e00-hashmap-\u6982\u8ff0\"><a name=\"t0\"><\/a>\u4e00. HashMap \u6982\u8ff0<\/h2>\n<p>Map \u662f Key-Value \u5bf9\u6620\u5c04\u7684\u62bd\u8c61\u63a5\u53e3\uff0c\u8be5\u6620\u5c04\u4e0d\u5305\u62ec\u91cd\u590d\u7684\u952e\uff0c\u5373\u4e00\u4e2a\u952e\u5bf9\u5e94\u4e00\u4e2a\u503c\u3002HashMap \u662f Java Collection Framework \u7684\u91cd\u8981\u6210\u5458\uff0c\u4e5f\u662fMap\u65cf(\u5982\u4e0b\u56fe\u6240\u793a)\u4e2d\u6211\u4eec\u6700\u4e3a\u5e38\u7528\u7684\u4e00\u79cd\u3002\u7b80\u5355\u5730\u8bf4\uff0cHashMap \u662f\u57fa\u4e8e\u54c8\u5e0c\u8868\u7684 Map \u63a5\u53e3\u7684\u5b9e\u73b0\uff0c\u4ee5 Key-Value \u7684\u5f62\u5f0f\u5b58\u5728\uff0c\u5373\u5b58\u50a8\u7684\u5bf9\u8c61\u662f Entry (\u540c\u65f6\u5305\u542b\u4e86 Key \u548c Value) \u3002\u5728HashMap\u4e2d\uff0c\u5176\u4f1a\u6839\u636ehash\u7b97\u6cd5\u6765\u8ba1\u7b97key-value\u7684\u5b58\u50a8\u4f4d\u7f6e\u5e76\u8fdb\u884c\u5feb\u901f\u5b58\u53d6\u3002\u7279\u522b\u5730\uff0c<span style=\"color: red;\"><b>HashMap\u6700\u591a\u53ea\u5141\u8bb8\u4e00\u6761Entry\u7684\u952e\u4e3aNull(\u591a\u6761\u4f1a\u8986\u76d6)\uff0c\u4f46\u5141\u8bb8\u591a\u6761Entry\u7684\u503c\u4e3aNull\u3002<\/b><\/span>\u6b64\u5916\uff0cHashMap \u662f Map \u7684\u4e00\u4e2a<span style=\"color: red;\"><b>\u975e\u540c\u6b65<\/b><\/span>\u7684\u5b9e\u73b0\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1990\" src=\"https:\/\/damogame.cn:15443\/wordpress\/wp-content\/uploads\/2018\/10\/20170317181610752.png\" alt=\"\" width=\"1018\" height=\"636\" srcset=\"https:\/\/i007.cc\/wordpress\/wp-content\/uploads\/2018\/10\/20170317181610752.png 1018w, https:\/\/i007.cc\/wordpress\/wp-content\/uploads\/2018\/10\/20170317181610752-768x480.png 768w\" sizes=\"auto, (max-width: 1018px) 100vw, 1018px\" \/><\/p>\n<p>\u540c\u6837\u5730\uff0cHashSet \u4e5f\u662f Java Collection Framework \u7684\u91cd\u8981\u6210\u5458\uff0c\u662f Set \u63a5\u53e3\u7684\u5e38\u7528\u5b9e\u73b0\u7c7b\uff0c\u4f46\u5176\u4e0e HashMap \u6709\u5f88\u591a\u76f8\u4f3c\u4e4b\u5904\u3002\u5bf9\u4e8e HashSet \u800c\u8a00\uff0c\u5176\u91c7\u7528 Hash \u7b97\u6cd5\u51b3\u5b9a\u5143\u7d20\u5728Set\u4e2d\u7684\u5b58\u50a8\u4f4d\u7f6e\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u5143\u7d20\u7684\u5feb\u901f\u5b58\u53d6\uff1b\u5bf9\u4e8e HashMap \u800c\u8a00\uff0c\u5176\u5c06 key-value \u5f53\u6210\u4e00\u4e2a\u6574\u4f53(Entry \u5bf9\u8c61)\u6765\u5904\u7406\uff0c\u5176\u4e5f\u91c7\u7528\u540c\u6837\u7684 Hash \u7b97\u6cd5\u53bb\u51b3\u5b9a key-value \u7684\u5b58\u50a8\u4f4d\u7f6e\u4ece\u800c\u4fdd\u8bc1\u952e\u503c\u5bf9\u7684\u5feb\u901f\u5b58\u53d6\u3002<span style=\"color: red;\"><b>\u867d\u7136 HashMap \u548c HashSet \u5b9e\u73b0\u7684\u63a5\u53e3\u89c4\u8303\u4e0d\u540c\uff0c\u4f46\u662f\u5b83\u4eec\u5e95\u5c42\u7684 Hash \u5b58\u50a8\u673a\u5236\u5b8c\u5168\u76f8\u540c\u3002\u5b9e\u9645\u4e0a\uff0cHashSet \u672c\u8eab\u5c31\u662f\u5728 HashMap \u7684\u57fa\u7840\u4e0a\u5b9e\u73b0\u7684\u3002<\/b><\/span>\u56e0\u6b64\uff0c\u901a\u8fc7\u5bf9 HashMap \u7684\u6570\u636e\u7ed3\u6784\u3001\u5b9e\u73b0\u539f\u7406\u3001\u6e90\u7801\u5b9e\u73b0\u4e09\u4e2a\u65b9\u9762\u4e86\u89e3\uff0c\u6211\u4eec\u4e0d\u4f46\u53ef\u4ee5\u8fdb\u4e00\u6b65\u638c\u63e1\u5176\u5e95\u5c42\u7684 Hash \u5b58\u50a8\u673a\u5236\uff0c\u4e5f\u6709\u52a9\u4e8e\u5bf9 HashSet \u7684\u4e86\u89e3\u3002<\/p>\n<p>\u5fc5\u987b\u6307\u51fa\u7684\u662f\uff0c\u867d\u7136\u5bb9\u5668\u53f7\u79f0\u5b58\u50a8\u7684\u662f Java \u5bf9\u8c61\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u4f1a\u771f\u6b63\u5c06 Java \u5bf9\u8c61\u653e\u5165\u5bb9\u5668\u4e2d\uff0c\u53ea\u662f\u5728\u5bb9\u5668\u4e2d\u4fdd\u7559\u8fd9\u4e9b\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cJava \u5bb9\u5668\u5b9e\u9645\u4e0a\u5305\u542b\u7684\u662f\u5f15\u7528\u53d8\u91cf\uff0c\u800c\u8fd9\u4e9b\u5f15\u7528\u53d8\u91cf\u6307\u5411\u4e86\u6211\u4eec\u8981\u5b9e\u9645\u4fdd\u5b58\u7684 Java \u5bf9\u8c61\u3002<\/p>\n<hr \/>\n<h2 id=\"\u4e8c-hashmap-\u5728-jdk-\u4e2d\u7684\u5b9a\u4e49\"><a name=\"t1\"><\/a>\u4e8c. HashMap \u5728 JDK \u4e2d\u7684\u5b9a\u4e49<\/h2>\n<p>HashMap\u5b9e\u73b0\u4e86Map\u63a5\u53e3\uff0c\u5e76\u7ee7\u627f AbstractMap \u62bd\u8c61\u7c7b\uff0c\u5176\u4e2d Map \u63a5\u53e3\u5b9a\u4e49\u4e86\u952e\u503c\u6620\u5c04\u89c4\u5219\u3002\u548c AbstractCollection\u62bd\u8c61\u7c7b\u5728 Collection \u65cf\u7684\u4f5c\u7528\u7c7b\u4f3c\uff0c AbstractMap \u62bd\u8c61\u7c7b\u63d0\u4f9b\u4e86 Map \u63a5\u53e3\u7684\u9aa8\u5e72\u5b9e\u73b0\uff0c\u4ee5\u6700\u5927\u9650\u5ea6\u5730\u51cf\u5c11\u5b9e\u73b0Map\u63a5\u53e3\u6240\u9700\u7684\u5de5\u4f5c\u3002HashMap \u5728JDK\u4e2d\u7684\u5b9a\u4e49\u4e3a\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class HashMap&lt;K,V&gt;\r\n    extends AbstractMap&lt;K,V&gt;\r\n    implements Map&lt;K,V&gt;, Cloneable, Serializable{\r\n...\r\n}<\/pre>\n<hr \/>\n<h2 id=\"\u4e09-hashmap-\u7684\u6784\u9020\u51fd\u6570\"><a name=\"t2\"><\/a>\u4e09. HashMap \u7684\u6784\u9020\u51fd\u6570<\/h2>\n<p>HashMap \u4e00\u5171\u63d0\u4f9b\u4e86\u56db\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u5176\u4e2d \u9ed8\u8ba4\u65e0\u53c2\u7684\u6784\u9020\u51fd\u6570 \u548c \u53c2\u6570\u4e3aMap\u7684\u6784\u9020\u51fd\u6570 \u4e3a Java Collection Framework \u89c4\u8303\u7684\u63a8\u8350\u5b9e\u73b0\uff0c\u5176\u4f59\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u5219\u662f HashMap \u4e13\u95e8\u63d0\u4f9b\u7684\u3002<\/p>\n<hr \/>\n<p><strong>1\u3001HashMap()<\/strong><\/p>\n<p>\u8be5\u6784\u9020\u51fd\u6570\u610f\u5728\u6784\u9020\u4e00\u4e2a\u5177\u6709&gt; \u9ed8\u8ba4\u521d\u59cb\u5bb9\u91cf (16) \u548c \u9ed8\u8ba4\u8d1f\u8f7d\u56e0\u5b50(0.75) \u7684\u7a7a HashMap\uff0c\u662f Java Collection Framework \u89c4\u8303\u63a8\u8350\u63d0\u4f9b\u7684\uff0c\u5176\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\"> \/**\r\n * Constructs an empty HashMap with the default initial capacity\r\n * (16) and the default load factor (0.75).\r\n *\/\r\npublic HashMap() {\r\n\r\n    \/\/\u8d1f\u8f7d\u56e0\u5b50:\u7528\u4e8e\u8861\u91cf\u7684\u662f\u4e00\u4e2a\u6563\u5217\u8868\u7684\u7a7a\u95f4\u7684\u4f7f\u7528\u7a0b\u5ea6\r\n    this.loadFactor = DEFAULT_LOAD_FACTOR; \r\n\r\n    \/\/HashMap\u8fdb\u884c\u6269\u5bb9\u7684\u9608\u503c\uff0c\u5b83\u7684\u503c\u7b49\u4e8e HashMap \u7684\u5bb9\u91cf\u4e58\u4ee5\u8d1f\u8f7d\u56e0\u5b50\r\n    threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);\r\n\r\n    \/\/ HashMap\u7684\u5e95\u5c42\u5b9e\u73b0\u4ecd\u662f\u6570\u7ec4\uff0c\u53ea\u662f\u6570\u7ec4\u7684\u6bcf\u4e00\u9879\u90fd\u662f\u4e00\u6761\u94fe\r\n    table = new Entry[DEFAULT_INITIAL_CAPACITY];\r\n\r\n    init();\r\n}<\/pre>\n<hr \/>\n<p><strong>2\u3001HashMap(int initialCapacity, float loadFactor)<\/strong><\/p>\n<p>\u8be5\u6784\u9020\u51fd\u6570\u610f\u5728\u6784\u9020\u4e00\u4e2a \u6307\u5b9a\u521d\u59cb\u5bb9\u91cf \u548c \u6307\u5b9a\u8d1f\u8f7d\u56e0\u5b50\u7684\u7a7a HashMap\uff0c\u5176\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/**\r\n * Constructs an empty HashMap with the specified initial capacity and load factor.\r\n *\/\r\npublic HashMap(int initialCapacity, float loadFactor) {\r\n    \/\/\u521d\u59cb\u5bb9\u91cf\u4e0d\u80fd\u5c0f\u4e8e 0\r\n    if (initialCapacity &lt; 0)\r\n        throw new IllegalArgumentException(\"Illegal initial capacity: \" + initialCapacity);\r\n\r\n    \/\/\u521d\u59cb\u5bb9\u91cf\u4e0d\u80fd\u8d85\u8fc7 2^30\r\n    if (initialCapacity &gt; MAXIMUM_CAPACITY)\r\n        initialCapacity = MAXIMUM_CAPACITY;\r\n\r\n    \/\/\u8d1f\u8f7d\u56e0\u5b50\u4e0d\u80fd\u5c0f\u4e8e 0            \r\n    if (loadFactor &lt;= 0 || Float.isNaN(loadFactor))\r\n        throw new IllegalArgumentException(\"Illegal load factor: \" +\r\n                                           loadFactor);\r\n\r\n    \/\/ HashMap \u7684\u5bb9\u91cf\u5fc5\u987b\u662f2\u7684\u5e42\u6b21\u65b9\uff0c\u8d85\u8fc7 initialCapacity \u7684\u6700\u5c0f 2^n \r\n    int capacity = 1;\r\n    while (capacity &lt; initialCapacity)\r\n        capacity &lt;&lt;= 1;   \r\n\r\n    \/\/\u8d1f\u8f7d\u56e0\u5b50\r\n    this.loadFactor = loadFactor;\r\n\r\n    \/\/\u8bbe\u7f6eHashMap\u7684\u5bb9\u91cf\u6781\u9650\uff0c\u5f53HashMap\u7684\u5bb9\u91cf\u8fbe\u5230\u8be5\u6781\u9650\u65f6\u5c31\u4f1a\u8fdb\u884c\u81ea\u52a8\u6269\u5bb9\u64cd\u4f5c\r\n    threshold = (int)(capacity * loadFactor);\r\n\r\n    \/\/ HashMap\u7684\u5e95\u5c42\u5b9e\u73b0\u4ecd\u662f\u6570\u7ec4\uff0c\u53ea\u662f\u6570\u7ec4\u7684\u6bcf\u4e00\u9879\u90fd\u662f\u4e00\u6761\u94fe\r\n    table = new Entry[capacity];\r\n    init();\r\n}<\/pre>\n<hr \/>\n<p><strong>3\u3001HashMap(int initialCapacity)<\/strong><\/p>\n<p>\u8be5\u6784\u9020\u51fd\u6570\u610f\u5728\u6784\u9020\u4e00\u4e2a\u6307\u5b9a\u521d\u59cb\u5bb9\u91cf\u548c\u9ed8\u8ba4\u8d1f\u8f7d\u56e0\u5b50 (0.75)\u7684\u7a7a HashMap\uff0c\u5176\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/\/ Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75)\r\npublic HashMap(int initialCapacity) {\r\n    this(initialCapacity, DEFAULT_LOAD_FACTOR);  \/\/ \u76f4\u63a5\u8c03\u7528\u4e0a\u8ff0\u6784\u9020\u51fd\u6570\r\n}<\/pre>\n<hr \/>\n<p><strong>4\u3001HashMap(Map&lt;? extends K, ? extends V&gt; m)<\/strong><\/p>\n<p>\u8be5\u6784\u9020\u51fd\u6570\u610f\u5728\u6784\u9020\u4e00\u4e2a\u4e0e\u6307\u5b9a Map \u5177\u6709\u76f8\u540c\u6620\u5c04\u7684 HashMap\uff0c\u5176 \u521d\u59cb\u5bb9\u91cf\u4e0d\u5c0f\u4e8e 16 (\u5177\u4f53\u4f9d\u8d56\u4e8e\u6307\u5b9aMap\u7684\u5927\u5c0f)\uff0c\u8d1f\u8f7d\u56e0\u5b50\u662f 0.75\uff0c\u662f Java Collection Framework \u89c4\u8303\u63a8\u8350\u63d0\u4f9b\u7684\uff0c\u5176\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/\/ Constructs a new HashMap with the same mappings as the specified Map. \r\n\/\/ The HashMap is created with default load factor (0.75) and an initial capacity\r\n\/\/ sufficient to hold the mappings in the specified Map.\r\npublic HashMap(Map&lt;? extends K, ? extends V&gt; m) {\r\n\r\n    \/\/ \u521d\u59cb\u5bb9\u91cf\u4e0d\u5c0f\u4e8e 16 \r\n    this(Math.max((int) (m.size() \/ DEFAULT_LOAD_FACTOR) + 1,\r\n                  DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);\r\n    putAllForCreate(m);\r\n}<\/pre>\n<hr \/>\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u63d0\u5230\u4e86\u4e24\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u53c2\u6570\uff1a<span style=\"color: red;\"><b>\u521d\u59cb\u5bb9\u91cf\u00a0<\/b><\/span>\u548c\u00a0<span style=\"color: red;\"><b>\u8d1f\u8f7d\u56e0\u5b50<\/b><\/span>\uff0c\u8fd9\u4e24\u4e2a\u53c2\u6570\u662f\u5f71\u54cdHashMap\u6027\u80fd\u7684\u91cd\u8981\u53c2\u6570\u3002\u5176\u4e2d\uff0c\u5bb9\u91cf\u8868\u793a\u54c8\u5e0c\u8868\u4e2d\u6876\u7684\u6570\u91cf (table \u6570\u7ec4\u7684\u5927\u5c0f)\uff0c\u521d\u59cb\u5bb9\u91cf\u662f\u521b\u5efa\u54c8\u5e0c\u8868\u65f6\u6876\u7684\u6570\u91cf\uff1b\u8d1f\u8f7d\u56e0\u5b50\u662f\u54c8\u5e0c\u8868\u5728\u5176\u5bb9\u91cf\u81ea\u52a8\u589e\u52a0\u4e4b\u524d\u53ef\u4ee5\u8fbe\u5230\u591a\u6ee1\u7684\u4e00\u79cd\u5c3a\u5ea6\uff0c\u5b83\u8861\u91cf\u7684\u662f\u4e00\u4e2a\u6563\u5217\u8868\u7684\u7a7a\u95f4\u7684\u4f7f\u7528\u7a0b\u5ea6\uff0c\u8d1f\u8f7d\u56e0\u5b50\u8d8a\u5927\u8868\u793a\u6563\u5217\u8868\u7684\u88c5\u586b\u7a0b\u5ea6\u8d8a\u9ad8\uff0c\u53cd\u4e4b\u6108\u5c0f\u3002<\/p>\n<p>\u5bf9\u4e8e\u4f7f\u7528\u00a0<span style=\"color: red;\"><b>\u62c9\u94fe\u6cd5<\/b><\/span>\uff08\u4e0b\u6587\u4f1a\u63d0\u5230\uff09\u7684\u54c8\u5e0c\u8868\u6765\u8bf4\uff0c\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u7684\u5e73\u5747\u65f6\u95f4\u662f\u00a0<span style=\"color: red;\">O(1+a)<\/span>\uff0ca \u6307\u7684\u662f\u94fe\u7684\u957f\u5ea6\uff0c\u662f\u4e00\u4e2a\u5e38\u6570\u3002\u7279\u522b\u5730\uff0c\u82e5\u8d1f\u8f7d\u56e0\u5b50\u8d8a\u5927\uff0c\u90a3\u4e48\u5bf9\u7a7a\u95f4\u7684\u5229\u7528\u66f4\u5145\u5206\uff0c\u4f46\u67e5\u627e\u6548\u7387\u7684\u4e5f\u5c31\u8d8a\u4f4e\uff1b\u82e5\u8d1f\u8f7d\u56e0\u5b50\u8d8a\u5c0f\uff0c\u90a3\u4e48\u54c8\u5e0c\u8868\u7684\u6570\u636e\u5c06\u8d8a\u7a00\u758f\uff0c\u5bf9\u7a7a\u95f4\u9020\u6210\u7684\u6d6a\u8d39\u4e5f\u5c31\u8d8a\u4e25\u91cd\u3002<span style=\"color: red;\"><b>\u7cfb\u7edf\u9ed8\u8ba4\u8d1f\u8f7d\u56e0\u5b50\u4e3a 0.75\uff0c\u8fd9\u662f\u65f6\u95f4\u548c\u7a7a\u95f4\u6210\u672c\u4e0a\u4e00\u79cd\u6298\u8877\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u6211\u4eec\u662f\u65e0\u9700\u4fee\u6539\u7684\u3002<\/b><\/span><\/p>\n<hr \/>\n<h2 id=\"\u56db-hashmap-\u7684\u6570\u636e\u7ed3\u6784\"><a name=\"t3\"><\/a>\u56db. HashMap \u7684\u6570\u636e\u7ed3\u6784<\/h2>\n<p>1\u3001\u54c8\u5e0c\u7684\u76f8\u5173\u6982\u5ff5<\/p>\n<p>Hash \u5c31\u662f\u628a\u4efb\u610f\u957f\u5ea6\u7684\u8f93\u5165(\u53c8\u53eb\u505a\u9884\u6620\u5c04\uff0c pre-image)\uff0c\u901a\u8fc7\u54c8\u5e0c\u7b97\u6cd5\uff0c\u53d8\u6362\u6210\u56fa\u5b9a\u957f\u5ea6\u7684\u8f93\u51fa(\u901a\u5e38\u662f\u6574\u578b)\uff0c\u8be5\u8f93\u51fa\u5c31\u662f\u54c8\u5e0c\u503c\u3002\u8fd9\u79cd\u8f6c\u6362\u662f\u4e00\u79cd<span style=\"color: red;\"><b>\u00a0\u538b\u7f29\u6620\u5c04\u00a0<\/b><\/span>\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u6563\u5217\u503c\u7684\u7a7a\u95f4\u901a\u5e38\u8fdc\u5c0f\u4e8e\u8f93\u5165\u7684\u7a7a\u95f4\u3002<span style=\"color: red;\"><b>\u4e0d\u540c\u7684\u8f93\u5165\u53ef\u80fd\u4f1a\u6563\u5217\u6210\u76f8\u540c\u7684\u8f93\u51fa\uff0c\u4ece\u800c\u4e0d\u53ef\u80fd\u4ece\u6563\u5217\u503c\u6765\u552f\u4e00\u7684\u786e\u5b9a\u8f93\u5165\u503c\u3002\u7b80\u5355\u7684\u8bf4\uff0c\u5c31\u662f\u4e00\u79cd\u5c06\u4efb\u610f\u957f\u5ea6\u7684\u6d88\u606f\u538b\u7f29\u5230\u67d0\u4e00\u56fa\u5b9a\u957f\u5ea6\u7684\u606f\u6458\u8981\u51fd\u6570\u3002<\/b><\/span><\/p>\n<hr \/>\n<p>2\u3001\u54c8\u5e0c\u7684\u5e94\u7528\uff1a\u6570\u636e\u7ed3\u6784<\/p>\n<p>\u6211\u4eec\u77e5\u9053\uff0c<span style=\"color: red;\">\u6570\u7ec4\u7684\u7279\u70b9\u662f\uff1a\u5bfb\u5740\u5bb9\u6613\uff0c\u63d2\u5165\u548c\u5220\u9664\u56f0\u96be\uff1b\u800c\u94fe\u8868\u7684\u7279\u70b9\u662f\uff1a\u5bfb\u5740\u56f0\u96be\uff0c\u63d2\u5165\u548c\u5220\u9664\u5bb9\u6613\u3002<\/span>\u90a3\u4e48\u6211\u4eec\u80fd\u4e0d\u80fd\u7efc\u5408\u4e24\u8005\u7684\u7279\u6027\uff0c\u505a\u51fa\u4e00\u79cd\u5bfb\u5740\u5bb9\u6613\uff0c\u63d2\u5165\u548c\u5220\u9664\u4e5f\u5bb9\u6613\u7684\u6570\u636e\u7ed3\u6784\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\uff0c\u8fd9\u5c31\u662f\u6211\u4eec\u8981\u63d0\u8d77\u7684\u54c8\u5e0c\u8868\u3002\u4e8b\u5b9e\u4e0a\uff0c\u54c8\u5e0c\u8868\u6709\u591a\u79cd\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u89e3\u91ca\u7684\u662f\u6700\u7ecf\u5178\u7684\u4e00\u79cd\u65b9\u6cd5 \u2014\u2014\u00a0<span style=\"color: red;\"><b>\u62c9\u94fe\u6cd5<\/b><\/span>\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u7406\u89e3\u4e3a\u00a0<span style=\"color: red;\"><b>\u94fe\u8868\u7684\u6570\u7ec4<\/b><\/span>\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1991\" src=\"https:\/\/damogame.cn:15443\/wordpress\/wp-content\/uploads\/2018\/10\/\u62c9\u94fe\u54c8\u5e0c\u8868.jpg\" alt=\"\" width=\"541\" height=\"456\" \/><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4ece\u4e0a\u56fe\u770b\u5230\uff0c\u5de6\u8fb9\u5f88\u660e\u663e\u662f\u4e2a\u6570\u7ec4\uff0c\u6570\u7ec4\u7684\u6bcf\u4e2a\u6210\u5458\u662f\u4e00\u4e2a\u94fe\u8868\u3002\u8be5\u6570\u636e\u7ed3\u6784\u6240\u5bb9\u7eb3\u7684\u6240\u6709\u5143\u7d20\u5747\u5305\u542b\u4e00\u4e2a\u6307\u9488\uff0c\u7528\u4e8e\u5143\u7d20\u95f4\u7684\u94fe\u63a5\u3002\u6211\u4eec\u6839\u636e\u5143\u7d20\u7684\u81ea\u8eab\u7279\u5f81\u628a\u5143\u7d20\u5206\u914d\u5230\u4e0d\u540c\u7684\u94fe\u8868\u4e2d\u53bb\uff0c\u53cd\u8fc7\u6765\u6211\u4eec\u4e5f\u6b63\u662f\u901a\u8fc7\u8fd9\u4e9b\u7279\u5f81\u627e\u5230\u6b63\u786e\u7684\u94fe\u8868\uff0c\u518d\u4ece\u94fe\u8868\u4e2d\u627e\u51fa\u6b63\u786e\u7684\u5143\u7d20\u3002\u5176\u4e2d\uff0c\u6839\u636e\u5143\u7d20\u7279\u5f81\u8ba1\u7b97\u5143\u7d20\u6570\u7ec4\u4e0b\u6807\u7684\u65b9\u6cd5\u5c31\u662f<span style=\"color: red;\"><b>\u00a0\u54c8\u5e0c\u7b97\u6cd5<\/b><\/span>\u3002<\/p>\n<p>\u603b\u7684\u6765\u8bf4\uff0c\u54c8\u5e0c\u8868\u9002\u5408\u7528\u4f5c\u5feb\u901f\u67e5\u627e\u3001\u5220\u9664\u7684\u57fa\u672c\u6570\u636e\u7ed3\u6784\uff0c\u901a\u5e38\u9700\u8981\u603b\u6570\u636e\u91cf\u53ef\u4ee5\u653e\u5165\u5185\u5b58\u3002\u5728\u4f7f\u7528\u54c8\u5e0c\u8868\u65f6\uff0c\u6709\u4ee5\u4e0b\u51e0\u4e2a\u5173\u952e\u70b9\uff1a<\/p>\n<ul>\n<li><span style=\"color: red;\"><b>hash \u51fd\u6570\uff08\u54c8\u5e0c\u7b97\u6cd5\uff09\u7684\u9009\u62e9<\/b><\/span>\uff1a\u9488\u5bf9\u4e0d\u540c\u7684\u5bf9\u8c61(\u5b57\u7b26\u4e32\u3001\u6574\u6570\u7b49)\u5177\u4f53\u7684\u54c8\u5e0c\u65b9\u6cd5\uff1b<\/li>\n<li><span style=\"color: red;\"><b>\u78b0\u649e\u5904\u7406<\/b><\/span>\uff1a\u5e38\u7528\u7684\u6709\u4e24\u79cd\u65b9\u5f0f\uff0c\u4e00\u79cd\u662fopen hashing\uff0c\u5373 &gt;\u62c9\u94fe\u6cd5\uff1b\u53e6\u4e00\u79cd\u5c31\u662f closed hashing\uff0c\u5373\u5f00\u5730\u5740\u6cd5(opened addressing)\u3002\u66f4\u591a\u5173\u4e8e\u54c8\u5e0c(Hash)\u7684\u4ecb\u7ecd\uff0c\u8bf7\u79fb\u6b65\u6211\u7684\u535a\u6587<a href=\"http:\/\/blog.csdn.net\/justloveyou_\/article\/details\/52464440\" target=\"_blank\" rel=\"nofollow noopener\">\u300aJava \u4e2d\u7684 ==, equals \u4e0e hashCode \u7684\u533a\u522b\u4e0e\u8054\u7cfb\u300b<\/a>\u3002<\/li>\n<\/ul>\n<hr \/>\n<p>3\u3001HashMap \u7684\u6570\u636e\u7ed3\u6784<\/p>\n<p>\u6211\u4eec\u77e5\u9053\uff0c\u5728Java\u4e2d\u6700\u5e38\u7528\u7684\u4e24\u79cd\u7ed3\u6784\u662f<span style=\"color: red;\"><b>\u00a0\u6570\u7ec4<\/b><\/span>\u00a0\u548c<span style=\"color: red;\"><b>\u00a0\u94fe\u8868<\/b><\/span>\uff0c\u51e0\u4e4e\u6240\u6709\u7684\u6570\u636e\u7ed3\u6784\u90fd\u53ef\u4ee5\u5229\u7528\u8fd9\u4e24\u79cd\u6765\u7ec4\u5408\u5b9e\u73b0\uff0cHashMap \u5c31\u662f\u8fd9\u79cd\u5e94\u7528\u7684\u4e00\u4e2a\u5178\u578b\u3002\u5b9e\u9645\u4e0a\uff0cHashMap \u5c31\u662f\u4e00\u4e2a\u00a0<span style=\"color: red;\"><b>\u94fe\u8868\u6570\u7ec4<\/b><\/span>\uff0c\u5982\u4e0b\u662f\u5b83\u6570\u636e\u7ed3\u6784\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1992\" src=\"https:\/\/damogame.cn:15443\/wordpress\/wp-content\/uploads\/2018\/10\/20170317181650025.png\" alt=\"\" width=\"809\" height=\"470\" srcset=\"https:\/\/i007.cc\/wordpress\/wp-content\/uploads\/2018\/10\/20170317181650025.png 809w, https:\/\/i007.cc\/wordpress\/wp-content\/uploads\/2018\/10\/20170317181650025-768x446.png 768w\" sizes=\"auto, (max-width: 809px) 100vw, 809px\" \/><\/p>\n<p>\u4ece\u4e0a\u56fe\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5f62\u8c61\u5730\u770b\u51faHashMap\u5e95\u5c42\u5b9e\u73b0\u8fd8\u662f\u6570\u7ec4\uff0c\u53ea\u662f\u6570\u7ec4\u7684\u6bcf\u4e00\u9879\u90fd\u662f\u4e00\u6761\u94fe\u3002\u5176\u4e2d\u53c2\u6570initialCapacity \u5c31\u4ee3\u8868\u4e86\u8be5\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4e5f\u5c31\u662f\u6876\u7684\u4e2a\u6570\u3002\u5728\u7b2c\u4e09\u8282\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86HashMap \u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u7684\u6e90\u7801\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/**\r\n    * Constructs an empty HashMap with the default initial capacity\r\n    * (16) and the default load factor (0.75).\r\n    *\/\r\n   public HashMap() {\r\n\r\n       \/\/\u8d1f\u8f7d\u56e0\u5b50:\u7528\u4e8e\u8861\u91cf\u7684\u662f\u4e00\u4e2a\u6563\u5217\u8868\u7684\u7a7a\u95f4\u7684\u4f7f\u7528\u7a0b\u5ea6\r\n       this.loadFactor = DEFAULT_LOAD_FACTOR; \r\n\r\n       \/\/HashMap\u8fdb\u884c\u6269\u5bb9\u7684\u9608\u503c\uff0c\u5b83\u7684\u503c\u7b49\u4e8e HashMap \u7684\u5bb9\u91cf\u4e58\u4ee5\u8d1f\u8f7d\u56e0\u5b50\r\n       threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);\r\n\r\n       \/\/ HashMap\u7684\u5e95\u5c42\u5b9e\u73b0\u4ecd\u662f\u6570\u7ec4\uff0c\u53ea\u662f\u6570\u7ec4\u7684\u6bcf\u4e00\u9879\u90fd\u662f\u4e00\u6761\u94fe\r\n       table = new Entry[DEFAULT_INITIAL_CAPACITY];\r\n\r\n       init();\r\n   }<\/pre>\n<p>\u4ece\u4e0a\u8ff0\u6e90\u7801\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0c\u6bcf\u6b21\u65b0\u5efa\u4e00\u4e2aHashMap\u65f6\uff0c\u90fd\u4f1a\u521d\u59cb\u5316\u4e00\u4e2aEntry\u7c7b\u578b\u7684table\u6570\u7ec4\uff0c\u5176\u4e2d Entry\u7c7b\u578b\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">static class Entry&lt;K,V&gt; implements Map.Entry&lt;K,V&gt; {\r\n\r\n    final K key;     \/\/ \u952e\u503c\u5bf9\u7684\u952e\r\n    V value;        \/\/ \u952e\u503c\u5bf9\u7684\u503c\r\n    Entry&lt;K,V&gt; next;    \/\/ \u4e0b\u4e00\u4e2a\u8282\u70b9\r\n    final int hash;     \/\/ hash(key.hashCode())\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\r\n\r\n    \/**\r\n     * Creates new entry.\r\n     *\/\r\n    Entry(int h, K k, V v, Entry&lt;K,V&gt; n) {     \/\/ Entry \u7684\u6784\u9020\u51fd\u6570\r\n        value = v;\r\n        next = n;\r\n        key = k;\r\n        hash = h;\r\n    }\r\n\r\n    ......\r\n\r\n}<\/pre>\n<p>\u5176\u4e2d\uff0cEntry\u4e3aHashMap\u7684\u5185\u90e8\u7c7b\uff0c\u5b9e\u73b0\u4e86 Map.Entry \u63a5\u53e3\uff0c\u5176\u5305\u542b\u4e86\u952ekey\u3001\u503cvalue\u3001\u4e0b\u4e00\u4e2a\u8282\u70b9next\uff0c\u4ee5\u53cahash\u503c\u56db\u4e2a\u5c5e\u6027\u3002\u4e8b\u5b9e\u4e0a\uff0c<span style=\"color: red;\"><b>Entry \u662f\u6784\u6210\u54c8\u5e0c\u8868\u7684\u57fa\u77f3\uff0c\u662f\u54c8\u5e0c\u8868\u6240\u5b58\u50a8\u7684\u5143\u7d20\u7684\u5177\u4f53\u5f62\u5f0f\u3002<\/b><\/span><\/p>\n<hr \/>\n<h2 id=\"\u4e94-hashmap-\u7684\u5feb\u901f\u5b58\u53d6\"><a name=\"t4\"><\/a>\u4e94. HashMap \u7684\u5feb\u901f\u5b58\u53d6<\/h2>\n<p>\u5728HashMap\u4e2d\uff0c\u6211\u4eec\u6700\u5e38\u7528\u7684\u4e24\u4e2a\u64cd\u4f5c\u5c31\u662f\uff1aput(Key,Value) \u548c get(Key)\u3002\u6211\u4eec\u90fd\u77e5\u9053\uff0c<span style=\"color: red;\">HashMap\u4e2d\u7684Key\u662f\u552f\u4e00\u7684\uff0c\u90a3\u5b83\u662f\u5982\u4f55\u4fdd\u8bc1\u552f\u4e00\u6027\u7684\u5462\uff1f<\/span>\u6211\u4eec\u9996\u5148\u60f3\u5230\u7684\u662f\u7528equals\u6bd4\u8f83\uff0c\u6ca1\u9519\uff0c\u8fd9\u6837\u53ef\u4ee5\u5b9e\u73b0\uff0c\u4f46\u968f\u7740\u5143\u7d20\u7684\u589e\u591a\uff0cput \u548c get \u7684\u6548\u7387\u5c06\u8d8a\u6765\u8d8a\u4f4e\uff0c\u8fd9\u91cc\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(n)\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5047\u5982 HashMap \u67091000\u4e2a\u5143\u7d20\uff0c\u90a3\u4e48 put\u65f6\u5c31\u9700\u8981\u6bd4\u8f83 1000 \u6b21\uff0c\u8fd9\u662f\u76f8\u5f53\u8017\u65f6\u7684\uff0c\u8fdc\u8fbe\u4e0d\u5230HashMap\u5feb\u901f\u5b58\u53d6\u7684\u76ee\u7684\u3002\u5b9e\u9645\u4e0a\uff0cHashMap \u5f88\u5c11\u4f1a\u7528\u5230equals\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5176\u5185\u901a\u8fc7\u4e00\u4e2a\u54c8\u5e0c\u8868\u7ba1\u7406\u6240\u6709\u5143\u7d20\uff0c\u5229\u7528\u54c8\u5e0c\u7b97\u6cd5\u53ef\u4ee5\u5feb\u901f\u7684\u5b58\u53d6\u5143\u7d20\u3002\u5f53\u6211\u4eec\u8c03\u7528put\u65b9\u6cd5\u5b58\u503c\u65f6\uff0cHashMap\u9996\u5148\u4f1a\u8c03\u7528Key\u7684hashCode\u65b9\u6cd5\uff0c\u7136\u540e\u57fa\u4e8e\u6b64\u83b7\u53d6Key\u54c8\u5e0c\u7801\uff0c\u901a\u8fc7\u54c8\u5e0c\u7801\u5feb\u901f\u627e\u5230\u67d0\u4e2a\u6876\uff0c\u8fd9\u4e2a\u4f4d\u7f6e\u53ef\u4ee5\u88ab\u79f0\u4e4b\u4e3a bucketIndex\u3002\u901a\u8fc7<a href=\"http:\/\/blog.csdn.net\/justloveyou_\/article\/details\/52464440\" target=\"_blank\" rel=\"nofollow noopener\">\u300aJava \u4e2d\u7684 ==, equals \u4e0e hashCode \u7684\u533a\u522b\u4e0e\u8054\u7cfb\u300b<\/a>\u00a0\u6240\u8ff0hashCode\u7684\u534f\u5b9a\u53ef\u4ee5\u77e5\u9053\uff0c<span style=\"color: red;\">\u5982\u679c\u4e24\u4e2a\u5bf9\u8c61\u7684hashCode\u4e0d\u540c\uff0c\u90a3\u4e48equals\u4e00\u5b9a\u4e3a false\uff1b\u5426\u5219\uff0c\u5982\u679c\u5176hashCode\u76f8\u540c\uff0cequals\u4e5f\u4e0d\u4e00\u5b9a\u4e3a true\u3002<\/span>\u6240\u4ee5\uff0c\u7406\u8bba\u4e0a\uff0chashCode \u53ef\u80fd\u5b58\u5728\u78b0\u649e\u7684\u60c5\u51b5\uff0c\u5f53\u78b0\u649e\u53d1\u751f\u65f6\uff0c\u8fd9\u65f6\u4f1a\u53d6\u51fabucketIndex\u6876\u5185\u5df2\u5b58\u50a8\u7684\u5143\u7d20\uff0c\u5e76\u901a\u8fc7hashCode() \u548c equals() \u6765\u9010\u4e2a\u6bd4\u8f83\u4ee5\u5224\u65adKey\u662f\u5426\u5df2\u5b58\u5728\u3002\u5982\u679c\u5df2\u5b58\u5728\uff0c\u5219\u4f7f\u7528\u65b0Value\u503c\u66ff\u6362\u65e7Value\u503c\uff0c\u5e76\u8fd4\u56de\u65e7Value\u503c\uff1b\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u5b58\u653e\u65b0\u7684\u952e\u503c\u5bf9&lt;Key, Value&gt;\u5230\u6876\u4e2d\u3002\u56e0\u6b64\uff0c\u5728 HashMap\u4e2d\uff0cequals() \u65b9\u6cd5\u53ea\u6709\u5728\u54c8\u5e0c\u7801\u78b0\u649e\u65f6\u624d\u4f1a\u88ab\u7528\u5230\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u7ed3\u5408JDK\u6e90\u7801\u770bHashMap \u7684\u5b58\u53d6\u5b9e\u73b0\u3002<\/p>\n<hr \/>\n<p><strong>1\u3001HashMap \u7684\u5b58\u50a8\u5b9e\u73b0<\/strong><\/p>\n<p>\u5728 HashMap \u4e2d\uff0c\u952e\u503c\u5bf9\u7684\u5b58\u50a8\u662f\u901a\u8fc7 put(key,vlaue) \u65b9\u6cd5\u6765\u5b9e\u73b0\u7684\uff0c\u5176\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/**\r\n * Associates the specified value with the specified key in this map.\r\n * If the map previously contained a mapping for the key, the old\r\n * value is replaced.\r\n *\r\n * @param key key with which the specified value is to be associated\r\n * @param value value to be associated with the specified key\r\n * @return the previous value associated with key, or null if there was no mapping for key.\r\n *  Note that a null return can also indicate that the map previously associated null with key.\r\n *\/\r\npublic V put(K key, V value) {\r\n\r\n    \/\/\u5f53key\u4e3anull\u65f6\uff0c\u8c03\u7528putForNullKey\u65b9\u6cd5\uff0c\u5e76\u5c06\u8be5\u952e\u503c\u5bf9\u4fdd\u5b58\u5230table\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e \r\n    if (key == null)\r\n        return putForNullKey(value); \r\n\r\n    \/\/\u6839\u636ekey\u7684hashCode\u8ba1\u7b97hash\u503c\r\n    int hash = hash(key.hashCode());             \/\/  ------- (1)\r\n\r\n    \/\/\u8ba1\u7b97\u8be5\u952e\u503c\u5bf9\u5728\u6570\u7ec4\u4e2d\u7684\u5b58\u50a8\u4f4d\u7f6e\uff08\u54ea\u4e2a\u6876\uff09\r\n    int i = indexFor(hash, table.length);              \/\/ ------- (2)\r\n\r\n    \/\/\u5728table\u7684\u7b2ci\u4e2a\u6876\u4e0a\u8fdb\u884c\u8fed\u4ee3\uff0c\u5bfb\u627e key \u4fdd\u5b58\u7684\u4f4d\u7f6e\r\n    for (Entry&lt;K,V&gt; e = table[i]; e != null; e = e.next) {      \/\/ ------- (3)\r\n        Object k;\r\n        \/\/\u5224\u65ad\u8be5\u6761\u94fe\u4e0a\u662f\u5426\u5b58\u5728hash\u503c\u76f8\u540c\u4e14key\u503c\u76f8\u7b49\u7684\u6620\u5c04\uff0c\u82e5\u5b58\u5728\uff0c\u5219\u76f4\u63a5\u8986\u76d6 value\uff0c\u5e76\u8fd4\u56de\u65e7value\r\n        if (e.hash == hash &amp;&amp; ((k = e.key) == key || key.equals(k))) {\r\n            V oldValue = e.value;\r\n            e.value = value;\r\n            e.recordAccess(this);\r\n            return oldValue;    \/\/ \u8fd4\u56de\u65e7\u503c\r\n        }\r\n    }\r\n\r\n    modCount++; \/\/\u4fee\u6539\u6b21\u6570\u589e\u52a01\uff0c\u5feb\u901f\u5931\u8d25\u673a\u5236\r\n\r\n    \/\/\u539fHashMap\u4e2d\u65e0\u8be5\u6620\u5c04\uff0c\u5c06\u8be5\u6dfb\u52a0\u81f3\u8be5\u94fe\u7684\u94fe\u5934\r\n    addEntry(hash, key, value, i);            \r\n    return null;\r\n}<\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u6e90\u7801\u6211\u4eec\u53ef\u4ee5\u6e05\u695a\u4e86\u89e3\u5230HashMap\u4fdd\u5b58\u6570\u636e\u7684\u8fc7\u7a0b\u3002\u9996\u5148\uff0c\u5224\u65adkey\u662f\u5426\u4e3anull\uff0c\u82e5\u4e3anull\uff0c\u5219\u76f4\u63a5\u8c03\u7528putForNullKey\u65b9\u6cd5\uff1b\u82e5\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5148\u8ba1\u7b97key\u7684hash\u503c\uff0c\u7136\u540e\u6839\u636ehash\u503c\u641c\u7d22\u5728table\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u4f4d\u7f6e\uff0c\u5982\u679ctable\u6570\u7ec4\u5728\u8be5\u4f4d\u7f6e\u5904\u6709\u5143\u7d20\uff0c\u5219\u67e5\u627e\u662f\u5426\u5b58\u5728\u76f8\u540c\u7684key\uff0c\u82e5\u5b58\u5728\u5219\u8986\u76d6\u539f\u6765key\u7684value\uff0c\u5426\u5219\u5c06\u8be5\u5143\u7d20\u4fdd\u5b58\u5728\u94fe\u5934\uff08\u6700\u5148\u4fdd\u5b58\u7684\u5143\u7d20\u653e\u5728\u94fe\u5c3e\uff09\u3002\u6b64\u5916\uff0c\u82e5table\u5728\u8be5\u5904\u6ca1\u6709\u5143\u7d20\uff0c\u5219\u76f4\u63a5\u4fdd\u5b58\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u770b\u4f3c\u6bd4\u8f83\u7b80\u5355\uff0c\u4f46\u5176\u5b9e\u6709\u5f88\u591a\u9700\u8981\u56de\u5473\u7684\u5730\u65b9\uff0c\u4e0b\u9762\u6211\u4eec\u4e00\u4e00\u6765\u770b\u3002<\/p>\n<p>\u5148\u770b\u6e90\u7801\u4e2d\u7684 (3) \u5904\uff0c\u6b64\u5904\u8fed\u4ee3\u539f\u56e0\u5c31\u662f\u4e3a\u4e86\u9632\u6b62\u5b58\u5728\u76f8\u540c\u7684key\u503c\u3002\u5982\u679c\u53d1\u73b0\u4e24\u4e2ahash\u503c\uff08key\uff09\u76f8\u540c\u65f6\uff0cHashMap\u7684\u5904\u7406\u65b9\u5f0f\u662f\u7528\u65b0value\u66ff\u6362\u65e7value\uff0c\u8fd9\u91cc\u5e76\u6ca1\u6709\u5904\u7406key\uff0c\u8fd9\u6b63\u597d\u89e3\u91ca\u4e86 HashMap \u4e2d\u6ca1\u6709\u4e24\u4e2a\u76f8\u540c\u7684 key\u3002<\/p>\n<hr \/>\n<p>1). \u5bf9NULL\u952e\u7684\u7279\u522b\u5904\u7406\uff1aputForNullKey()<\/p>\n<p>\u6211\u4eec\u76f4\u63a5\u770b\u5176\u6e90\u7801\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/**\r\n * Offloaded version of put for null keys\r\n *\/\r\nprivate V putForNullKey(V value) {\r\n    \/\/ \u82e5key==null\uff0c\u5219\u5c06\u5176\u653e\u5165table\u7684\u7b2c\u4e00\u4e2a\u6876\uff0c\u5373 table[0]\r\n    for (Entry&lt;K,V&gt; e = table[0]; e != null; e = e.next) {   \r\n        if (e.key == null) {   \/\/ \u82e5\u5df2\u7ecf\u5b58\u5728key\u4e3anull\u7684\u952e\uff0c\u5219\u66ff\u6362\u5176\u503c\uff0c\u5e76\u8fd4\u56de\u65e7\u503c\r\n            V oldValue = e.value;\r\n            e.value = value;\r\n            e.recordAccess(this);\r\n            return oldValue;\r\n        }\r\n    }\r\n    modCount++;        \/\/ \u5feb\u901f\u5931\u8d25\r\n    addEntry(0, null, value, 0);       \/\/ \u5426\u5219\uff0c\u5c06\u5176\u6dfb\u52a0\u5230 table[0] \u7684\u6876\u4e2d\r\n    return null;\r\n}<\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u6e90\u7801\u6211\u4eec\u53ef\u4ee5\u6e05\u695a\u77e5\u5230\uff0cHashMap \u4e2d\u53ef\u4ee5\u4fdd\u5b58\u952e\u4e3aNULL\u7684\u952e\u503c\u5bf9\uff0c\u4e14\u8be5\u952e\u503c\u5bf9\u662f\u552f\u4e00\u7684\u3002\u82e5\u518d\u6b21\u5411\u5176\u4e2d\u6dfb\u52a0\u952e\u4e3aNULL\u7684\u952e\u503c\u5bf9\uff0c\u5c06\u8986\u76d6\u5176\u539f\u503c\u3002\u6b64\u5916\uff0c\u5982\u679cHashMap\u4e2d\u5b58\u5728\u952e\u4e3aNULL\u7684\u952e\u503c\u5bf9\uff0c\u90a3\u4e48\u4e00\u5b9a\u5728\u7b2c\u4e00\u4e2a\u6876\u4e2d\u3002<\/p>\n<p>2). HashMap \u4e2d\u7684\u54c8\u5e0c\u7b56\u7565\uff08\u7b97\u6cd5\uff09<\/p>\n<p>\u5728\u4e0a\u8ff0\u7684 put(key,vlaue) \u65b9\u6cd5\u7684\u6e90\u7801\u4e2d\uff0c\u6211\u4eec\u6807\u51fa\u4e86 HashMap \u4e2d\u7684\u54c8\u5e0c\u7b56\u7565\uff08\u5373(1)\u3001(2)\u4e24\u5904\uff09\uff0c<span style=\"color: red;\"><b>hash() \u65b9\u6cd5\u7528\u4e8e\u5bf9Key\u7684hashCode\u8fdb\u884c\u91cd\u65b0\u8ba1\u7b97\uff0c\u800c indexFor() \u65b9\u6cd5\u7528\u4e8e\u751f\u6210\u8fd9\u4e2aEntry\u5bf9\u8c61\u7684\u63d2\u5165\u4f4d\u7f6e\u3002<\/b><\/span>\u5f53\u8ba1\u7b97\u51fa\u6765\u7684hash\u503c\u4e0ehashMap\u7684(length-1)\u505a\u4e86&amp;\u8fd0\u7b97\u540e\uff0c\u4f1a\u5f97\u5230\u4f4d\u4e8e\u533a\u95f4[0\uff0clength-1]\u7684\u4e00\u4e2a\u503c\u3002\u7279\u522b\u5730\uff0c\u8fd9\u4e2a\u503c\u5206\u5e03\u7684\u8d8a\u5747\u5300\uff0c HashMap \u7684\u7a7a\u95f4\u5229\u7528\u7387\u4e5f\u5c31\u8d8a\u9ad8\uff0c\u5b58\u53d6\u6548\u7387\u4e5f\u5c31\u8d8a\u597d\u3002<\/p>\n<hr \/>\n<p>\u6211\u4eec\u9996\u5148\u770b(1)\u5904\u7684 hash() \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4e3a\u4e00\u4e2a\u7eaf\u7cb9\u7684\u6570\u5b66\u8ba1\u7b97\uff0c\u7528\u4e8e\u8fdb\u4e00\u6b65\u8ba1\u7b97key\u7684hash\u503c\uff0c\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/**\r\n * Applies a supplemental hash function to a given hashCode, which\r\n * defends against poor quality hash functions.  This is critical\r\n * because HashMap uses power-of-two length hash tables, that\r\n * otherwise encounter collisions for hashCodes that do not differ\r\n * in lower bits. \r\n * \r\n * Note: Null keys always map to hash 0, thus index 0.\r\n *\/\r\nstatic int hash(int h) {\r\n    \/\/ This function ensures that hashCodes that differ only by\r\n    \/\/ constant multiples at each bit position have a bounded\r\n    \/\/ number of collisions (approximately 8 at default load factor).\r\n    h ^= (h &gt;&gt;&gt; 20) ^ (h &gt;&gt;&gt; 12);\r\n    return h ^ (h &gt;&gt;&gt; 7) ^ (h &gt;&gt;&gt; 4);\r\n}<\/pre>\n<p>\u6b63\u5982JDK\u5b98\u65b9\u5bf9\u8be5\u65b9\u6cd5\u7684\u63cf\u8ff0\u90a3\u6837\uff0c<span style=\"color: red;\"><b>\u4f7f\u7528hash()\u65b9\u6cd5\u5bf9\u4e00\u4e2a\u5bf9\u8c61\u7684hashCode\u8fdb\u884c\u91cd\u65b0\u8ba1\u7b97\u662f\u4e3a\u4e86\u9632\u6b62\u8d28\u91cf\u4f4e\u4e0b\u7684hashCode()\u51fd\u6570\u5b9e\u73b0\u3002\u7531\u4e8ehashMap\u7684\u652f\u6491\u6570\u7ec4\u957f\u5ea6\u603b\u662f 2 \u7684\u5e42\u6b21\uff0c\u901a\u8fc7\u53f3\u79fb\u53ef\u4ee5\u4f7f\u4f4e\u4f4d\u7684\u6570\u636e\u5c3d\u91cf\u7684\u4e0d\u540c\uff0c\u4ece\u800c\u4f7fhash\u503c\u7684\u5206\u5e03\u5c3d\u91cf\u5747\u5300\u3002<\/b><\/span>\u66f4\u591a\u5173\u4e8e\u8be5 hash(int h)\u65b9\u6cd5\u7684\u4ecb\u7ecd\u8bf7\u89c1<a href=\"http:\/\/www.iteye.com\/topic\/709945\" target=\"_blank\" rel=\"nofollow noopener\">\u300aHashMap hash\u65b9\u6cd5\u5206\u6790\u300b<\/a>\uff0c\u6b64\u4e0d\u8d58\u8ff0\u3002<\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0hash()\u65b9\u6cd5\u8ba1\u7b97\u5f97\u5230 Key \u7684 hash\u503c \u540e\uff0c\u600e\u4e48\u624d\u80fd\u4fdd\u8bc1\u5143\u7d20\u5747\u5300\u5206\u5e03\u5230table\u7684\u6bcf\u4e2a\u6876\u4e2d\u5462\uff1f<span style=\"color: red;\">\u6211\u4eec\u4f1a\u60f3\u5230\u53d6\u6a21\uff0c\u4f46\u662f\u7531\u4e8e\u53d6\u6a21\u7684\u6548\u7387\u8f83\u4f4e\uff0cHashMap \u662f\u901a\u8fc7\u8c03\u7528(2)\u5904\u7684indexFor()\u65b9\u6cd5\u5904\u7406\u7684\uff0c\u5176\u4e0d\u4f46\u7b80\u5355\u800c\u4e14\u6548\u7387\u5f88\u9ad8\uff0c<\/span>\u5bf9\u5e94\u6e90\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/**\r\n * Returns index for hash code h.\r\n *\/\r\nstatic int indexFor(int h, int length) {\r\n    return h &amp; (length-1);  \/\/ \u4f5c\u7528\u7b49\u4ef7\u4e8e\u53d6\u6a21\u8fd0\u7b97\uff0c\u4f46\u8fd9\u79cd\u65b9\u5f0f\u6548\u7387\u66f4\u9ad8\r\n}<\/pre>\n<p>\u6211\u4eec\u77e5\u9053\uff0cHashMap\u7684\u5e95\u5c42\u6570\u7ec4\u957f\u5ea6\u603b\u662f2\u7684n\u6b21\u65b9\u3002<span style=\"color: red;\"><b>\u5f53length\u4e3a2\u7684n\u6b21\u65b9\u65f6\uff0ch&amp;(length &#8211; 1)\u5c31\u76f8\u5f53\u4e8e\u5bf9length\u53d6\u6a21\uff0c\u800c\u4e14\u901f\u5ea6\u6bd4\u76f4\u63a5\u53d6\u6a21\u8981\u5feb\u5f97\u591a\uff0c\u8fd9\u662fHashMap\u5728\u901f\u5ea6\u4e0a\u7684\u4e00\u4e2a\u4f18\u5316\u3002<\/b><\/span>\u81f3\u4e8eHashMap\u7684\u5e95\u5c42\u6570\u7ec4\u957f\u5ea6\u4e3a\u4ec0\u4e48\u662f2\u7684n\u6b21\u65b9\uff0c\u4e0b\u4e00\u8282\u5c06\u7ed9\u51fa\u89e3\u91ca\u3002<\/p>\n<hr \/>\n<p>\u603b\u800c\u8a00\u4e4b\uff0c\u4e0a\u8ff0\u7684hash()\u65b9\u6cd5\u548cindexFor()\u65b9\u6cd5\u7684\u4f5c\u7528\u53ea\u6709\u4e00\u4e2a\uff1a\u4fdd\u8bc1\u5143\u7d20\u5747\u5300\u5206\u5e03\u5230table\u7684\u6bcf\u4e2a\u6876\u4e2d\u4ee5\u4fbf\u5145\u5206\u5229\u7528\u7a7a\u95f4\u3002<\/p>\n<hr \/>\n<p>3). HashMap \u4e2d\u952e\u503c\u5bf9\u7684\u6dfb\u52a0\uff1aaddEntry()<br \/>\n\u6211\u4eec\u76f4\u63a5\u770b\u5176\u6e90\u7801\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/**\r\n * Adds a new entry with the specified key, value and hash code to\r\n * the specified bucket.  It is the responsibility of this\r\n * method to resize the table if appropriate.\r\n *\r\n * Subclass overrides this to alter the behavior of put method.\r\n * \r\n * \u6c38\u8fdc\u90fd\u662f\u5728\u94fe\u8868\u7684\u8868\u5934\u6dfb\u52a0\u65b0\u5143\u7d20\r\n *\/\r\nvoid addEntry(int hash, K key, V value, int bucketIndex) {\r\n\r\n    \/\/\u83b7\u53d6bucketIndex\u5904\u7684\u94fe\u8868\r\n    Entry&lt;K,V&gt; e = table[bucketIndex];\r\n\r\n    \/\/\u5c06\u65b0\u521b\u5efa\u7684 Entry \u94fe\u5165 bucketIndex\u5904\u7684\u94fe\u8868\u7684\u8868\u5934 \r\n    table[bucketIndex] = new Entry&lt;K,V&gt;(hash, key, value, e);\r\n\r\n    \/\/\u82e5HashMap\u4e2d\u5143\u7d20\u7684\u4e2a\u6570\u8d85\u8fc7\u6781\u9650\u503c threshold\uff0c\u5219\u5bb9\u91cf\u6269\u5927\u4e24\u500d\r\n    if (size++ &gt;= threshold)\r\n        resize(2 * table.length);\r\n}<\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u6e90\u7801\u6211\u4eec\u53ef\u4ee5\u6e05\u695a\u5730\u4e86\u89e3\u5230\u00a0<span style=\"color: red;\"><b>\u94fe\u7684\u4ea7\u751f\u65f6\u673a<\/b><\/span>\u3002HashMap \u603b\u662f\u5c06\u65b0\u7684Entry\u5bf9\u8c61\u6dfb\u52a0\u5230bucketIndex\u5904\uff0c\u82e5bucketIndex\u5904\u5df2\u7ecf\u6709\u4e86Entry\u5bf9\u8c61\uff0c\u90a3\u4e48\u65b0\u6dfb\u52a0\u7684Entry\u5bf9\u8c61\u5c06\u6307\u5411\u539f\u6709\u7684Entry\u5bf9\u8c61\uff0c\u5e76\u5f62\u6210\u4e00\u6761\u65b0\u7684\u4ee5\u5b83\u4e3a\u94fe\u5934\u7684Entry\u94fe\uff1b\u4f46\u662f\uff0c\u82e5bucketIndex\u5904\u539f\u5148\u6ca1\u6709Entry\u5bf9\u8c61\uff0c\u90a3\u4e48\u65b0\u6dfb\u52a0\u7684Entry\u5bf9\u8c61\u5c06\u6307\u5411 null\uff0c\u4e5f\u5c31\u751f\u6210\u4e86\u4e00\u6761\u957f\u5ea6\u4e3a 1 \u7684\u5168\u65b0\u7684Entry\u94fe\u4e86\u3002<span style=\"color: red;\"><b>HashMap \u6c38\u8fdc\u90fd\u662f\u5728\u94fe\u8868\u7684\u8868\u5934\u6dfb\u52a0\u65b0\u5143\u7d20\u3002\u6b64\u5916\uff0c\u82e5HashMap\u4e2d\u5143\u7d20\u7684\u4e2a\u6570\u8d85\u8fc7\u6781\u9650\u503c threshold\uff0c\u5176\u5c06\u8fdb\u884c\u6269\u5bb9\u64cd\u4f5c\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u5bb9\u91cf\u5c06\u6269\u5927\u81f3\u539f\u6765\u7684\u4e24\u500d\u3002<\/b><\/span><\/p>\n<p>4). HashMap \u7684\u6269\u5bb9\uff1aresize()<\/p>\n<p>\u968f\u7740HashMap\u4e2d\u5143\u7d20\u7684\u6570\u91cf\u8d8a\u6765\u8d8a\u591a\uff0c\u53d1\u751f\u78b0\u649e\u7684\u6982\u7387\u5c06\u8d8a\u6765\u8d8a\u5927\uff0c\u6240\u4ea7\u751f\u7684\u5b50\u94fe\u957f\u5ea6\u5c31\u4f1a\u8d8a\u6765\u8d8a\u957f\uff0c\u8fd9\u6837\u52bf\u5fc5\u4f1a\u5f71\u54cdHashMap\u7684\u5b58\u53d6\u901f\u5ea6\u3002<span style=\"color: red;\"><b>\u4e3a\u4e86\u4fdd\u8bc1HashMap\u7684\u6548\u7387\uff0c\u7cfb\u7edf\u5fc5\u987b\u8981\u5728\u67d0\u4e2a\u4e34\u754c\u70b9\u8fdb\u884c\u6269\u5bb9\u5904\u7406\uff0c\u8be5\u4e34\u754c\u70b9\u5c31\u662fHashMap\u4e2d\u5143\u7d20\u7684\u6570\u91cf\u5728\u6570\u503c\u4e0a\u7b49\u4e8ethreshold\uff08table\u6570\u7ec4\u957f\u5ea6*\u52a0\u8f7d\u56e0\u5b50\uff09\u3002<\/b><\/span>\u4f46\u662f\uff0c\u4e0d\u5f97\u4e0d\u8bf4\uff0c\u6269\u5bb9\u662f\u4e00\u4e2a\u975e\u5e38\u8017\u65f6\u7684\u8fc7\u7a0b\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u91cd\u65b0\u8ba1\u7b97\u8fd9\u4e9b\u5143\u7d20\u5728\u65b0table\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\u5e76\u8fdb\u884c\u590d\u5236\u5904\u7406\u3002\u6240\u4ee5\uff0c\u5982\u679c\u6211\u4eec\u80fd\u591f\u63d0\u524d\u9884\u77e5HashMap \u4e2d\u5143\u7d20\u7684\u4e2a\u6570\uff0c\u90a3\u4e48\u5728\u6784\u9020HashMap\u65f6\u9884\u8bbe\u5143\u7d20\u7684\u4e2a\u6570\u80fd\u591f\u6709\u6548\u7684\u63d0\u9ad8HashMap\u7684\u6027\u80fd\u3002\u6211\u4eec\u76f4\u63a5\u770b\u5176\u6e90\u7801\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\"> \/**\r\n * Rehashes the contents of this map into a new array with a\r\n * larger capacity.  This method is called automatically when the\r\n * number of keys in this map reaches its threshold.\r\n *\r\n * If current capacity is MAXIMUM_CAPACITY, this method does not\r\n * resize the map, but sets threshold to Integer.MAX_VALUE.\r\n * This has the effect of preventing future calls.\r\n *\r\n * @param newCapacity the new capacity, MUST be a power of two;\r\n *        must be greater than current capacity unless current\r\n *        capacity is MAXIMUM_CAPACITY (in which case value\r\n *        is irrelevant).\r\n *\/\r\nvoid resize(int newCapacity) {\r\n    Entry[] oldTable = table;\r\n    int oldCapacity = oldTable.length;\r\n\r\n    \/\/ \u82e5 oldCapacity \u5df2\u8fbe\u5230\u6700\u5927\u503c\uff0c\u76f4\u63a5\u5c06 threshold \u8bbe\u4e3a Integer.MAX_VALUE\r\n    if (oldCapacity == MAXIMUM_CAPACITY) {  \r\n        threshold = Integer.MAX_VALUE;\r\n        return;             \/\/ \u76f4\u63a5\u8fd4\u56de\r\n    }\r\n\r\n    \/\/ \u5426\u5219\uff0c\u521b\u5efa\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u7ec4\r\n    Entry[] newTable = new Entry[newCapacity];\r\n\r\n    \/\/\u5c06\u6bcf\u6761Entry\u91cd\u65b0\u54c8\u5e0c\u5230\u65b0\u7684\u6570\u7ec4\u4e2d\r\n    transfer(newTable);\r\n\r\n    table = newTable;\r\n    threshold = (int)(newCapacity * loadFactor);  \/\/ \u91cd\u65b0\u8bbe\u5b9a threshold\r\n}<\/pre>\n<p>\u8be5\u65b9\u6cd5\u7684\u4f5c\u7528\u53ca\u89e6\u53d1\u52a8\u673a\u5982\u4e0b\uff1a<\/p>\n<p>Rehashes the contents of this map into a new array with a larger capacity. This method is called automatically when the number of keys in this map reaches its threshold.<\/p>\n<hr \/>\n<p>5). HashMap \u7684\u91cd\u54c8\u5e0c\uff1atransfer()<\/p>\n<p><span style=\"color: red;\"><b>\u91cd\u54c8\u5e0c\u7684\u4e3b\u8981\u662f\u4e00\u4e2a\u91cd\u65b0\u8ba1\u7b97\u539fHashMap\u4e2d\u7684\u5143\u7d20\u5728\u65b0table\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\u5e76\u8fdb\u884c\u590d\u5236\u5904\u7406\u7684\u8fc7\u7a0b\uff0c<\/b><\/span>\u6211\u4eec\u76f4\u63a5\u770b\u5176\u6e90\u7801\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/**\r\n * Transfers all entries from current table to newTable.\r\n *\/\r\nvoid transfer(Entry[] newTable) {\r\n\r\n    \/\/ \u5c06\u539f\u6570\u7ec4 table \u8d4b\u7ed9\u6570\u7ec4 src\r\n    Entry[] src = table;\r\n    int newCapacity = newTable.length;\r\n\r\n    \/\/ \u5c06\u6570\u7ec4 src \u4e2d\u7684\u6bcf\u6761\u94fe\u91cd\u65b0\u6dfb\u52a0\u5230 newTable \u4e2d\r\n    for (int j = 0; j &lt; src.length; j++) {\r\n        Entry&lt;K,V&gt; e = src[j];\r\n        if (e != null) {\r\n            src[j] = null;   \/\/ src \u56de\u6536\r\n\r\n            \/\/ \u5c06\u6bcf\u6761\u94fe\u7684\u6bcf\u4e2a\u5143\u7d20\u4f9d\u6b21\u6dfb\u52a0\u5230 newTable \u4e2d\u76f8\u5e94\u7684\u6876\u4e2d\r\n            do {\r\n                Entry&lt;K,V&gt; next = e.next;\r\n\r\n                \/\/ e.hash\u6307\u7684\u662f hash(key.hashCode())\u7684\u8fd4\u56de\u503c;\r\n                \/\/ \u8ba1\u7b97\u5728newTable\u4e2d\u7684\u4f4d\u7f6e\uff0c\u6ce8\u610f\u539f\u6765\u5728\u540c\u4e00\u6761\u5b50\u94fe\u4e0a\u7684\u5143\u7d20\u53ef\u80fd\u88ab\u5206\u914d\u5230\u4e0d\u540c\u7684\u5b50\u94fe\r\n                int i = indexFor(e.hash, newCapacity);   \r\n                e.next = newTable[i];\r\n                newTable[i] = e;\r\n                e = next;\r\n            } while (e != null);\r\n        }\r\n    }\r\n}<\/pre>\n<p>\u7279\u522b\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u91cd\u54c8\u5e0c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u539f\u5c5e\u4e8e\u4e00\u4e2a\u6876\u4e2d\u7684Entry\u5bf9\u8c61\u53ef\u80fd\u88ab\u5206\u5230\u4e0d\u540c\u7684\u6876\uff0c\u56e0\u4e3aHashMap \u7684\u5bb9\u91cf\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u90a3\u4e48\u00a0<span style=\"color: red;\"><b>h&amp;(length &#8211; 1)\u00a0<\/b><\/span>\u7684\u503c\u4e5f\u4f1a\u53d1\u751f\u76f8\u5e94\u7684\u53d8\u5316\u3002\u6781\u7aef\u5730\u8bf4\uff0c\u5982\u679c\u91cd\u54c8\u5e0c\u540e\uff0c\u539f\u5c5e\u4e8e\u4e00\u4e2a\u6876\u4e2d\u7684Entry\u5bf9\u8c61\u4ecd\u5c5e\u4e8e\u540c\u4e00\u6876\uff0c\u90a3\u4e48\u91cd\u54c8\u5e0c\u4e5f\u5c31\u5931\u53bb\u4e86\u610f\u4e49\u3002<\/p>\n<hr \/>\n<p><strong>2\u3001HashMap \u7684\u8bfb\u53d6\u5b9e\u73b0<\/strong><\/p>\n<p>\u76f8\u5bf9\u4e8eHashMap\u7684\u5b58\u50a8\u800c\u8a00\uff0c\u8bfb\u53d6\u5c31\u663e\u5f97\u6bd4\u8f83\u7b80\u5355\u4e86\u3002\u56e0\u4e3a\uff0cHashMap\u53ea\u9700\u901a\u8fc7key\u7684hash\u503c\u5b9a\u4f4d\u5230table\u6570\u7ec4\u7684\u67d0\u4e2a\u7279\u5b9a\u7684\u6876\uff0c\u7136\u540e\u67e5\u627e\u5e76\u8fd4\u56de\u8be5key\u5bf9\u5e94\u7684value\u5373\u53ef\uff0c\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/**\r\n     * Returns the value to which the specified key is mapped,\r\n     * or {@code null} if this map contains no mapping for the key.\r\n     *\r\n     * &lt;p&gt;More formally, if this map contains a mapping from a key\r\n     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :\r\n     * key.equals(k))}, then this method returns {@code v}; otherwise\r\n     * it returns {@code null}.  (There can be at most one such mapping.)\r\n     *\r\n     * &lt;p&gt;A return value of {@code null} does not &lt;i&gt;necessarily&lt;\/i&gt;\r\n     * indicate that the map contains no mapping for the key; it's also\r\n     * possible that the map explicitly maps the key to {@code null}.\r\n     * The {@link #containsKey containsKey} operation may be used to\r\n     * distinguish these two cases.\r\n     *\r\n     * @see #put(Object, Object)\r\n     *\/\r\n    public V get(Object key) {\r\n        \/\/ \u82e5\u4e3anull\uff0c\u8c03\u7528getForNullKey\u65b9\u6cd5\u8fd4\u56de\u76f8\u5bf9\u5e94\u7684value\r\n        if (key == null)\r\n            \/\/ \u4ecetable\u7684\u7b2c\u4e00\u4e2a\u6876\u4e2d\u5bfb\u627e key \u4e3a null \u7684\u6620\u5c04\uff1b\u82e5\u4e0d\u5b58\u5728\uff0c\u76f4\u63a5\u8fd4\u56denull\r\n            return getForNullKey();  \r\n\r\n        \/\/ \u6839\u636e\u8be5 key \u7684 hashCode \u503c\u8ba1\u7b97\u5b83\u7684 hash \u7801 \r\n        int hash = hash(key.hashCode());\r\n        \/\/ \u627e\u51fa table \u6570\u7ec4\u4e2d\u5bf9\u5e94\u7684\u6876\r\n        for (Entry&lt;K,V&gt; e = table[indexFor(hash, table.length)];\r\n             e != null;\r\n             e = e.next) {\r\n            Object k;\r\n            \/\/\u82e5\u641c\u7d22\u7684key\u4e0e\u67e5\u627e\u7684key\u76f8\u540c\uff0c\u5219\u8fd4\u56de\u76f8\u5bf9\u5e94\u7684value\r\n            if (e.hash == hash &amp;&amp; ((k = e.key) == key || key.equals(k)))\r\n                return e.value;\r\n        }\r\n        return null;\r\n    }<\/pre>\n<p>\u5728\u8fd9\u91cc\u80fd\u591f\u6839\u636ekey\u5feb\u901f\u7684\u53d6\u5230value\uff0c\u9664\u4e86\u548cHashMap\u7684\u6570\u636e\u7ed3\u6784\u5bc6\u4e0d\u53ef\u5206\u5916\uff0c\u8fd8\u548cEntry\u6709\u83ab\u5927\u7684\u5173\u7cfb\u3002\u5728\u524d\u9762\u5c31\u5df2\u7ecf\u63d0\u5230\u8fc7\uff0cHashMap\u5728\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u5e76\u6ca1\u6709\u5c06key\uff0cvalue\u5206\u5f00\u6765\u5b58\u50a8\uff0c\u800c\u662f\u5f53\u505a\u4e00\u4e2a\u6574\u4f53key-value\u6765\u5904\u7406\u7684\uff0c\u8fd9\u4e2a\u6574\u4f53\u5c31\u662fEntry\u5bf9\u8c61\u3002\u7279\u522b\u5730\uff0c\u5728Entry\u5bf9\u8c61\u4e2d\uff0cvalue\u7684\u5730\u4f4d\u8981\u6bd4key\u4f4e\u4e00\u4e9b\uff0c\u76f8\u5f53\u4e8e\u662f key \u7684\u9644\u5c5e\u3002<\/p>\n<hr \/>\n<p>\u5176\u4e2d\uff0c\u9488\u5bf9\u952e\u4e3aNULL\u7684\u952e\u503c\u5bf9\uff0cHashMap \u63d0\u4f9b\u4e86\u4e13\u95e8\u7684\u5904\u7406\uff1agetForNullKey()\uff0c\u5176\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/**\r\n    * Offloaded version of get() to look up null keys.  Null keys map\r\n    * to index 0.  This null case is split out into separate methods\r\n    * for the sake of performance in the two most commonly used\r\n    * operations (get and put), but incorporated with conditionals in\r\n    * others.\r\n    *\/\r\n   private V getForNullKey() {\r\n       \/\/ \u952e\u4e3aNULL\u7684\u952e\u503c\u5bf9\u82e5\u5b58\u5728\uff0c\u5219\u5fc5\u5b9a\u5728\u7b2c\u4e00\u4e2a\u6876\u4e2d\r\n       for (Entry&lt;K,V&gt; e = table[0]; e != null; e = e.next) {\r\n           if (e.key == null)\r\n               return e.value;\r\n       }\r\n       \/\/ \u952e\u4e3aNULL\u7684\u952e\u503c\u5bf9\u82e5\u4e0d\u5b58\u5728\uff0c\u5219\u76f4\u63a5\u8fd4\u56de null\r\n       return null;\r\n   }<\/pre>\n<p>\u56e0\u6b64\uff0c\u8c03\u7528HashMap\u7684get(Object key)\u65b9\u6cd5\u540e\uff0c\u82e5\u8fd4\u56de\u503c\u662f NULL\uff0c\u5219\u5b58\u5728\u5982\u4e0b\u4e24\u79cd\u53ef\u80fd\uff1a<\/p>\n<ul>\n<li><span style=\"color: red;\"><b>\u8be5 key \u5bf9\u5e94\u7684\u503c\u5c31\u662f null;<\/b><\/span><\/li>\n<li><span style=\"color: red;\"><b>HashMap \u4e2d\u4e0d\u5b58\u5728\u8be5 key\u3002<\/b><\/span><\/li>\n<\/ul>\n<hr \/>\n<p><strong>3\u3001HashMap \u5b58\u53d6\u5c0f\u7ed3<\/strong><\/p>\n<p>\u5728\u5b58\u50a8\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7cfb\u7edf\u6839\u636ekey\u7684hash\u503c\u6765\u5b9a\u4f4dEntry\u5728table\u6570\u7ec4\u4e2d\u7684\u54ea\u4e2a\u6876\uff0c\u5e76\u4e14\u5c06\u5176\u653e\u5230\u5bf9\u5e94\u7684\u94fe\u8868\u7684\u94fe\u5934\uff1b\u5728\u53d6\u7684\u8fc7\u7a0b\u4e2d\uff0c\u540c\u6837\u6839\u636ekey\u7684hash\u503c\u6765\u5b9a\u4f4dEntry\u5728table\u6570\u7ec4\u4e2d\u7684\u54ea\u4e2a\u6876\uff0c\u7136\u540e\u5728\u8be5\u6876\u4e2d\u67e5\u627e\u5e76\u8fd4\u56de\u3002<\/p>\n<hr \/>\n<h2 id=\"\u516d-hashmap-\u7684\u5e95\u5c42\u6570\u7ec4\u957f\u5ea6\u4e3a\u4f55\u603b\u662f2\u7684n\u6b21\u65b9\"><a name=\"t5\"><\/a>\u516d. HashMap \u7684\u5e95\u5c42\u6570\u7ec4\u957f\u5ea6\u4e3a\u4f55\u603b\u662f2\u7684n\u6b21\u65b9\uff1f<\/h2>\n<p>\u6211\u4eec\u77e5\u9053\uff0c<span style=\"color: red;\"><b>HashMap\u7684\u5e95\u5c42\u6570\u7ec4\u957f\u5ea6\u603b\u662f2\u7684n\u6b21\u65b9<\/b><\/span>\uff0c\u539f\u56e0\u662f HashMap \u5728\u5176\u6784\u9020\u51fd\u6570 HashMap(int initialCapacity, float loadFactor) \u4e2d\u4f5c\u4e86\u7279\u522b\u7684\u5904\u7406\uff0c\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u6240\u793a\u3002\u5f53\u5e95\u5c42\u6570\u7ec4\u7684length\u4e3a2\u7684n\u6b21\u65b9\u65f6\uff0c h&amp;(length &#8211; 1) \u5c31\u76f8\u5f53\u4e8e\u5bf9length\u53d6\u6a21\uff0c\u5176\u6548\u7387\u8981\u6bd4\u76f4\u63a5\u53d6\u6a21\u9ad8\u5f97\u591a\uff0c\u8fd9\u662fHashMap\u5728\u6548\u7387\u4e0a\u7684\u4e00\u4e2a\u4f18\u5316\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/\/ HashMap \u7684\u5bb9\u91cf\u5fc5\u987b\u662f2\u7684\u5e42\u6b21\u65b9\uff0c\u8d85\u8fc7 initialCapacity \u7684\u6700\u5c0f 2^n \r\nint capacity = 1;\r\nwhile (capacity &lt; initialCapacity)\r\n    capacity &lt;&lt;= 1;<\/pre>\n<p>\u5728\u4e0a\u6587\u5df2\u7ecf\u63d0\u5230\u8fc7\uff0cHashMap \u4e2d\u7684\u6570\u636e\u7ed3\u6784\u662f\u4e00\u4e2a\u6570\u7ec4\u94fe\u8868\uff0c\u6211\u4eec\u5e0c\u671b\u7684\u662f\u5143\u7d20\u5b58\u653e\u7684\u8d8a\u5747\u5300\u8d8a\u597d\u3002\u6700\u7406\u60f3\u7684\u6548\u679c\u662f\uff0cEntry\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u4f4d\u7f6e\u90fd\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff0c\u8fd9\u6837\uff0c\u67e5\u8be2\u7684\u65f6\u5019\u6548\u7387\u6700\u9ad8\uff0c\u4e0d\u9700\u8981\u904d\u5386\u5355\u94fe\u8868\uff0c\u4e5f\u4e0d\u9700\u8981\u901a\u8fc7equals\u53bb\u6bd4\u8f83Key\uff0c\u800c\u4e14\u7a7a\u95f4\u5229\u7528\u7387\u6700\u5927\u3002<\/p>\n<p>\u90a3\u5982\u4f55\u8ba1\u7b97\u624d\u4f1a\u5206\u5e03\u6700\u5747\u5300\u5462\uff1f\u6b63\u5982\u4e0a\u4e00\u8282\u63d0\u5230\u7684\uff0cHashMap\u91c7\u7528\u4e86\u4e00\u4e2a\u5206\u4e24\u6b65\u8d70\u7684\u54c8\u5e0c\u7b56\u7565\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 hash() \u65b9\u6cd5\u7528\u4e8e\u5bf9Key\u7684hashCode\u8fdb\u884c\u91cd\u65b0\u8ba1\u7b97\uff0c\u4ee5\u9632\u6b62\u8d28\u91cf\u4f4e\u4e0b\u7684hashCode()\u51fd\u6570\u5b9e\u73b0\u3002\u7531\u4e8ehashMap\u7684\u652f\u6491\u6570\u7ec4\u957f\u5ea6\u603b\u662f 2 \u7684\u500d\u6570\uff0c\u901a\u8fc7\u53f3\u79fb\u53ef\u4ee5\u4f7f\u4f4e\u4f4d\u7684\u6570\u636e\u5c3d\u91cf\u7684\u4e0d\u540c\uff0c\u4ece\u800c\u4f7fKey\u7684hash\u503c\u7684\u5206\u5e03\u5c3d\u91cf\u5747\u5300\uff1b<\/li>\n<li>\u4f7f\u7528 indexFor() \u65b9\u6cd5\u8fdb\u884c\u53d6\u4f59\u8fd0\u7b97\uff0c\u4ee5\u4f7fEntry\u5bf9\u8c61\u7684\u63d2\u5165\u4f4d\u7f6e\u5c3d\u91cf\u5206\u5e03\u5747\u5300(\u4e0b\u6587\u5c06\u4e13\u95e8\u5bf9\u6b64\u9610\u8ff0)\u3002<\/li>\n<\/ul>\n<p>\u5bf9\u4e8e\u53d6\u4f59\u8fd0\u7b97\uff0c\u6211\u4eec\u9996\u5148\u60f3\u5230\u7684\u662f\uff1a\u54c8\u5e0c\u503c%length = bucketIndex\u3002<span style=\"color: red;\"><b>\u4f46\u5f53\u5e95\u5c42\u6570\u7ec4\u7684length\u4e3a2\u7684n\u6b21\u65b9\u65f6\uff0c\u00a0h&amp;(length &#8211; 1)\u00a0\u5c31\u76f8\u5f53\u4e8e\u5bf9length\u53d6\u6a21\uff0c\u800c\u4e14\u901f\u5ea6\u6bd4\u76f4\u63a5\u53d6\u6a21\u5feb\u5f97\u591a\uff0c\u8fd9\u662fHashMap\u5728\u901f\u5ea6\u4e0a\u7684\u4e00\u4e2a\u4f18\u5316\u3002<\/b><\/span>\u9664\u6b64\u4e4b\u5916\uff0cHashMap \u7684\u5e95\u5c42\u6570\u7ec4\u957f\u5ea6\u603b\u662f2\u7684n\u6b21\u65b9\u7684\u4e3b\u8981\u539f\u56e0\u662f\u4ec0\u4e48\u5462\uff1f\u6211\u4eec\u501f\u52a9\u4e8e chenssy \u5728\u5176\u535a\u5ba2<a href=\"http:\/\/www.cnblogs.com\/chenssy\/p\/3521565.html\" target=\"_blank\" rel=\"nofollow noopener\">\u300ajava\u63d0\u9ad8\u7bc7\uff08\u4e8c\u4e09\uff09\u2014\u2013HashMap\u300b<\/a>\u00a0\u4e2d\u7684\u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\u7684\u9610\u8ff0\uff1a<\/p>\n<p>\u8fd9\u91cc\uff0c\u6211\u4eec\u5047\u8bbelength\u5206\u522b\u4e3a16(2^4) \u548c 15\uff0ch \u5206\u522b\u4e3a 5\u30016\u30017\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1993\" src=\"https:\/\/damogame.cn:15443\/wordpress\/wp-content\/uploads\/2018\/10\/20170317222404822.jpeg\" alt=\"\" width=\"590\" height=\"211\" \/><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5f53n=15\u65f6\uff0c6\u548c7\u7684\u7ed3\u679c\u4e00\u6837\uff0c\u5373\u5b83\u4eec\u4f4d\u4e8etable\u7684\u540c\u4e00\u4e2a\u6876\u4e2d\uff0c\u4e5f\u5c31\u662f\u4ea7\u751f\u4e86\u78b0\u649e\uff0c6\u30017\u5c31\u4f1a\u5728\u8fd9\u4e2a\u6876\u4e2d\u5f62\u6210\u94fe\u8868\uff0c\u8fd9\u6837\u5c31\u4f1a\u5bfc\u81f4\u67e5\u8be2\u901f\u5ea6\u964d\u4f4e\u3002\u8bda\u7136\u8fd9\u91cc\u53ea\u5206\u6790\u4e09\u4e2a\u6570\u5b57\u4e0d\u662f\u5f88\u591a\uff0c\u90a3\u4e48\u6211\u4eec\u518d\u770b h \u5206\u522b\u53d6 0-15\u65f6\u7684\u60c5\u51b5\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1994\" src=\"https:\/\/damogame.cn:15443\/wordpress\/wp-content\/uploads\/2018\/10\/20170317222422033.jpeg\" alt=\"\" width=\"596\" height=\"381\" \/><\/p>\n<p>\u4ece\u4e0a\u9762\u7684\u56fe\u8868\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5f53 length \u4e3a15\u65f6\u603b\u5171\u53d1\u751f\u4e868\u6b21\u78b0\u649e\uff0c\u540c\u65f6\u53d1\u73b0\u7a7a\u95f4\u6d6a\u8d39\u975e\u5e38\u5927\uff0c\u56e0\u4e3a\u5728 1\u30013\u30015\u30017\u30019\u300111\u300113\u300115 \u8fd9\u516b\u5904\u6ca1\u6709\u5b58\u653e\u6570\u636e\u3002\u8fd9\u662f\u56e0\u4e3ahash\u503c\u5728\u4e0e14\uff08\u5373 1110\uff09\u8fdb\u884c&amp;\u8fd0\u7b97\u65f6\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u6700\u540e\u4e00\u4f4d\u6c38\u8fdc\u90fd\u662f0\uff0c\u5373 0001\u30010011\u30010101\u30010111\u30011001\u30011011\u30011101\u30011111\u4f4d\u7f6e\u5904\u662f\u4e0d\u53ef\u80fd\u5b58\u50a8\u6570\u636e\u7684\u3002\u8fd9\u6837\uff0c\u7a7a\u95f4\u7684\u51cf\u5c11\u4f1a\u5bfc\u81f4\u78b0\u649e\u51e0\u7387\u7684\u8fdb\u4e00\u6b65\u589e\u52a0\uff0c\u4ece\u800c\u5c31\u4f1a\u5bfc\u81f4\u67e5\u8be2\u901f\u5ea6\u6162\u3002<\/p>\n<p>\u800c\u5f53length\u4e3a16\u65f6\uff0clength \u2013 1 = 15\uff0c \u5373 1111\uff0c\u90a3\u4e48\uff0c<span style=\"color: red;\"><b>\u5728\u8fdb\u884c\u4f4e\u4f4d&amp;\u8fd0\u7b97\u65f6\uff0c\u503c\u603b\u662f\u4e0e\u539f\u6765hash\u503c\u76f8\u540c\uff0c\u800c\u8fdb\u884c\u9ad8\u4f4d\u8fd0\u7b97\u65f6\uff0c\u5176\u503c\u7b49\u4e8e\u5176\u4f4e\u4f4d\u503c\u3002<\/b><\/span>\u6240\u4ee5\uff0c\u5f53 length=2^n \u65f6\uff0c\u4e0d\u540c\u7684hash\u503c\u53d1\u751f\u78b0\u649e\u7684\u6982\u7387\u6bd4\u8f83\u5c0f\uff0c\u8fd9\u6837\u5c31\u4f1a\u4f7f\u5f97\u6570\u636e\u5728table\u6570\u7ec4\u4e2d\u5206\u5e03\u8f83\u5747\u5300\uff0c\u67e5\u8be2\u901f\u5ea6\u4e5f\u8f83\u5feb\u3002<\/p>\n<hr \/>\n<p>\u56e0\u6b64\uff0c\u603b\u7684\u6765\u8bf4\uff0cHashMap \u7684\u5e95\u5c42\u6570\u7ec4\u957f\u5ea6\u603b\u662f2\u7684n\u6b21\u65b9\u7684\u539f\u56e0\u6709\u4e24\u4e2a\uff0c\u5373\u5f53 length=2^n \u65f6\uff1a<\/p>\n<ul>\n<li><span style=\"color: red;\"><b>\u4e0d\u540c\u7684hash\u503c\u53d1\u751f\u78b0\u649e\u7684\u6982\u7387\u6bd4\u8f83\u5c0f\uff0c\u8fd9\u6837\u5c31\u4f1a\u4f7f\u5f97\u6570\u636e\u5728table\u6570\u7ec4\u4e2d\u5206\u5e03\u8f83\u5747\u5300\uff0c\u7a7a\u95f4\u5229\u7528\u7387\u8f83\u9ad8\uff0c\u67e5\u8be2\u901f\u5ea6\u4e5f\u8f83\u5feb\uff1b<\/b><\/span><\/li>\n<li><span style=\"color: red;\"><b>h&amp;(length &#8211; 1) \u5c31\u76f8\u5f53\u4e8e\u5bf9length\u53d6\u6a21\uff0c\u800c\u4e14\u5728\u901f\u5ea6\u3001\u6548\u7387\u4e0a\u6bd4\u76f4\u63a5\u53d6\u6a21\u8981\u5feb\u5f97\u591a\uff0c\u5373\u4e8c\u8005\u662f\u7b49\u4ef7\u4e0d\u7b49\u6548\u7684\uff0c\u8fd9\u662fHashMap\u5728\u901f\u5ea6\u548c\u6548\u7387\u4e0a\u7684\u4e00\u4e2a\u4f18\u5316\u3002<\/b><\/span><\/li>\n<\/ul>\n<hr \/>\n<h2 id=\"\u4e03-\u66f4\u591a\"><a name=\"t6\"><\/a>\u4e03. \u66f4\u591a<\/h2>\n<p>HashMap \u7684\u76f4\u63a5\u5b50\u7c7bLinkedHashMap\u7ee7\u627f\u4e86HashMap\u7684\u6240\u7528\u7279\u6027\uff0c\u5e76\u4e14\u8fd8\u901a\u8fc7\u989d\u5916\u7ef4\u62a4\u4e00\u4e2a\u53cc\u5411\u94fe\u8868\u4fdd\u6301\u4e86\u6709\u5e8f\u6027, \u901a\u8fc7\u5bf9\u6bd4LinkedHashMap\u548cHashMap\u7684\u5b9e\u73b0\u6709\u52a9\u4e8e\u66f4\u597d\u7684\u7406\u89e3HashMap\u3002\u5173\u4e8eLinkedHashMap\u7684\u66f4\u591a\u4ecb\u7ecd\uff0c\u8bf7\u53c2\u89c1\u6211\u7684\u53e6\u4e00\u7bc7\u535a\u6587<a href=\"http:\/\/blog.csdn.net\/justloveyou_\/article\/details\/71713781\" target=\"_blank\" rel=\"nofollow noopener\">\u300aMap \u7efc\u8ff0\uff08\u4e8c\uff09\uff1a\u5f7b\u5934\u5f7b\u5c3e\u7406\u89e3 LinkedHashMap\u300b<\/a>\uff0c\u6b22\u8fce\u6307\u6b63~<\/p>\n<p>\u66f4\u591a\u5173\u4e8e\u54c8\u5e0c(Hash)\u548cequals\u65b9\u6cd5\u7684\u4ecb\u7ecd\uff0c\u8bf7\u79fb\u6b65\u6211\u7684\u535a\u6587<a href=\"http:\/\/blog.csdn.net\/justloveyou_\/article\/details\/52464440\" target=\"_blank\" rel=\"nofollow noopener\">\u300aJava \u4e2d\u7684 ==, equals \u4e0e hashCode \u7684\u533a\u522b\u4e0e\u8054\u7cfb\u300b<\/a>\u3002<\/p>\n<p>\u66f4\u591a\u5173\u4e8e Java SE \u8fdb\u9636 \u65b9\u9762\u7684\u5185\u5bb9\uff0c\u8bf7\u5173\u6ce8\u6211\u7684\u4e13\u680f\u00a0<a href=\"http:\/\/blog.csdn.net\/column\/details\/14840.html\" target=\"_blank\" rel=\"nofollow noopener\">\u300aJava SE \u8fdb\u9636\u4e4b\u8def\u300b<\/a>\u3002\u672c\u4e13\u680f\u4e3b\u8981\u7814\u7a76Java\u57fa\u7840\u77e5\u8bc6\u3001Java\u6e90\u7801\u548c\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u4ece\u521d\u7ea7\u5230\u9ad8\u7ea7\u4e0d\u65ad\u603b\u7ed3\u3001\u5256\u6790\u5404\u77e5\u8bc6\u70b9\u7684\u5185\u5728\u903b\u8f91\uff0c\u8d2f\u7a7f\u3001\u8986\u76d6\u6574\u4e2aJava\u77e5\u8bc6\u9762\uff0c\u5728\u4e00\u6b65\u6b65\u5b8c\u5584\u3001\u63d0\u9ad8\u628a\u81ea\u5df1\u7684\u540c\u65f6\uff0c\u628a\u5bf9Java\u7684\u6240\u5b66\u6240\u601d\u5206\u4eab\u7ed9\u5927\u5bb6\u3002\u4e07\u4e08\u9ad8\u697c\u5e73\u5730\u8d77\uff0c\u57fa\u7840\u51b3\u5b9a\u4f60\u7684\u4e0a\u9650\uff0c\u8ba9\u6211\u4eec\u643a\u624b\u4e00\u8d77\u52c7\u6500Java\u4e4b\u5dc5\u2026<\/p>\n<hr \/>\n<h2 id=\"\u5f15\u7528\"><a name=\"t7\"><\/a>\u5f15\u7528<\/h2>\n<p><a href=\"http:\/\/alex09.iteye.com\/blog\/539545\/\" target=\"_blank\" rel=\"nofollow noopener\">java\u4e2dHashMap\u8be6\u89e3<\/a><br \/>\n<a href=\"http:\/\/www.cnblogs.com\/chenssy\/p\/3521565.html\" target=\"_blank\" rel=\"nofollow noopener\">java\u63d0\u9ad8\u7bc7\uff08\u4e8c\u4e09\uff09\u2014\u2013HashMap<\/a><\/p>\n<\/div>\n<\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981\uff1a \u3000\u3000HashMap\u662fMap\u65cf\u4e2d\u6700<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-1987","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=1987"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1987\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=1987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=1987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=1987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}