{"id":3383,"date":"2018-11-20T14:52:40","date_gmt":"2018-11-20T06:52:40","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3383"},"modified":"2018-11-20T14:58:05","modified_gmt":"2018-11-20T06:58:05","slug":"%e4%bb%8e-hashmap-%e7%9a%84%e6%89%a9%e5%ae%b9%e6%9c%ba%e5%88%b6%e6%9d%a5%e8%af%b4%e5%ae%83%e4%b8%ba%e4%bb%80%e4%b9%88%e4%b8%8d%e6%98%af%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e7%9a%84%ef%bc%81","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3383","title":{"rendered":"\u4ece HashMap \u7684\u6269\u5bb9\u673a\u5236\u6765\u8bf4\u5b83\u4e3a\u4ec0\u4e48\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff01"},"content":{"rendered":"<p>\n\t\u6211\u5728\u524d\u9762\u7684\u6587\u7ae0\u300a<a href=\"https:\/\/www.xttblog.com\/?p=3326\" name=\"Java \u7ebf\u7a0b\u5b89\u5168\u76843\u5927\u6838\u5fc3\uff1a\u539f\u5b50\u6027\u3001\u53ef\u89c1\u6027\u3001\u6709\u5e8f\u6027\" target=\"_blank\" title=\"Java \u7ebf\u7a0b\u5b89\u5168\u76843\u5927\u6838\u5fc3\uff1a\u539f\u5b50\u6027\u3001\u53ef\u89c1\u6027\u3001\u6709\u5e8f\u6027 - \u4e1a\u4f59\u8349\">Java \u7ebf\u7a0b\u5b89\u5168\u76843\u5927\u6838\u5fc3\uff1a\u539f\u5b50\u6027\u3001\u53ef\u89c1\u6027\u3001\u6709\u5e8f\u6027<\/a>\u300b\u4e2d\u5df2\u7ecf\u8bf4\u5230\u4e86\u4ec0\u4e48\u662f\u7ebf\u7a0b\u5b89\u5168\uff01\u6839\u636e\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u5bf9\u7167\u7740 HashMap \u6765\u8bf4\u8bf4\u5b83\u4e3a\u4ec0\u4e48\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff1f\n<\/p>\n<p>\n\t\u524d\u9762\u6211\u4e5f\u5f3a\u8c03\u8fc7\u591a\u6b21\uff0c\u56de\u7b54\u662f\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u8bf7\u4ece\uff1a\u539f\u5b50\u6027\u3001\u53ef\u89c1\u6027\u3001\u6709\u5e8f\u6027\uff08\u6709\u7684\u8bf4\u987a\u5e8f\u6027\uff0c\u5176\u5b9e\u662f\u4e00\u4e2a\u610f\u601d\uff093 \u4e2a\u65b9\u9762\u6765\u56de\u7b54\u3002\u53c2\u8003\u7740 3 \u4e2a\u7279\u6027\u6307\u6807\u6765\u8bf4\uff0cHashMap \u7684 put\u3001get\u3001remove\u7b49\u6ca1\u6709\u4e00\u4e2a\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bf4\u8bf4 HashMap \u5728\u5e76\u53d1\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u4f7f\u7528\u662f\u5982\u4f55\u53ef\u80fd\u9020\u6210\u6b7b\u5faa\u73af\u95ee\u9898\u7684\uff01\n<\/p>\n<pre class=\"brush:java;\">\r\npublic class HashMapInfiniteLoopTest {  \r\n    \/\/ \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n    private static HashMap&lt;Integer,String&gt; map = \r\n        new HashMap&lt;Integer,String&gt;(2\uff0c0.75f);  \r\n    public static void main(String[] args) {  \r\n        map.put(5\uff0c &quot;C&quot;);  \r\n        new Thread(&quot;Thread1&quot;) {  \r\n            public void run() {  \r\n                map.put(7, &quot;B&quot;);  \r\n                System.out.println(map);  \r\n            };  \r\n        }.start();  \r\n        new Thread(&quot;Thread2&quot;) {  \r\n            public void run() {  \r\n                map.put(3, &quot;A);  \r\n                System.out.println(map);  \r\n            };  \r\n        }.start();        \r\n    }  \r\n}<\/pre>\n<p>\n\t\u9700\u8981\u8bf4\u660e\u7684\u662f\uff0c\u8fd9\u4e2a\u4f8b\u5b50\u6211\u662f\u5728 JDK 1.7 \u73af\u5883\u4e0b\u6d4b\u8bd5\u7684\uff01\n<\/p>\n<p>\n\t\u73b0\u5728\u6211\u5bf9\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u505a\u4e00\u4e2a\u7b80\u5355\u7684\u89e3\u8bfb\u3002\n<\/p>\n<p>\n\t\u9996\u5148\uff0c\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u957f\u5ea6\u4e3a 2 \u7684 HashMap\uff0cloadFactor=0.75\uff0cthreshold=2*0.75=1\uff0c\u4e5f\u5c31\u662f\u8bf4\u5f53 put \u7b2c\u4e8c\u4e2a key \u7684\u65f6\u5019\uff0c\u6211\u4eec\u7684 map \u5c31\u9700\u8981\u8fdb\u884c resize\uff08\u6269\u5bb9\uff09\u3002\n<\/p>\n<p>\n\t\u4e3a\u4e86\u5feb\u901f\u8bf4\u660e\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff0c\u6211\u4eec\u901a\u8fc7\u8bbe\u7f6e\u65ad\u70b9\u8ba9\u7ebf\u7a0b1\u548c\u7ebf\u7a0b2\u540c\u65f6 debug \u5230 put \u65b9\u6cd5\u4e2d\u8c03\u7528\u7684 transfer \u65b9\u6cd5\u7684\u9996\u884c\u3002\n<\/p>\n<p>\n\t\u8fd9\u65f6\u4e24\u4e2a\u7ebf\u7a0b\u5df2\u7ecf\u6210\u529f\u6dfb\u52a0\u6570\u636e\u3002\u653e\u5f00 thread1 \u7684\u65ad\u70b9\u81f3 transfer \u65b9\u6cd5\u7684&ldquo;Entry next = e.next;&rdquo; \u8fd9\u4e00\u884c\uff1b\u7136\u540e\u653e\u5f00\u7ebf\u7a0b2\u7684\u7684\u65ad\u70b9\uff0c\u8ba9\u7ebf\u7a0b2\u8fdb\u884c resize\u3002\n<\/p>\n<p>\n\t\u6211\u7528\u4e00\u5f20\u56fe\u7247\u6765\u603b\u7ed3\u4e00\u4e0b\u8fd9\u4e2a\u8fc7\u7a0b\uff01\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"JDK 1.7 HashMap \u5f15\u53d1\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/jdk1.7-hashmap-demo.png\" style=\"width: 1388px;height: 628px\" \/>\n<\/p>\n<p>\n\t\u6839\u636e\u4e0a\u56fe\u518d\u7ed3\u5408\u6e90\u4ee3\u7801\u4ee5\u53ca\u8fd0\u884c\u7684\u6848\u4f8b\u5f97\u77e5\uff0cThread1 \u7684 e \u6307\u5411\u4e86key(3)\uff0c\u800c next \u6307\u5411\u4e86 key(7)\uff0c\u5176\u5728\u7ebf\u7a0b\u4e8c rehash \u540e\uff0c\u6307\u5411\u4e86\u7ebf\u7a0b\u4e8c\u91cd\u7ec4\u540e\u7684\u94fe\u8868\u3002\n<\/p>\n<p>\n\t\u7ebf\u7a0b\u4e00\u88ab\u8c03\u5ea6\u56de\u6765\u6267\u884c\uff0c\u5148\u662f\u6267\u884c newTalbe[i] = e\uff0c \u7136\u540e\u662f e = next\uff0c\u5bfc\u81f4\u4e86 e \u6307\u5411\u4e86 key(7)\uff0c\u800c\u4e0b\u4e00\u6b21\u5faa\u73af\u7684 next = e.next \u5bfc\u81f4\u4e86 next \u6307\u5411\u4e86 key(3)\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"JDK1.7 \u591a\u7ebf\u7a0b\u73af\u5883HashMap\u5f15\u53d1\u7684\u6b7b\u5faa\u73af\u95ee\u9898\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/jdk1.7-hashmap-put.png\" style=\"width: 1380px;height: 522px\" \/>\n<\/p>\n<p>\n\te.next = newTable[i] \u5bfc\u81f4 key(3).next \u6307\u5411\u4e86 key(7)\u3002\u8fd9\u65f6\u7684 key(7).next \u5df2\u7ecf\u6307\u5411\u4e86 key(3)\uff0c\u4e8e\u662f\u73af\u5f62\u94fe\u8868\u5c31\u8fd9\u6837\u5f62\u6210\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"HashMap \u5f15\u53d1\u7684\u6b7b\u5faa\u73af\u95ee\u9898\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/jdk1.7-hashMap-infinite-loop.png\" style=\"width: 1384px;height: 482px\" \/>\n<\/p>\n<p>\n\t\u73af\u5f62\u94fe\u8868\u5f62\u6210\u4e4b\u540e\uff0c\u5982\u679c\u540e\u9762\u6709 get(1) \u7684\u64cd\u4f5c\uff0c\u5373\u53d6\u51fa\u8fd9\u4e2a\u5143\u7d20\u7684\u64cd\u4f5c\uff0c\u5c31\u4f1a\u53d1\u751f&nbsp;Infinite Loop\uff08\u6b7b\u5faa\u73af\uff09\u95ee\u9898\u3002\n<\/p>\n<p>\n\t\u4e3a\u4ec0\u4e48\u4f1a\u53d1\u751f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff1f\n<\/p>\n<p>\n\t\u5f88\u7b80\u5355\uff0cHashMap \u7684\u6240\u6709\u64cd\u4f5c\u90fd\u6ca1\u6709\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u7684\u6838\u5fc3 3 \u8981\u7d20\uff0c\u5373\uff1a\u539f\u5b50\u6027\u3001\u53ef\u89c1\u6027\u3001\u987a\u5e8f\u6027\u3002\u6240\u4ee5\u5b83\u53d1\u751f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff0c\u6211\u4e00\u70b9\u4e5f\u4e0d\u60ca\u8bb6\uff01\n<\/p>\n<p>\n\t\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u5f88\u7b80\u5355\uff0c\u4e00\u70b9\u4e5f\u4e0d\u590d\u6742\uff0c\u5982\u679c\u4f60\u770b\u4e0d\u61c2\uff0c\u5efa\u8bae\u4f60\u64cd\u4f5c\u6211\u7684\u64cd\u4f5c\uff0c\u5bf9\u7167\u8bf4\u660e\uff0c\u4e00\u6b65\u4e00\u6b65\u8c03\u8bd5\u3002\n<\/p>\n<p>\n\t\u6700\u540e\uff0c\u6211\u5728\u7559\u4e00\u4e2a\u95ee\u9898\uff0cHashMap \u521d\u59cb\u5316\u540e\uff0c\u5728\u6ca1\u6709\u4efb\u4f55\u5143\u7d20\u7684\u60c5\u51b5\u4e0b\uff0cremove(null) \u4f1a\u62a5\u9519\u5417\uff1f\u4e3a\u4ec0\u4e48\uff1f<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3383\">\u4ece HashMap \u7684\u6269\u5bb9\u673a\u5236\u6765\u8bf4\u5b83\u4e3a\u4ec0\u4e48\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff01<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6211\u5728\u524d\u9762\u7684\u6587\u7ae0\u300aJava \u7ebf\u7a0b\u5b89\u5168\u76843\u5927\u6838\u5fc3\uff1a\u539f\u5b50\u6027\u3001\u53ef\u89c1\u6027\u3001\u6709\u5e8f\u6027\u300b\u4e2d\u5df2\u7ecf\u8bf4\u5230\u4e86\u4ec0\u4e48\u662f\u7ebf\u7a0b\u5b89\u5168\uff01\u6839\u636e\u8fd9\u7bc7\u6587\u7ae0 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1164,1161,565,1163,531],"class_list":["post-3383","post","type-post","status-publish","format-standard","hentry","category-java","tag-concurrenthashmap","tag-hashmap","tag-java-","tag-linkedhashmap","tag-531"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3383"}],"version-history":[{"count":3,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3383\/revisions"}],"predecessor-version":[{"id":3386,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3383\/revisions\/3386"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}