From 1a288b591186c7b8699a096b4ef5ac40b9080c4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Melo?= Date: Mon, 10 Apr 2017 11:44:16 +0100 Subject: [PATCH] Code for Avoiding ConcurrentModificationException when iterating and removing (#1617) --- core-java/README.md | 2 + .../ConcurrentModificationExceptionTest.java | 82 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 core-java/src/test/java/com/baeldung/java/collections/ConcurrentModificationExceptionTest.java diff --git a/core-java/README.md b/core-java/README.md index e75701717f..a7c8b0e855 100644 --- a/core-java/README.md +++ b/core-java/README.md @@ -88,5 +88,7 @@ - [Java Primitive Conversions](http://www.baeldung.com/java-primitive-conversions) - [Java Money and the Currency API](http://www.baeldung.com/java-money-and-currency) - [Guide to Java 8 Comparator.comparing()](http://www.baeldung.com/java-8-comparator-comparing) +- [Avoiding ConcurrentModificationException when iterating and removing](http://www.baeldung.com/avoiding-concurrentmodificationexception-when-iterating-and-removing) - [Removing all nulls from a List in Java](http://www.baeldung.com/java-remove-nulls-from-list) - [Removing all duplicates from a List in Java](http://www.baeldung.com/java-remove-duplicates-from-list) + diff --git a/core-java/src/test/java/com/baeldung/java/collections/ConcurrentModificationExceptionTest.java b/core-java/src/test/java/com/baeldung/java/collections/ConcurrentModificationExceptionTest.java new file mode 100644 index 0000000000..100d25ab8d --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/collections/ConcurrentModificationExceptionTest.java @@ -0,0 +1,82 @@ +package com.baeldung.java.collections; + +import org.junit.Test; + +import java.util.*; +import java.util.concurrent.CopyOnWriteArrayList; + +import static java.util.Arrays.asList; +import static org.testng.Assert.assertEquals; + +public class ConcurrentModificationExceptionTest { + @Test + public void changingContentWithSetDoesNotThrowConcurrentModificationException() throws Exception { + ArrayList array = new ArrayList<>(asList(0, "one", 2, "three")); + + for (Object item : array) { + array.set(3, 3); + } + } + + @Test + public void removingElementUsingIteratorAPI() throws Exception { + List originalList = new ArrayList<>(asList("zero", "one", "two", "three")); + + Iterator iterator = originalList.iterator(); + + while (iterator.hasNext()) { + String next = iterator.next(); + if (Objects.equals(next, "one")) iterator.remove(); + } + + assertEquals(originalList, asList("zero", "two", "three")); + } + + @Test + public void modifyingContentAndIteratingUsingListIteratorAPI() throws Exception { + List originalList = new ArrayList<>(asList("zero", "one", "two", "three")); + + ListIterator iterator = originalList.listIterator(); + + while (iterator.hasNext()) { + String next = iterator.next(); + if (Objects.equals(next, "one")) { + iterator.set("another"); + } + + if (Objects.equals(next, "two")) { + iterator.remove(); + } + + if (Objects.equals(next, "three")) { + iterator.add("four"); + } + } + + assertEquals(originalList, asList("zero", "another", "three", "four")); + } + + @Test + public void removingElementUsingCopyAndListAPI() throws Exception { + List originalList = new ArrayList<>(asList("zero", "one", "two", "three")); + + List listCopy = new ArrayList<>(originalList); + + for (String next : listCopy) { + if (Objects.equals(next, "one")) originalList.remove(originalList.indexOf(next) - 1); + } + + assertEquals(originalList, asList("one", "two", "three")); + } + + @Test + public void copyOnWriteList() throws Exception { + List originalList = new CopyOnWriteArrayList<>(asList("zero", "one", "two", "three")); + + for (String next : originalList) { + if (Objects.equals(next, "one")) originalList.remove(originalList.indexOf(next) - 1); + } + + assertEquals(originalList, asList("one", "two", "three")); + } +}