{"id":3387,"date":"2018-11-20T16:26:39","date_gmt":"2018-11-20T08:26:39","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3387"},"modified":"2018-11-20T16:26:39","modified_gmt":"2018-11-20T08:26:39","slug":"hashmap-%e5%9c%a8jdk1-8%e4%b8%8ejdk1-7%e7%9a%84%e6%80%a7%e8%83%bd%e6%b5%8b%e8%af%95%e5%af%b9%e6%af%94","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3387","title":{"rendered":"HashMap \u5728JDK1.8\u4e0eJDK1.7\u7684\u6027\u80fd\u6d4b\u8bd5\u5bf9\u6bd4"},"content":{"rendered":"<p>\n\t\u4efb\u4f55\u6570\u636e\u7ed3\u6784\u7684\u4ea7\u751f\u603b\u5bf9\u5e94\u7740\u8981\u89e3\u51b3\u4e00\u4e2a\u5b9e\u9645\u7684\u95ee\u9898\uff01\u6211\u5728\u300a<a href=\"https:\/\/www.xttblog.com\/?p=3381\" name=\"HashMap \u5b58\u5728\u7684\u610f\u4e49\u662f\u4ec0\u4e48?\" target=\"_blank\" title=\"HashMap \u5b58\u5728\u7684\u610f\u4e49\u662f\u4ec0\u4e48? - \u4e1a\u4f59\u8349\">HashMap \u5b58\u5728\u7684\u610f\u4e49\u662f\u4ec0\u4e48?<\/a>\u300b\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u603b\u7ed3\u5230\uff1aHashMap \u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u89e3\u51b3\u5b58\u53d6\u4e00\u7ec4 key-vaule \u952e\u503c\u5bf9\u6570\u636e\uff0c\u5e76\u4e14\u5728\u63d2\u5165\u3001\u5220\u9664\u3001\u904d\u5386\u90fd\u6709\u4e0d\u9519\u6027\u80fd\u7684\u6570\u636e\u7ed3\u6784\u3002\u6211\u4eec\u4e5f\u77e5\u9053\uff0cJDK1.8 \u5bf9 HashMap \u7684\u6539\u52a8\u8fd8\u4e0d\u5c11\uff0c\u90a3\u4e48\u8fd9\u4e9b\u6539\u52a8\u5bf9 HashMap \u7684\u4f7f\u7528\u6027\u80fd\u5230\u5e95\u6709\u591a\u5c11\u5f71\u54cd\u5462\uff1f\u4eca\u5929\u6211\u4eec\u4e00\u8d77\u6765\u63a2\u7a76\u63a2\u7a76\uff01\n<\/p>\n<p>\n\t\u5f71\u54cd HashMap \u6027\u80fd\u7684\u4e00\u4e2a\u91cd\u8981\u6307\u6807\u5c31\u662f Hash\uff0cHash \u7684\u597d\u574f\uff08\u4e5f\u5c31\u662f Hash \u51b2\u7a81\u7684\u6982\u7387\uff09\u3002\u6839\u636e\u8fd9\u4e2a\u7279\u6027\uff0c\u6211\u4eec\u6765\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a Key\uff0c\u8fdb\u884c\u5b83\u7684 Hash \u8ba1\u7b97\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nclass Key implements Comparable&lt;Key&gt; {\r\n    \/\/ \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n    private final int value;\r\n    Key(int value) {\r\n        this.value = value;\r\n    }\r\n    @Override\r\n    public int compareTo(Key o) {\r\n        return Integer.compare(this.value, o.value);\r\n    }\r\n    @Override\r\n    public boolean equals(Object o) {\r\n        if (this == o) return true;\r\n        if (o == null || getClass() != o.getClass())\r\n            return false;\r\n        Key key = (Key) o;\r\n        return value == key.value;\r\n    }\r\n    @Override\r\n    public int hashCode() {\r\n        return value;\r\n    }\r\n}<\/pre>\n<p>\n\t\u9488\u5bf9\u8fd9\u4e2a Key\uff0c\u6211\u4eec\u91cd\u5199\u4e86 equals\u3001hashCode\u3001compareTo 3 \u4e2a\u65b9\u6cd5\u3002\u8fd9\u4e2a Hash \u7684\u8ba1\u7b97\u5e94\u8be5\u662f\u76f8\u5f53\u597d\u7684\u4e86\uff0c\u76f4\u63a5\u4f7f\u7528 int \u7684 value \u503c\u3002\u4e3a\u4e86\u907f\u514d\u9891\u7e41\u7684 GC\uff0c\u6211\u5c06\u4e0d\u53d8\u7684 Key \u5b9e\u4f8b\u7f13\u5b58\u8d77\u6765\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\npublic class Keys {\r\n    \/\/ \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n    public static final int MAX_KEY = 10_000_000;\r\n    private static final Key[] KEYS_CACHE = new Key[MAX_KEY];\r\n    static {\r\n        for (int i = 0; i &lt; MAX_KEY; ++i) {\r\n            KEYS_CACHE[i] = new Key(i);\r\n        }\r\n    }\r\n    public static Key of(int value) {\r\n        return KEYS_CACHE[value];\r\n    }\r\n}<\/pre>\n<p>\n\t\u4e0b\u9762\u770b\u6211\u4eec\u7684\u6d4b\u8bd5\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nstatic void test(int mapSize) {\r\n    HashMap&lt;Key, Integer&gt; map = new HashMap&lt;Key,Integer&gt;(mapSize);\r\n    for (int i = 0; i &lt; mapSize; ++i) {\r\n        map.put(Keys.of(i), i);\r\n    }\r\n    \/\/ \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n    long beginTime = System.nanoTime(); \/\/\u83b7\u53d6\u7eb3\u79d2\r\n    for (int i = 0; i &lt; mapSize; i++) {\r\n        map.get(Keys.of(i));\r\n    }\r\n    long endTime = System.nanoTime();\r\n    System.out.println(endTime - beginTime);\r\n}\r\npublic static void main(String[] args) {\r\n    for(int i=10;i&lt;= 10000000;i*= 10){\r\n        test(i);\r\n    }\r\n}<\/pre>\n<p>\n\t\u4e3a\u4e86\u5b9e\u9a8c\u6d4b\u8bd5\uff0c\u6211\u4eec\u9700\u8981\u505a\u7684\u662f\uff0c\u521b\u5efa\u4e0d\u540c size \u7684 HashMap\uff081\u300110\u3001100\u3001&#8230;&#8230;10000000\uff09\uff0c\u8fd9\u6837\u53ef\u4ee5\u5c4f\u853d\u6269\u5bb9\u7684\u5f71\u54cd\u3002\n<\/p>\n<p>\n\t\u5206\u522b\u5728 JDK1.7 \u548c JDK1.8 \u4e0a\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u5e76\u8bb0\u5f55\u4e0d\u540c size \u6240\u82b1\u8d39\u7684\u65f6\u95f4\u3002\u7ed3\u679c\u5982\u4e0b\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"HashMap \u5728JDK1.8\u4e0eJDK1.7\u7684\u6027\u80fd\u6d4b\u8bd5\u5bf9\u6bd4\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/jdk1.7-jdk1.8-hashmap-get.png\" style=\"width: 1246px;height: 206px\" \/>\n<\/p>\n<p>\n\t\u901a\u8fc7\u591a\u6b21\u7684\u6d4b\u8bd5\uff0c\u7ed3\u679c\u8868\u660e\uff1aJDK1.8 \u7684\u6027\u80fd\u8981\u9ad8\u4e8e JDK1.7 15%\u4ee5\u4e0a\uff0c\u5728\u67d0\u4e9b size \u7684\u533a\u57df\u4e0a\uff0c\u751a\u81f3\u9ad8\u4e8e 100%\u3002\u7531\u4e8e Hash \u7b97\u6cd5\u8f83\u5747\u5300\uff0cJDK1.8 \u5f15\u5165\u7684\u7ea2\u9ed1\u6811\u6548\u679c\u4e0d\u660e\u663e\uff0c\u4e0b\u9762\u6211\u4eec\u770b\u770b Hash \u4e0d\u5747\u5300\u7684\u7684\u60c5\u51b5\u3002\n<\/p>\n<p>\n\t\u6211\u4eec\u6539\u8fdb\u4e00\u4e0b Key \u5bf9 Hash \u7684\u8ba1\u7b97\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nclass Key implements Comparable&lt;Key&gt; {\r\n    \/\/...\r\n    \/\/ \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n    @Override\r\n    public int hashCode() {\r\n        return 1;\r\n    }\r\n}<\/pre>\n<p>\n\t\u6a21\u62df\u4e00\u4e2a\u975e\u5e38\u5dee\u7684 Hash \u8ba1\u7b97\uff0chashCode \u59cb\u7ec8\u8fd4\u56de 1\uff0c\u8fd9\u6837\u5c31\u4f1a\u7528\u5230 JDK1.8 \u5f15\u5165\u7684\u7ea2\u9ed1\u6811\u3002\n<\/p>\n<p>\n\t\u6d4b\u8bd5\u4ee3\u7801\u548c\u8fc7\u7a0b\u548c\u4e0a\u9762\u7c7b\u4f3c\uff0c\u6211\u76f4\u63a5\u8d34\u51fa\u7ed3\u679c\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Jdk1.7\u548cJdk1.8\u4e2d\u94fe\u8868\u6d4b\u8bd5\u6bd4\u5bf9\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/jdk1.7-jdk1.8-hashmap-tree.png\" style=\"width: 1252px;height: 202px\" \/>\n<\/p>\n<p>\n\t\u7ed3\u679c\u8868\u660e\uff1a\u968f\u7740 size \u7684\u53d8\u5927\uff0cJDK1.7 \u7684\u82b1\u8d39\u65f6\u95f4\u662f\u589e\u957f\u7684\u8d8b\u52bf\uff0c\u800c JDK1.8 \u662f\u660e\u663e\u7684\u964d\u4f4e\u8d8b\u52bf\uff0c\u5e76\u4e14\u5448\u73b0\u5bf9\u6570\u589e\u957f\u7a33\u5b9a\u3002\u5f53\u4e00\u4e2a\u94fe\u8868\u592a\u957f\u7684\u65f6\u5019\uff0cHashMap \u4f1a\u52a8\u6001\u7684\u5c06\u5b83\u66ff\u6362\u6210\u4e00\u4e2a\u7ea2\u9ed1\u6811\uff0c\u8fd9\u8bdd\u7684\u8bdd\u4f1a\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4ece O(n) \u964d\u4e3a O(logn)\u3002hash \u7b97\u6cd5\u5747\u5300\u548c\u4e0d\u5747\u5300\u6240\u82b1\u8d39\u7684\u65f6\u95f4\u660e\u663e\u4e5f\u4e0d\u76f8\u540c\uff0c\u8fd9\u4e24\u79cd\u60c5\u51b5\u7684\u76f8\u5bf9\u6bd4\u8f83\uff0c\u53ef\u4ee5\u8bf4\u660e\u4e00\u4e2a\u597d\u7684 hash \u7b97\u6cd5\u7684\u91cd\u8981\u6027\u3002\n<\/p>\n<p>\n\t\u6d4b\u8bd5\u786c\u4ef6\u73af\u5883\u3001\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u7c7b\u7684\u6211\u5c31\u4e0d\u5199\u4e86\uff0c\u5927\u5bb6\u81ea\u5df1\u53bb\u6d4b\u8bd5\uff0c\u5f97\u5230\u7684\u624d\u662f\u6700\u771f\u5b9e\u7684\u611f\u89c2\u3002<\/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=3387\">HashMap \u5728JDK1.8\u4e0eJDK1.7\u7684\u6027\u80fd\u6d4b\u8bd5\u5bf9\u6bd4<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u4efb\u4f55\u6570\u636e\u7ed3\u6784\u7684\u4ea7\u751f\u603b\u5bf9\u5e94\u7740\u8981\u89e3\u51b3\u4e00\u4e2a\u5b9e\u9645\u7684\u95ee\u9898\uff01\u6211\u5728\u300aHashMap \u5b58\u5728\u7684\u610f\u4e49\u662f\u4ec0\u4e48?\u300b\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u603b\u7ed3\u5230\uff1aH [&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,1727,1334,1163,1471],"class_list":["post-3387","post","type-post","status-publish","format-standard","hentry","category-java","tag-concurrenthashmap","tag-hashmap","tag-java8","tag-jdk8","tag-linkedhashmap","tag-optional"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3387","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=3387"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3387\/revisions"}],"predecessor-version":[{"id":3388,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3387\/revisions\/3388"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}