{"id":37408,"date":"2023-12-11T14:41:20","date_gmt":"2023-12-11T06:41:20","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=37408"},"modified":"2023-12-11T14:41:20","modified_gmt":"2023-12-11T06:41:20","slug":"hashmap%e7%9a%84%e5%ba%95%e5%b1%82%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/hashmap%e7%9a%84%e5%ba%95%e5%b1%82%e5%ae%9e%e7%8e%b0\/","title":{"rendered":"HashMap\u7684\u5e95\u5c42\u5b9e\u73b0?"},"content":{"rendered":"<p>&#8220;`&#8221;<\/p>\n<h3>JDK1.8\u4e4b\u524d<\/h3>\n<p>JDK1.8 \u4e4b\u524d <code>HashMap<\/code> \u5e95\u5c42\u662f <strong>\u6570\u7ec4\u548c\u94fe\u8868<\/strong> \u7ed3\u5408\u5728\u4e00\u8d77\u4f7f\u7528\u4e5f\u5c31\u662f <strong>\u94fe\u8868\u6563\u5217<\/strong>\u3002<strong>HashMap \u901a\u8fc7 key \u7684 hashCode \u7ecf\u8fc7\u6270\u52a8\u51fd\u6570\u5904\u7406\u8fc7\u540e\u5f97\u5230 hash \u503c\uff0c\u7136\u540e\u901a\u8fc7 (n &#8211; 1) &amp; hash \u5224\u65ad\u5f53\u524d\u5143\u7d20\u5b58\u653e\u7684\u4f4d\u7f6e\uff08\u8fd9\u91cc\u7684 n \u6307\u7684\u662f\u6570\u7ec4\u7684\u957f\u5ea6\uff09\uff0c\u5982\u679c\u5f53\u524d\u4f4d\u7f6e\u5b58\u5728\u5143\u7d20\u7684\u8bdd\uff0c\u5c31\u5224\u65ad\u8be5\u5143\u7d20\u4e0e\u8981\u5b58\u5165\u7684\u5143\u7d20\u7684 hash \u503c\u4ee5\u53ca key \u662f\u5426\u76f8\u540c\uff0c\u5982\u679c\u76f8\u540c\u7684\u8bdd\uff0c\u76f4\u63a5\u8986\u76d6\uff0c\u4e0d\u76f8\u540c\u5c31\u901a\u8fc7\u62c9\u94fe\u6cd5\u89e3\u51b3\u51b2\u7a81\u3002<\/strong><\/p>\n<p><strong>\u6240\u8c13\u6270\u52a8\u51fd\u6570\u6307\u7684\u5c31\u662f HashMap \u7684 hash \u65b9\u6cd5\u3002\u4f7f\u7528 hash \u65b9\u6cd5\u4e5f\u5c31\u662f\u6270\u52a8\u51fd\u6570\u662f\u4e3a\u4e86\u9632\u6b62\u4e00\u4e9b\u5b9e\u73b0\u6bd4\u8f83\u5dee\u7684 hashCode() \u65b9\u6cd5 \u6362\u53e5\u8bdd\u8bf4\u4f7f\u7528\u6270\u52a8\u51fd\u6570\u4e4b\u540e\u53ef\u4ee5\u51cf\u5c11\u78b0\u649e\u3002<\/strong><\/p>\n<p><strong>JDK 1.8 HashMap \u7684 hash \u65b9\u6cd5\u6e90\u7801:<\/strong><\/p>\n<p>JDK 1.8 \u7684 hash\u65b9\u6cd5 \u76f8\u6bd4\u4e8e JDK 1.7 hash \u65b9\u6cd5\u66f4\u52a0\u7b80\u5316\uff0c\u4f46\u662f\u539f\u7406\u4e0d\u53d8\u3002<\/p>\n<pre><code>java    static final int hash(Object key) {      int h;      \/\/ key.hashCode()\uff1a\u8fd4\u56de\u6563\u5217\u503c\u4e5f\u5c31\u662fhashcode      \/\/ ^ \uff1a\u6309\u4f4d\u5f02\u6216      \/\/ &gt;&gt;&gt;:\u65e0\u7b26\u53f7\u53f3\u79fb\uff0c\u5ffd\u7565\u7b26\u53f7\u4f4d\uff0c\u7a7a\u4f4d\u90fd\u4ee50\u8865\u9f50      return (key == null) ? 0 : (h = key.hashCode()) ^ (h &gt;&gt;&gt; 16);  }<\/code><\/pre>\n<p>\u5bf9\u6bd4\u4e00\u4e0b JDK1.7\u7684 HashMap \u7684 hash \u65b9\u6cd5\u6e90\u7801.<\/p>\n<pre>`javastatic int hash(int h) {    \/\/ This function ensures that hashCodes that differ only by    \/\/ constant multiples at each bit position have a bounded    \/\/ number of collisions (approximately 8 at default load factor).h ^= (h &gt;&gt;&gt; 20) ^ (h &gt;&gt;&gt; 12);return h ^ (h &gt;&gt;&gt; 7) ^ (h &gt;&gt;&gt; 4);}<code><\/code>`<\/pre>\n<p><code><\/code><\/p>\n<p>\u76f8\u6bd4\u4e8e JDK1.8 \u7684 hash \u65b9\u6cd5 \uff0cJDK 1.7 \u7684 hash \u65b9\u6cd5\u7684\u6027\u80fd\u4f1a\u7a0d\u5dee\u4e00\u70b9\u70b9\uff0c\u56e0\u4e3a\u6bd5\u7adf\u6270\u52a8\u4e86 4 \u6b21\u3002<\/p>\n<p>\u6240\u8c13 <strong>\u201c\u62c9\u94fe\u6cd5\u201d<\/strong> \u5c31\u662f\uff1a\u5c06\u94fe\u8868\u548c\u6570\u7ec4\u76f8\u7ed3\u5408\u3002\u4e5f\u5c31\u662f\u8bf4\u521b\u5efa\u4e00\u4e2a\u94fe\u8868\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u6bcf\u4e00\u683c\u5c31\u662f\u4e00\u4e2a\u94fe\u8868\u3002\u82e5\u9047\u5230\u54c8\u5e0c\u51b2\u7a81\uff0c\u5219\u5c06\u51b2\u7a81\u7684\u503c\u52a0\u5230\u94fe\u8868\u4e2d\u5373\u53ef\u3002<\/p>\n<p><img decoding=\"async\" src=\"\"http:\/\/res.mianshigee.com\/upload\/article\/20200423\/0d186045-bf36-46ac-a9b5-93ba12023061.jpg\"\" style=\"\"width: 348px;\"\"><\/p>\n<h3>JDK1.8\u4e4b\u540e<\/h3>\n<p>\u76f8\u6bd4\u4e8e\u4e4b\u524d\u7684\u7248\u672c\uff0c JDK1.8\u4e4b\u540e\u5728\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u65f6\u6709\u4e86\u8f83\u5927\u7684\u53d8\u5316\uff0c\u5f53\u94fe\u8868\u957f\u5ea6\u5927\u4e8e\u9608\u503c\uff08\u9ed8\u8ba4\u4e3a8\uff09\u65f6\uff0c\u5c06\u94fe\u8868\u8f6c\u5316\u4e3a\u7ea2\u9ed1\u6811\uff0c\u4ee5\u51cf\u5c11\u641c\u7d22\u65f6\u95f4\u3002<\/p>\n<p><img decoding=\"async\" src=\"\"http:\/\/res.mianshigee.com\/upload\/article\/20200423\/648b431e-0568-4b83-9123-389d5e34e802.jpg\"\" style=\"\"width: 861px;\"\"><\/p>\n<blockquote><p>TreeMap\u3001TreeSet\u4ee5\u53caJDK1.8\u4e4b\u540e\u7684HashMap\u5e95\u5c42\u90fd\u7528\u5230\u4e86\u7ea2\u9ed1\u6811\u3002\u7ea2\u9ed1\u6811\u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u4e8c\u53c9\u67e5\u627e\u6811\u7684\u7f3a\u9677\uff0c\u56e0\u4e3a\u4e8c\u53c9\u67e5\u627e\u6811\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u9000\u5316\u6210\u4e00\u4e2a\u7ebf\u6027\u7ed3\u6784\u3002<\/p>\n<\/blockquote>\n<p><strong>\u63a8\u8350\u9605\u8bfb\uff1a<\/strong><\/p>\n<ul>\n<li>\u300aJava 8\u7cfb\u5217\u4e4b\u91cd\u65b0\u8ba4\u8bc6HashMap\u300b \uff1a<https: zhuanlan.zhihu.com=\"\"\"\" p=\"\"\"\" 21673805<=\"\"\"\" li=\"\"\"\"><\/https:><\/li>\n<\/ul>\n<pre><code>            \"```\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; JDK1.8\u4e4b\u524d JDK1.8 \u4e4b\u524d HashMap \u5e95\u5c42\u662f \u6570\u7ec4\u548c\u94fe\u8868 \u7ed3\u5408 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-37408","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=37408"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37408\/revisions"}],"predecessor-version":[{"id":37409,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37408\/revisions\/37409"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=37408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=37408"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=37408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}