{"id":3849,"date":"2024-08-14T12:58:07","date_gmt":"2024-08-14T04:58:07","guid":{"rendered":"https:\/\/qaqaq.top\/?p=3849"},"modified":"2024-08-14T12:58:07","modified_gmt":"2024-08-14T04:58:07","slug":"concurrentmodificationexception%e5%bc%82%e5%b8%b8","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=3849","title":{"rendered":"ConcurrentModificationException\u5f02\u5e38"},"content":{"rendered":"\n<h6 class=\"wp-block-heading\">\u4ee3\u7801<\/h6>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp;public static void main(String[] args) {<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;test test = new test();<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;test.bugTwo();<br>\u200b<br> &nbsp;  }<br>\u200b<br> &nbsp; &nbsp;private void bugTwo() {<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;List&lt;Map&lt;String, Object&gt;&gt; list = new ArrayList&lt;&gt;();<br> &nbsp; &nbsp; &nbsp; &nbsp;HashMap&lt;String, Object&gt; map = new HashMap&lt;&gt;();<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;map.put(\"1\", \"1\");<br> &nbsp; &nbsp; &nbsp; &nbsp;map.put(\"2\", \"2\");<br> &nbsp; &nbsp; &nbsp; &nbsp;map.put(\"3\", \"3\");<br> &nbsp; &nbsp; &nbsp; &nbsp;map.put(\"4\", \"4\");<br> &nbsp; &nbsp; &nbsp; &nbsp;list.add(map);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(list);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;for (Map&lt;String, Object&gt; demo : list) {<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (int i = 0; i &lt; 2; i++) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Map&lt;String, Object&gt; iMap = new HashMap&lt;&gt;();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iMap.put(\"1\", \"11\" + i);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iMap.put(\"2\", \"22\" + i);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iMap.put(\"3\", \"33\" + i);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iMap.put(\"4\", \"44\" + i);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list.add(map);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br>\u200b<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(list);<br>\u200b<br> &nbsp;  }<\/pre>\n\n\n\n<h6 class=\"wp-block-heading\">\u7ed3\u679c<\/h6>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/qaqaq.top\/wp-content\/uploads\/2024\/08\/image-20240814113902510-1024x185.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"185\" data-original=\"https:\/\/qaqaq.top\/wp-content\/uploads\/2024\/08\/image-20240814113902510-1024x185.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-3850\"  sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<h6 class=\"wp-block-heading\">\u539f\u56e0<\/h6>\n\n\n\n<p>ConcurrentModificationException \u662f Java \u4e2d\u7684\u4e00\u4e2a\u5f02\u5e38\u7c7b\u578b\uff0c\u5c5e\u4e8e java.util.ConcurrentModificationException \u7c7b\u3002\u5b83\u662f\u4e00\u79cd\u8fd0\u884c\u65f6\u5f02\u5e38\uff08RuntimeException\uff09\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u5f02\u5e38\u901a\u5e38\u53d1\u751f\u5728\u591a\u7ebf\u7a0b\u6216\u8005\u975e\u7ebf\u7a0b\u5b89\u5168\u7684\u4e0a\u4e0b\u6587\u4e2d\uff0c\u5f53\u4e00\u4e2a\u96c6\u5408\uff08\u6bd4\u5982 ArrayList\u3001LinkedList\u3001HashSet \u7b49\uff09\u6b63\u5728\u8fdb\u884c\u8fed\u4ee3\uff08\u904d\u5386\uff09\u65f6\uff0c\u6709\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u6216\u64cd\u4f5c\u4fee\u6539\u4e86\u8fd9\u4e2a\u96c6\u5408\u7684\u7ed3\u6784\uff08\u5982\u6dfb\u52a0\u3001\u5220\u9664\u6216\u66ff\u6362\u5143\u7d20\uff09\u3002<\/p>\n\n\n\n<p>1\u3001\u5f53\u5728\u4f7f\u7528\u8fed\u4ee3\u5668\uff08Iterator\uff09\u6216\u589e\u5f3a\u578b for \u5faa\u73af\u904d\u5386\u96c6\u5408\u65f6\uff0c\u540c\u65f6\u5c1d\u8bd5\u4fee\u6539\u96c6\u5408\uff08\u9664\u4e86\u4f7f\u7528 Iterator \u7684 remove() \u65b9\u6cd5\u5916\uff09\uff0c\u4f1a\u5bfc\u81f4 ConcurrentModificationException\u3002<\/p>\n\n\n\n<p>2\u3001\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u5982\u679c\u4e00\u4e2a\u7ebf\u7a0b\u6b63\u5728\u904d\u5386\u96c6\u5408\uff0c\u800c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4fee\u6539\u4e86\u8fd9\u4e2a\u96c6\u5408\uff0c\u540c\u6837\u4f1a\u89e6\u53d1 ConcurrentModificationException\u3002<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">\u89e3\u51b3<\/h6>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 List \u8fdb\u884c\u6dfb\u52a0\u5143\u7d20<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp;public static void main(String[] args) {<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;test test = new test();<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;test.bugTwo();<br>\u200b<br> &nbsp;  }<br>\u200b<br> &nbsp; &nbsp;private void bugTwo() {<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;List&lt;Map&lt;String, Object&gt;&gt; list = new ArrayList&lt;&gt;();<br> &nbsp; &nbsp; &nbsp; &nbsp;HashMap&lt;String, Object&gt; map = new HashMap&lt;&gt;();<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;map.put(\"1\", \"1\");<br> &nbsp; &nbsp; &nbsp; &nbsp;map.put(\"2\", \"2\");<br> &nbsp; &nbsp; &nbsp; &nbsp;map.put(\"3\", \"3\");<br> &nbsp; &nbsp; &nbsp; &nbsp;map.put(\"4\", \"4\");<br> &nbsp; &nbsp; &nbsp; &nbsp;list.add(map);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(list);<br>\/\/ &nbsp; &nbsp; &nbsp;  for (Map&lt;String, Object&gt; demo : list) {<br>\/\/<br>\/\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  for (int i = 0; i &lt; 2; i++) {<br>\/\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  Map&lt;String, Object&gt; iMap = new HashMap&lt;&gt;();<br>\/\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  iMap.put(\"1\", \"11\" + i);<br>\/\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  iMap.put(\"2\", \"22\" + i);<br>\/\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  iMap.put(\"3\", \"33\" + i);<br>\/\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  iMap.put(\"4\", \"44\" + i);<br>\/\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  list.add(map);<br>\/\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br>\/\/<br>\/\/ &nbsp; &nbsp; &nbsp;  }<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;List&lt;Map&lt;String, Object&gt;&gt; listCopy = new ArrayList&lt;&gt;();<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;for (Map&lt;String, Object&gt; demo : list) {<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listCopy.add(demo);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (int i = 0; i &lt; 2; i++) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Map&lt;String, Object&gt; iMap = new HashMap&lt;&gt;();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iMap.put(\"1\", \"11\" + i);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iMap.put(\"2\", \"22\" + i);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iMap.put(\"3\", \"33\" + i);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iMap.put(\"4\", \"44\" + i);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listCopy.add(iMap);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br>\u200b<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;list.clear();<br> &nbsp; &nbsp; &nbsp; &nbsp;list.addAll(listCopy);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(list);<br>\u200b<br> &nbsp;  }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee3\u7801 &nbsp; &nbsp;public static void main(String[] args)  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[46],"class_list":["post-3849","post","type-post","status-publish","format-standard","hentry","category-java-","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/3849"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3849"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/3849\/revisions"}],"predecessor-version":[{"id":3851,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/3849\/revisions\/3851"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}