{"id":423,"date":"2011-05-04T23:01:00","date_gmt":"2011-05-04T23:01:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/how-to-avoid-concurrentmodificationexception-when-using-an-iterator.html"},"modified":"2019-12-04T16:32:27","modified_gmt":"2019-12-04T14:32:27","slug":"avoid-concurrentmodificationexception","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html","title":{"rendered":"How to Avoid ConcurrentModificationException when using an Iterator"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left\">Java Collection classes are fail-fast which means that if the Collection will be changed while some thread is traversing over it using iterator, the iterator.next() will throw a <a href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/ConcurrentModificationException.html\">ConcurrentModificationException<\/a>.<\/p>\n<p>This situation can come in case of multithreaded as well as single threaded environment.<\/p>\n<p>Lets explore this scenario with the following example:<\/p>\n<pre class=\"brush:java\">import java.util.*;\n \npublic class IteratorExample {\n \n    public static void main(String args[]){\n        List&lt;String&gt; myList = new ArrayList&lt;String&gt;();\n \n        myList.add(\"1\");\n        myList.add(\"2\");\n        myList.add(\"3\");\n        myList.add(\"4\");\n        myList.add(\"5\");\n \n        Iterator&lt;String&gt; it = myList.iterator();\n        while(it.hasNext()){\n            String value = it.next();\n            System.out.println(\"List Value:\"+value);\n            if(value.equals(\"3\")) myList.remove(value);\n        }\n \n        Map&lt;String,String&gt; myMap = new HashMap&lt;String,String&gt;();\n        myMap.put(\"1\", \"1\");\n        myMap.put(\"2\", \"2\");\n        myMap.put(\"3\", \"3\");\n \n        Iterator&lt;String&gt; it1 = myMap.keySet().iterator();\n        while(it1.hasNext()){\n            String key = it1.next();\n            System.out.println(\"Map Value:\"+myMap.get(key));\n            if(key.equals(\"2\")){\n                myMap.put(\"1\",\"4\");\n                \/\/myMap.put(\"4\", \"4\");\n            }\n        }\n \n    }\n}\n<\/pre>\n<p>Output is:<\/p>\n<pre class=\"brush:java\">List Value:1\nList Value:2\nList Value:3\nException in thread \"main\" java.util.ConcurrentModificationException\n    at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)\n    at java.util.AbstractList$Itr.next(AbstractList.java:343)\n    at com.journaldev.java.IteratorExample.main(IteratorExample.java:27)\n<\/pre>\n<p>From the output stack trace, its clear that the exception is coming when we call iterator next() function. If you are wondering how Iterator checks for the modification, its implementation is present in AbstractList class where an int variable modCount is defined that provides the number of times list size has been changed. This value is used in every next() call to check for any modifications in a function checkForComodification().<\/p>\n<p>Now comment the list part and run the program again.<\/p>\n<p>Output will be:<\/p>\n<pre class=\"brush:bash\">Map Value:3\nMap Value:2\nMap Value:4\n<\/pre>\n<p>Since we are updating the existing key value in the myMap, its size has not been changed and we are not getting ConcurrentModificationException. Note that the output may differ in your system because HashMap keyset is not ordered like list. If you will uncomment the statement where I am adding a new key-value in the HashMap, it will cause ConcurrentModificationException.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><strong>To Avoid ConcurrentModificationException in multi-threaded environment:<\/strong><\/p>\n<p>1. You can convert the list to an array and then iterate on the array. This approach works well for small or medium size list but if the list is large then it will affect the performance a lot.<\/p>\n<p>2. You can lock the list while iterating by putting it in a synchronized block. This approach is not recommended because it will cease the benefits of multithreading.<\/p>\n<p>3. If you are using JDK1.5 or higher then you can use ConcurrentHashMap and CopyOnWriteArrayList classes. It is the recommended approach.<\/p>\n<p><strong>To Avoid ConcurrentModificationException in single-threaded environment:<\/strong><\/p>\n<p>You can use the iterator remove() function to remove the object from underlying collection object. But in this case you can remove the same object and not any other object from the list.<\/p>\n<p>Let us run an example using Concurrent Collection classes:<\/p>\n<pre class=\"brush:java\">package com.journaldev.java;\n \nimport java.util.Iterator;\nimport java.util.List;\nimport java.util.Map;\nimport java.util.concurrent.ConcurrentHashMap;\nimport java.util.concurrent.CopyOnWriteArrayList;\n \npublic class ThreadSafeIteratorExample {\n \n    public static void main(String[] args) {\n \n        List&lt;String&gt; myList = new CopyOnWriteArrayList&lt;String&gt;();\n \n        myList.add(\"1\");\n        myList.add(\"2\");\n        myList.add(\"3\");\n        myList.add(\"4\");\n        myList.add(\"5\");\n \n        Iterator&lt;String&gt; it = myList.iterator();\n        while(it.hasNext()){\n            String value = it.next();\n            System.out.println(\"List Value:\"+value);\n            if(value.equals(\"3\")){\n                myList.remove(\"4\");\n                myList.add(\"6\");\n                myList.add(\"7\");\n            }\n        }\n        System.out.println(\"List Size:\"+myList.size());\n \n        Map&lt;String,String&gt; myMap = \n             new ConcurrentHashMap&lt;String,String&gt;();\n        myMap.put(\"1\", \"1\");\n        myMap.put(\"2\", \"2\");\n        myMap.put(\"3\", \"3\");\n \n        Iterator&lt;String&gt; it1 = myMap.keySet().iterator();\n        while(it1.hasNext()){\n            String key = it1.next();\n            System.out.println(\"Map Value:\"+myMap.get(key));\n            if(key.equals(\"1\")){\n                myMap.remove(\"3\");\n                myMap.put(\"4\", \"4\");\n                myMap.put(\"5\", \"5\");\n            }\n        }\n \n        System.out.println(\"Map Size:\"+myMap.size());\n    }\n \n}\n<\/pre>\n<p>Output is:<\/p>\n<pre class=\"brush:bash\">List Value:1\nList Value:2\nList Value:3\nList Value:4\nList Value:5\nList Size:6\nMap Value:1\nMap Value:null\nMap Value:4\nMap Value:2\nMap Size:4\n<\/pre>\n<p>From the above example its clear that:<\/p>\n<p>1. Concurrent Collection classes can be modified avoiding <a href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/ConcurrentModificationException.html\">ConcurrentModificationException<\/a>.<\/p>\n<p>2. In case of <a href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/concurrent\/CopyOnWriteArrayList.html\">CopyOnWriteArrayList<\/a>, iterator doesn\u2019t accomodate the changes in the list and works on the original list.<\/p>\n<p>3. In case of <a href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/util\/concurrent\/ConcurrentHashMap.html\">ConcurrentHashMap<\/a>, the behavior is not always the same.<\/p>\n<p>For condition:<\/p>\n<pre class=\"brush:java\">if(key.equals(\"1\")){\n    myMap.remove(\"3\");\n<\/pre>\n<p>Output is:<\/p>\n<pre class=\"brush:bash\">Map Value:1\nMap Value:null\nMap Value:4\nMap Value:2\nMap Size:4\n<\/pre>\n<p>It is taking the new object added with key \u201c4? but not the next added object with key \u201c5?.<\/p>\n<p>Now if I change the condition to<\/p>\n<pre class=\"brush:java\">if(key.equals(\"3\")){\n    myMap.remove(\"2\");\n<\/pre>\n<p>Output is:<\/p>\n<pre class=\"brush:bash\">Map Value:1\nMap Value:3\nMap Value:null\nMap Size:4\n<\/pre>\n<p>In this case its not considering the new added objects.<\/p>\n<p>So if you are using ConcurrentHashMap then avoid adding new objects as it can be processed depending on the keyset. Note that the same program can print different values in your system because HashMap keyset is not in any order.<\/p>\n<p><strong>Extra Toppings:<\/strong><\/p>\n<pre class=\"brush:java\">for(int i = 0; i&lt;myList.size(); i++){\n    System.out.println(myList.get(i));\n    if(myList.get(i).equals(\"3\")){\n        myList.remove(i);\n        i--;\n        myList.add(\"6\");\n    }\n}\n<\/pre>\n<p>If you are working on single-threaded environment and want your code to take care of the extra added objects in the list then you can do so using following code and avoiding iterator.<\/p>\n<p>Note that I am decreasing the counter because I am removing the same object, if you have to remove the next or further far object then you don\u2019t need to decrease the counter.<\/p>\n<p>Try it yourself.<\/p>\n<p><strong><i>Reference :<\/i><\/strong> <a href=\"http:\/\/www.journaldev.com\/378\/how-to-avoid-concurrentmodificationexception-when-using-an-iterator\">How to Avoid ConcurrentModificationException when using an Iterator<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/p\/jcg.html\">JCG partner<\/a> at <a href=\"http:\/\/www.journaldev.com\/\">JournalDev<\/a><a href=\"http:\/\/sivalabs.blogspot.com\/\"><\/a>.<\/p>\n<ul style=\"text-align: left\"><\/ul>\n<p><strong>Related Articles:<\/strong><\/p>\n<ul style=\"text-align: left\">\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html\">Java Best Practices \u2013 Vector vs ArrayList vs HashSet<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/06\/spring-3-restful-web-services.html\">Java Best Practices \u2013 Queue battle and the Linked ConcurrentHashMap<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/02\/java-forkjoin-parallel-programming.html\">Java Fork\/Join for Parallel Programming<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/10\/concurrentlinkedhashmap-v-101-released.html\">ConcurrentLinkedHashMap v 1.0.1 released<\/a><\/li>\n<\/ul>\n<div style=\"margin: 0px\"><strong><i>Related Snippets :<\/i><\/strong><\/div>\n<ul style=\"text-align: left\">\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/util\/concurrent\/blocking-queue-example-to-execute-commands\">Blocking Queue example to execute commands<\/a><\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/util\/concurrent\/semaphores-example-limiting-url-connections\">Semaphores example limiting URL connections<\/a>&nbsp;<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/util\/concurrent\/synchronous-queue-example-to-execute-commands\">Synchronous Queue example to execute commands<\/a><\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/util\/concurrent\/countdownlatch-example-of-a-more-general-wait-notify-mechanism\">CountDownLatch example of a more general wait\/notify mechanism<\/a>&nbsp;<\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Java Collection classes are fail-fast which means that if the Collection will be changed while some thread is traversing over it using iterator, the iterator.next() will throw a ConcurrentModificationException. This situation can come in case of multithreaded as well as single threaded environment. Lets explore this scenario with the following example: import java.util.*; public class &hellip;<\/p>\n","protected":false},"author":26,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[88],"class_list":["post-423","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-concurrency"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Avoid ConcurrentModificationException when using an Iterator - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Java Collection classes are fail-fast which means that if the Collection will be changed while some thread is traversing over it using iterator, the\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Avoid ConcurrentModificationException when using an Iterator - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Java Collection classes are fail-fast which means that if the Collection will be changed while some thread is traversing over it using iterator, the\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/JournalDev\" \/>\n<meta property=\"article:published_time\" content=\"2011-05-04T23:01:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-04T14:32:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Pankaj Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/journaldev\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Pankaj Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/05\\\/avoid-concurrentmodificationexception.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/05\\\/avoid-concurrentmodificationexception.html\"},\"author\":{\"name\":\"Pankaj Kumar\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/4e0e33e2f8fd3dcdb0b5239f79fdddc0\"},\"headline\":\"How to Avoid ConcurrentModificationException when using an Iterator\",\"datePublished\":\"2011-05-04T23:01:00+00:00\",\"dateModified\":\"2019-12-04T14:32:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/05\\\/avoid-concurrentmodificationexception.html\"},\"wordCount\":616,\"commentCount\":14,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/05\\\/avoid-concurrentmodificationexception.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"Concurrency\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/05\\\/avoid-concurrentmodificationexception.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/05\\\/avoid-concurrentmodificationexception.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/05\\\/avoid-concurrentmodificationexception.html\",\"name\":\"How to Avoid ConcurrentModificationException when using an Iterator - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/05\\\/avoid-concurrentmodificationexception.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/05\\\/avoid-concurrentmodificationexception.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2011-05-04T23:01:00+00:00\",\"dateModified\":\"2019-12-04T14:32:27+00:00\",\"description\":\"Java Collection classes are fail-fast which means that if the Collection will be changed while some thread is traversing over it using iterator, the\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/05\\\/avoid-concurrentmodificationexception.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/05\\\/avoid-concurrentmodificationexception.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/05\\\/avoid-concurrentmodificationexception.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/05\\\/avoid-concurrentmodificationexception.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"How to Avoid ConcurrentModificationException when using an Iterator\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/4e0e33e2f8fd3dcdb0b5239f79fdddc0\",\"name\":\"Pankaj Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2f858dbb96781814cab524da3bfeeecf59a9327a985d0d8d35f20ba80b964586?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2f858dbb96781814cab524da3bfeeecf59a9327a985d0d8d35f20ba80b964586?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2f858dbb96781814cab524da3bfeeecf59a9327a985d0d8d35f20ba80b964586?s=96&d=mm&r=g\",\"caption\":\"Pankaj Kumar\"},\"sameAs\":[\"http:\\\/\\\/www.journaldev.com\\\/\",\"https:\\\/\\\/www.facebook.com\\\/JournalDev\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/journaldev\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/Pankaj-Kumar\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Avoid ConcurrentModificationException when using an Iterator - Java Code Geeks","description":"Java Collection classes are fail-fast which means that if the Collection will be changed while some thread is traversing over it using iterator, the","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html","og_locale":"en_US","og_type":"article","og_title":"How to Avoid ConcurrentModificationException when using an Iterator - Java Code Geeks","og_description":"Java Collection classes are fail-fast which means that if the Collection will be changed while some thread is traversing over it using iterator, the","og_url":"https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_author":"https:\/\/www.facebook.com\/JournalDev","article_published_time":"2011-05-04T23:01:00+00:00","article_modified_time":"2019-12-04T14:32:27+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Pankaj Kumar","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/journaldev","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Pankaj Kumar","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html"},"author":{"name":"Pankaj Kumar","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/4e0e33e2f8fd3dcdb0b5239f79fdddc0"},"headline":"How to Avoid ConcurrentModificationException when using an Iterator","datePublished":"2011-05-04T23:01:00+00:00","dateModified":"2019-12-04T14:32:27+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html"},"wordCount":616,"commentCount":14,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["Concurrency"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html","url":"https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html","name":"How to Avoid ConcurrentModificationException when using an Iterator - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2011-05-04T23:01:00+00:00","dateModified":"2019-12-04T14:32:27+00:00","description":"Java Collection classes are fail-fast which means that if the Collection will be changed while some thread is traversing over it using iterator, the","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2011\/05\/avoid-concurrentmodificationexception.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"How to Avoid ConcurrentModificationException when using an Iterator"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/4e0e33e2f8fd3dcdb0b5239f79fdddc0","name":"Pankaj Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2f858dbb96781814cab524da3bfeeecf59a9327a985d0d8d35f20ba80b964586?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2f858dbb96781814cab524da3bfeeecf59a9327a985d0d8d35f20ba80b964586?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2f858dbb96781814cab524da3bfeeecf59a9327a985d0d8d35f20ba80b964586?s=96&d=mm&r=g","caption":"Pankaj Kumar"},"sameAs":["http:\/\/www.journaldev.com\/","https:\/\/www.facebook.com\/JournalDev","https:\/\/x.com\/https:\/\/twitter.com\/journaldev"],"url":"https:\/\/www.javacodegeeks.com\/author\/Pankaj-Kumar"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=423"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/423\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}