{"id":1447,"date":"2019-04-08T12:39:12","date_gmt":"2019-04-08T07:09:12","guid":{"rendered":"http:\/\/blog.openwebsolutions.in\/?p=1447"},"modified":"2019-04-08T12:39:12","modified_gmt":"2019-04-08T07:09:12","slug":"solution-of-concurrent-exception-in-java-android","status":"publish","type":"post","link":"https:\/\/openwebsolutions.in\/blog\/solution-of-concurrent-exception-in-java-android\/","title":{"rendered":"Solution Of Concurrent Exception In JAVA (Android)"},"content":{"rendered":"<p>Hello guys! We all know about a Collection named ArrayList. You can find this class in java. This class belongs to the package &#8220;package java.util;&#8221;. We can store any kind of values inside this class and can sort, reverse accordingly. But you will face a problem while sorting with respect to any class with the help of comparator. In that case you are sorting with &#8220;Collections&#8221; class like &#8220;Collections.sort(myList,new Comparator)&#8221;. There might be an exception called &#8220;ConcurrentModificationException&#8221;, this happens because of the &#8220;Iterator&#8221; class. This &#8220;Iterator&#8221; class used to add or remove any data of the collection. Now, you have a question that you are not adding or removing any kind of data of that collection and you are not using &#8220;Iterator&#8221; class either then why are you going to face this exception? Yes! my friend the main problem is the method &#8220;sort()&#8221;, which belongs to the &#8220;Collections&#8221; class. In this method when you are sorting the collection the method converts the collection to &#8220;Array&#8221; then sort that collection, in this instance &#8220;Iterator&#8221; class is being used.<\/p>\n<p>package java.util;<\/p>\n<p>public static &lt;T&gt; void sort(List&lt;T&gt; list, Comparator&lt;? super T&gt; c) {<\/p>\n<p>\/\/ BEGIN Android-changed: Compat behavior for apps targeting APIs &lt;= 25.<\/p>\n<p>\/\/ list.sort(c);<\/p>\n<p>int targetSdkVersion = VMRuntime.getRuntime().getTargetSdkVersion();<\/p>\n<p>if (targetSdkVersion &gt; 25) {<\/p>\n<p>list.sort(c);<\/p>\n<p>} else {<\/p>\n<p>\/\/ Compatibility behavior for API &lt;= 25. http:\/\/b\/33482884<\/p>\n<p>if (list.getClass() == ArrayList.class) {<\/p>\n<p>Arrays.sort((T[]) ((ArrayList) list).elementData, 0, list.size(), c);<\/p>\n<p>return;<\/p>\n<p>}<\/p>\n<p>Object[] a = list.toArray();<\/p>\n<p>Arrays.sort(a, (Comparator) c);<\/p>\n<p>ListIterator&lt;T&gt; i = list.listIterator();<\/p>\n<p>for (int j = 0; j &lt; a.length; j++) {<\/p>\n<p>i.next();<\/p>\n<p>i.set((T) a[j]);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/ END Android-changed: Compat behavior for apps targeting APIs &lt;= 25.<\/p>\n<p>}<\/p>\n<p>So, now you need to handle this exception. But that is almost untouchable because it&#8217;s an inner class. So, how can you solve this? yes, you&#8217;re in right place now. There is another collection named &#8220;CopyOnWriteArrayList&#8221;, it belongs to package &#8220;package java.util.concurrent;&#8221;, this class also implements &#8220;List&lt;&gt;&#8221; like &#8220;ArrayList&lt;&gt;&#8221;. So, you won&#8217;t face any problem of casting. Now your question is what it does? what&#8217;s the difference? yes, there is a difference, this class has its own sort() method, which doesn&#8217;t use &#8220;Iterator&#8221; class. So, In this way, you can avoid &#8220;ConcurrentModificationException&#8221;. Your code will run smoothly.<\/p>\n<p>package java.util.concurrent;<\/p>\n<p>public void sort(Comparator&lt;? super E&gt; c) {<\/p>\n<p>synchronized (lock) {<\/p>\n<p>Object[] elements = getArray();<\/p>\n<p>Object[] newElements = Arrays.copyOf(elements, elements.length);<\/p>\n<p>@SuppressWarnings(&#8220;unchecked&#8221;) E[] es = (E[])newElements;<\/p>\n<p>Arrays.sort(es, c);<\/p>\n<p>setArray(newElements);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello guys! We all know about a Collection named ArrayList. You can find this class in java. This class belongs to the package &#8220;package java.util;&#8221;. We can store any kind of values inside this class and can sort, reverse accordingly. But you will face a problem while sorting with respect to any class with the [&hellip;]<\/p>\n","protected":false},"author":26,"featured_media":1545,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[94,72,5],"tags":[95,73,107],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Solution Of Concurrent Exception In JAVA (Android)<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/openwebsolutions.in\/blog\/solution-of-concurrent-exception-in-java-android\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Solution Of Concurrent Exception In JAVA (Android)\" \/>\n<meta property=\"og:description\" content=\"Hello guys! We all know about a Collection named ArrayList. You can find this class in java. This class belongs to the package &#8220;package java.util;&#8221;. We can store any kind of values inside this class and can sort, reverse accordingly. But you will face a problem while sorting with respect to any class with the [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/openwebsolutions.in\/blog\/solution-of-concurrent-exception-in-java-android\/\" \/>\n<meta property=\"og:site_name\" content=\"Openweb Solutions Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-08T07:09:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/04\/java.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"576\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/\",\"name\":\"Openweb Solutions Blog\",\"description\":\"Transforming ideas into reality\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/openwebsolutions.in\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/solution-of-concurrent-exception-in-java-android\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/04\/java.png\",\"width\":1024,\"height\":576},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/solution-of-concurrent-exception-in-java-android\/#webpage\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/solution-of-concurrent-exception-in-java-android\/\",\"name\":\"Solution Of Concurrent Exception In JAVA (Android)\",\"isPartOf\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/solution-of-concurrent-exception-in-java-android\/#primaryimage\"},\"datePublished\":\"2019-04-08T07:09:12+00:00\",\"dateModified\":\"2019-04-08T07:09:12+00:00\",\"author\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/f78c49d2ecec483cf2dc09e314edd870\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/openwebsolutions.in\/blog\/solution-of-concurrent-exception-in-java-android\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/f78c49d2ecec483cf2dc09e314edd870\",\"name\":\"Soumendu Santra\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f8372e790aaebb27615f6c69b8205ea1?s=96&r=g\",\"caption\":\"Soumendu Santra\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1447"}],"collection":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/users\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=1447"}],"version-history":[{"count":4,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1447\/revisions"}],"predecessor-version":[{"id":1546,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1447\/revisions\/1546"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media\/1545"}],"wp:attachment":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=1447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=1447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=1447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}