{"id":94,"date":"2023-02-14T21:55:40","date_gmt":"2023-02-14T13:55:40","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=94"},"modified":"2023-02-14T21:56:32","modified_gmt":"2023-02-14T13:56:32","slug":"synchronizedmap-concurrenthashmap-comparison","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/14\/synchronizedmap-concurrenthashmap-comparison\/","title":{"rendered":"SynchronizedMap\u548cConcurrentHashMap\u4e4b\u540c\u6b65\u65b9\u5f0f\u6bd4\u8f83"},"content":{"rendered":"<p>Map\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff0c<code>HashMap<\/code>\u7684\u5b9e\u73b0\u91c7\u7528\u7684\u662fhash\u8868\uff1b\u800c<code>TreeMap<\/code>\u91c7\u7528\u7684\u662f\u7ea2\u9ed1\u6811\u3002<\/p>\n<h3>Hashtable \u548c HashMap<\/h3>\n<p>\u8fd9\u4e24\u4e2a\u7c7b\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u65b9\u9762\u7684\u4e0d\u540c\uff1a<\/p>\n<p><code>Hashtable<\/code>\u548c<code>HashMap<\/code>\u90fd\u5b9e\u73b0\u4e86Map\u63a5\u53e3\uff0c\u4f46\u662f<code>Hashtable<\/code>\u7684\u5b9e\u73b0\u662f\u57fa\u4e8e<code>Dictionary<\/code>\u62bd\u8c61\u7c7b\u3002<\/p>\n<p>\u5728<code>HashMap<\/code>\u4e2d\uff0cnull\u53ef\u4ee5\u4f5c\u4e3a\u952e\uff0c\u8fd9\u6837\u7684\u952e\u53ea\u6709\u4e00\u4e2a\uff1b\u53ef\u4ee5\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u952e\u6240\u5bf9\u5e94\u7684\u503c\u4e3anull\u3002 \u5f53<code>get()<\/code>\u65b9\u6cd5\u8fd4\u56denull\u503c\u65f6\uff0c\u5373\u53ef\u4ee5\u8868\u793a <code>HashMap<\/code>\u4e2d\u6ca1\u6709\u8be5\u952e\uff0c\u4e5f\u53ef\u4ee5\u8868\u793a\u8be5\u952e\u6240\u5bf9\u5e94\u7684\u503c\u4e3anull\u3002\u56e0\u6b64\uff0c\u5728HashMap\u4e2d\u4e0d\u80fd\u7531<code>get()<\/code>\u65b9\u6cd5\u6765\u5224\u65ad<code>HashMap<\/code>\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u952e\uff0c\u800c\u5e94\u8be5\u7528<code>containsKey()<\/code>\u65b9\u6cd5\u6765\u5224\u65ad\u3002\u800c\u5728<code>Hashtable<\/code>\u4e2d\uff0c\u65e0\u8bba\u662fkey\u8fd8\u662fvalue\u90fd\u4e0d\u80fd\u4e3anull \u3002<\/p>\n<p>\u8fd9\u4e24\u4e2a\u7c7b\u6700\u5927\u7684\u4e0d\u540c\u5728\u4e8e<code>Hashtable<\/code>\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u5b83\u7684\u65b9\u6cd5\u662f\u540c\u6b65\u4e86\u7684\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u3002\u800c<code>HashMap<\/code>\u5219\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u9700\u8981\u624b\u52a8\u5b9e\u73b0\u540c\u6b65\u673a\u5236\u3002\u56e0\u6b64\uff0c\u5728<code>Collections<\/code>\u7c7b\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u540c\u6b65\u7248\u672c\u7684<code>HashMap<\/code>\u7528\u4e8e\u591a\u7ebf\u7a0b\u7684\u73af\u5883\uff1a<\/p>\n<pre><code class=\"language-java\">public static &lt;K,V&gt; Ma&lt;K,V&gt; synchronizedMap(Map&lt;K,V&gt; m) {\n    return new SynchronizedMap&lt;K,V&gt;(m);\n}<\/code><\/pre>\n<p>\u8be5\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u4e00\u4e2a<code>SynchronizedMap<\/code>\u7684\u5b9e\u4f8b\u3002<code>SynchronizedMap<\/code>\u7c7b\u662f\u5b9a\u4e49\u5728<code>Collections<\/code>\u4e2d\u7684\u4e00\u4e2a\u9759\u6001\u5185\u90e8\u7c7b\u3002\u5b83\u5b9e\u73b0\u4e86Map\u63a5\u53e3\uff0c\u5e76\u5bf9\u5176\u4e2d\u7684\u6bcf\u4e00\u4e2a\u65b9\u6cd5\u5b9e\u73b0\uff0c\u901a\u8fc7<code>synchronized<\/code>\u5173\u952e\u5b57\u8fdb\u884c\u4e86\u540c\u6b65\u63a7\u5236\u3002<\/p>\n<h3>\u6f5c\u5728\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/h3>\n<p>\u4e0a\u9762\u63d0\u5230<code>Collections<\/code>\u4e3a<code>HashMap<\/code>\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5e76\u53d1\u7248\u672c<code>SynchronizedMap<\/code>\u3002\u8fd9\u4e2a\u7248\u672c\u4e2d\u7684\u65b9\u6cd5\u90fd\u8fdb\u884c\u4e86\u540c\u6b65\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u7b49\u4e8e\u8fd9\u4e2a\u7c7b\u5c31\u4e00\u5b9a\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u5728\u67d0\u4e9b\u65f6\u5019\u4f1a\u51fa\u73b0\u4e00\u4e9b\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u5982\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/ shm\u662fSynchronizedMap\u7684\u4e00\u4e2a\u5b9e\u4f8b   \nif (shm.containsKey(&#039;key&#039;)) {   \n    shm.remove(key);   \n}<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u7528\u4e8e\u4ecemap\u4e2d\u5220\u9664\u4e00\u4e2a\u5143\u7d20\u4e4b\u524d\u5224\u65ad\u662f\u5426\u5b58\u5728\u8fd9\u4e2a\u5143\u7d20\u3002\u8fd9\u91cc\u7684<code>containsKey<\/code>\u548creomve\u65b9\u6cd5\u90fd\u662f\u540c\u6b65\u7684\uff0c\u4f46\u662f\u6574\u6bb5\u4ee3\u7801\u5374\u4e0d\u662f\u3002\u8003\u8651\u8fd9\u4e48\u4e00\u4e2a\u4f7f\u7528\u573a\u666f\uff1a\u7ebf\u7a0bA\u6267\u884c\u4e86<code>containsKey<\/code>\u65b9\u6cd5\u8fd4\u56detrue\uff0c\u51c6\u5907\u6267\u884cremove\u64cd\u4f5c\uff1b\u8fd9\u65f6\u53e6\u4e00\u4e2a\u7ebf\u7a0bB\u5f00\u59cb\u6267\u884c\uff0c\u540c\u6837\u6267\u884c\u4e86<code>containsKey<\/code>\u65b9\u6cd5\u8fd4\u56detrue\uff0c\u5e76\u63a5\u7740\u6267\u884c\u4e86remove\u64cd\u4f5c\uff1b\u7136\u540e\u7ebf\u7a0bA\u63a5\u7740\u6267\u884cremove\u64cd\u4f5c\u65f6\u53d1\u73b0\u6b64\u65f6\u5df2\u7ecf\u6ca1\u6709\u8fd9\u4e2a\u5143\u7d20\u4e86\u3002\u8981\u4fdd\u8bc1\u8fd9\u6bb5\u4ee3\u7801\u6309\u6211\u4eec\u7684\u610f\u613f\u5de5\u4f5c\uff0c\u4e00\u4e2a\u529e\u6cd5\u5c31\u662f\u5bf9\u8fd9\u6bb5\u4ee3\u7801\u8fdb\u884c\u540c\u6b65\u63a7\u5236\uff0c\u4f46\u662f\u8fd9\u4e48\u505a\u4ed8\u51fa\u7684\u4ee3\u4ef7\u592a\u5927\u3002<\/p>\n<p>\u5728\u8fdb\u884c\u8fed\u4ee3\u65f6\u8fd9\u4e2a\u95ee\u9898\u66f4\u6539\u660e\u663e\u3002Map\u96c6\u5408\u5171\u63d0\u4f9b\u4e86\u4e09\u79cd\u65b9\u5f0f\u6765\u5206\u522b\u8fd4\u56de\u952e\u3001\u503c\u3001\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff1a<\/p>\n<pre><code class=\"language-java\">Set&lt;K&gt; keySet();\n\nCollection&lt;V&gt; values();\n\nSet&lt;Map.Entry&lt;K,V&gt;&gt; entrySet();<\/code><\/pre>\n<p>\u5728\u8fd9\u4e09\u4e2a\u65b9\u6cd5\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u4e00\u822c\u901a\u8fc7\u5982\u4e0b\u65b9\u5f0f\u8bbf\u95eeMap\u7684\u5143\u7d20\uff1a<\/p>\n<pre><code class=\"language-java\">Iterator keys = map.keySet().iterator();   \n\nwhile (keys.hasNext()) {   \n    map.get(keys.next());   \n}<\/code><\/pre>\n<p>\u5728\u8fd9\u91cc\uff0c\u6709\u4e00\u4e2a\u5730\u65b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u5f97\u5230\u7684keySet\u548c\u8fed\u4ee3\u5668\u90fd\u662fMap\u4e2d\u5143\u7d20\u7684\u4e00\u4e2a\u201c\u89c6\u56fe\u201d\uff0c\u800c\u4e0d\u662f\u201c\u526f\u672c\u201d \u3002\u95ee\u9898\u4e5f\u5c31\u51fa\u73b0\u5728\u8fd9\u91cc\uff0c\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u6b63\u5728\u8fed\u4ee3Map\u4e2d\u7684\u5143\u7d20\u65f6\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u6b63\u5728\u4fee\u6539\u5176\u4e2d\u7684\u5143\u7d20\u3002\u6b64\u65f6\uff0c\u5728\u8fed\u4ee3\u5143\u7d20\u65f6\u5c31\u53ef\u80fd\u4f1a\u629b\u51fa <code>ConcurrentModificationException<\/code>\u5f02\u5e38\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u901a\u5e38\u6709\u4e24\u79cd\u65b9\u6cd5\uff0c\u4e00\u662f\u76f4\u63a5\u8fd4\u56de\u5143\u7d20\u7684\u526f\u672c\uff0c\u800c\u4e0d\u662f\u89c6\u56fe\u3002\u8fd9\u4e2a\u53ef\u4ee5\u901a\u8fc7\u96c6\u5408\u7c7b\u7684 <code>toArray()<\/code>\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4f46\u662f\u521b\u5efa\u526f\u672c\u7684\u65b9\u5f0f\u6548\u7387\u6bd4\u4e4b\u524d\u6709\u6240\u964d\u4f4e\uff0c\u7279\u522b\u662f\u5728\u5143\u7d20\u5f88\u591a\u7684\u60c5\u51b5\u4e0b\uff1b\u53e6\u4e00\u79cd\u65b9\u6cd5\u5c31\u662f\u5728\u8fed\u4ee3\u7684\u65f6\u5019\u9501\u4f4f\u6574\u4e2a\u96c6\u5408\uff0c\u8fd9\u6837\u7684\u8bdd\u6548\u7387\u5c31\u66f4\u4f4e\u4e86\u3002<\/p>\n<h3>\u66f4\u597d\u7684\u9009\u62e9\uff1aConcurrentHashMap<\/h3>\n<p>Java5\u4e2d\u65b0\u589e\u4e86<code>ConcurrentMap<\/code>\u63a5\u53e3\u548c\u5b83\u7684\u4e00\u4e2a\u5b9e\u73b0\u7c7b<code>ConcurrentHashMap<\/code>\u3002<\/p>\n<p><code>ConcurrentHashMap<\/code>\u63d0\u4f9b\u4e86\u548c<code>Hashtable<\/code>\u4ee5\u53ca<code>SynchronizedMap<\/code>\u4e2d\u6240\u4e0d\u540c\u7684\u9501\u673a\u5236\u3002<code>Hashtable<\/code>\u4e2d\u91c7\u7528\u7684\u9501\u673a\u5236\u662f\u4e00\u6b21\u9501\u4f4f\u6574\u4e2ahash\u8868\uff0c\u4ece\u800c\u540c\u4e00\u65f6\u523b\u53ea\u80fd\u7531\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u5176\u8fdb\u884c\u64cd\u4f5c\uff1b\u800c<code>ConcurrentHashMap<\/code>\u4e2d\u5219\u662f\u4e00\u6b21\u9501\u4f4f\u4e00\u4e2a\u6876\u3002<code>ConcurrentHashMap<\/code>\u9ed8\u8ba4\u5c06hash\u8868\u5206\u4e3a16\u4e2a\u6876\uff0c\u8bf8\u5982<code>get,put,remove<\/code>\u7b49\u5e38\u7528\u64cd\u4f5c\u53ea\u9501\u5f53\u524d\u9700\u8981\u7528\u5230\u7684\u6876\u3002\u539f\u6765\u53ea\u80fd\u4e00\u4e2a\u7ebf\u7a0b\u8fdb\u5165\uff0c\u73b0\u5728\u5374\u80fd\u540c\u65f6\u670916\u4e2a\u5199\u7ebf\u7a0b\u6267\u884c\uff0c\u5e76\u53d1\u6027\u80fd\u7684\u63d0\u5347\u662f\u663e\u800c\u6613\u89c1\u7684\u3002<\/p>\n<p>\u4e0a\u9762\u8bf4\u5230\u768416\u4e2a\u7ebf\u7a0b\u6307\u7684\u662f\u5199\u7ebf\u7a0b\uff0c\u800c\u8bfb\u64cd\u4f5c\u5927\u90e8\u5206\u65f6\u5019\u90fd\u4e0d\u9700\u8981\u7528\u5230\u9501\u3002\u53ea\u6709\u5728size\u7b49\u64cd\u4f5c\u65f6\u624d\u9700\u8981\u9501\u4f4f\u6574\u4e2ahash\u8868\u3002<\/p>\n<p>\u5728\u8fed\u4ee3\u65b9\u9762\uff0c<code>ConcurrentHashMap<\/code>\u4f7f\u7528\u4e86\u4e00\u79cd\u4e0d\u540c\u7684\u8fed\u4ee3\u65b9\u5f0f\u3002\u5728\u8fd9\u79cd\u8fed\u4ee3\u65b9\u5f0f\u4e2d\uff0c\u5f53<code>iterator<\/code>\u88ab\u521b\u5efa\u540e\u96c6\u5408\u518d\u53d1\u751f\u6539\u53d8\u5c31\u4e0d\u518d\u662f\u629b\u51fa<code>ConcurrentModificationException<\/code>\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\u5728\u6539\u53d8\u65f6new\u65b0\u7684\u6570\u636e\u4ece\u800c\u4e0d\u5f71\u54cd\u539f\u6709\u7684\u6570\u636e\uff0c<code>iterator<\/code>\u5b8c\u6210\u540e\u518d\u5c06\u5934\u6307\u9488\u66ff\u6362\u4e3a\u65b0\u7684\u6570\u636e\uff0c\u8fd9\u6837<code>iterator<\/code>\u7ebf\u7a0b\u53ef\u4ee5\u4f7f\u7528\u539f\u6765\u8001\u7684\u6570\u636e\uff0c\u800c\u5199\u7ebf\u7a0b\u4e5f\u53ef\u4ee5\u5e76\u53d1\u7684\u5b8c\u6210\u6539\u53d8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Map\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff0cHashMap\u7684\u5b9e\u73b0\u91c7\u7528\u7684\u662fhash\u8868\uff1b\u800cTreeMap\u91c7\u7528\u7684\u662f\u7ea2\u9ed1\u6811\u3002 Hash [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-94","post","type-post","status-publish","format-standard","hentry","category-java-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}