tag:blogger.com,1999:blog-8712770457197348465.post1192121229926973518..comments2024-03-28T02:47:19.159-07:00Comments on Javarevisited: How to avoid of fix ConcurrentModificationException in Java? Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-8712770457197348465.post-7282625609152495672018-10-08T07:21:47.552-07:002018-10-08T07:21:47.552-07:00@Javin: The remove inside an extended for loop fai...@Javin: The remove inside an extended for loop fails unfortunately, even converting to a functional implementation like this:<br /><br />lb.stream().filter(t -> t.contains("Java")).forEach(t -> lb.remove(t));<br /><br />J. Ernesto Aneiroshttps://www.blogger.com/profile/15587633878909019151noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-71026992277396800582018-10-08T07:14:24.052-07:002018-10-08T07:14:24.052-07:00@Najstariji zanatlija
Uhmm it doesn't look ri...@Najstariji zanatlija<br /><br />Uhmm it doesn't look right, check your code, the exception will be thrown in both cases. Just in case: the contains method is case sensitive, behind the scene it uses indexOf which matches char by char.<br />J. Ernesto Aneiroshttps://www.blogger.com/profile/15587633878909019151noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-38595356922941905002018-10-07T06:46:11.350-07:002018-10-07T06:46:11.350-07:00If we write like this, there is an exception :
f...If we write like this, there is an exception :<br /><br />for (String book : listOfBooks) {<br /> if (book.contains("Code")) {<br /> listOfBooks.remove(book);<br /> }<br />}<br /><br />On the other hand, if we write like this, there is NO concurrent modification exception !<br /><br />for (String book : listOfBooks) {<br /> if (book.contains("Java")) {<br /> Najstariji zanatlijahttps://www.blogger.com/profile/15419227034409122786noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-76246584330154575532018-10-06T00:55:55.255-07:002018-10-06T00:55:55.255-07:00Hello -j aneiros , thanks for removeIf(), it's...Hello -j aneiros , thanks for removeIf(), it's seriously good edition and much cleaner code. Regarding remove() not throwing exception, did you tested it in the condition where you iterate or loop and then remove?javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-74839382842191527622018-10-05T09:46:43.040-07:002018-10-05T09:46:43.040-07:00Of course if multiple threads access a list concur...Of course if multiple threads access a list concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.)<br /><br />This is typically J. Ernesto Aneiroshttps://www.blogger.com/profile/15587633878909019151noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-88399988967501822952018-10-05T09:38:49.229-07:002018-10-05T09:38:49.229-07:00In fact the remove method doesn't throw that e...In fact the remove method doesn't throw that exception anymore!<br /><br />listOfBooks.remove("Programming Pearls");J. Ernesto Aneiroshttps://www.blogger.com/profile/15587633878909019151noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-47209885183216732622018-10-05T09:02:33.588-07:002018-10-05T09:02:33.588-07:00Sorry Jevin but this is not using the Java 8, just...Sorry Jevin but this is not using the Java 8, just call removeIf with a predicate like:<br /><br />lb.removeIf(t -> "Clean Code".equals(t));<br /><br />To all reading this out there: Java 8 is a complete new animal!<br /><br /><br />J. Ernesto Aneiroshttps://www.blogger.com/profile/15587633878909019151noreply@blogger.com