Skip to content

Commit 1a288b5

Browse files
jmelo-lyncodepivovarit
authored andcommitted
Code for Avoiding ConcurrentModificationException when iterating and removing (eugenp#1617)
1 parent 4e56009 commit 1a288b5

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

‎core-java/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,7 @@
8888
- [Java Primitive Conversions](http://www.baeldung.com/java-primitive-conversions)
8989
- [Java Money and the Currency API](http://www.baeldung.com/java-money-and-currency)
9090
- [Guide to Java 8 Comparator.comparing()](http://www.baeldung.com/java-8-comparator-comparing)
91+
- [Avoiding ConcurrentModificationException when iterating and removing](http://www.baeldung.com/avoiding-concurrentmodificationexception-when-iterating-and-removing)
9192
- [Removing all nulls from a List in Java](http://www.baeldung.com/java-remove-nulls-from-list)
9293
- [Removing all duplicates from a List in Java](http://www.baeldung.com/java-remove-duplicates-from-list)
94+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.baeldung.java.collections;
2+
3+
import org.junit.Test;
4+
5+
import java.util.*;
6+
import java.util.concurrent.CopyOnWriteArrayList;
7+
8+
import static java.util.Arrays.asList;
9+
import static org.testng.Assert.assertEquals;
10+
11+
public class ConcurrentModificationExceptionTest {
12+
@Test
13+
public void changingContentWithSetDoesNotThrowConcurrentModificationException() throws Exception {
14+
ArrayList<Object> array = new ArrayList<>(asList(0, "one", 2, "three"));
15+
16+
for (Object item : array) {
17+
array.set(3, 3);
18+
}
19+
}
20+
21+
@Test
22+
public void removingElementUsingIteratorAPI() throws Exception {
23+
List<String> originalList = new ArrayList<>(asList("zero", "one", "two", "three"));
24+
25+
Iterator<String> iterator = originalList.iterator();
26+
27+
while (iterator.hasNext()) {
28+
String next = iterator.next();
29+
if (Objects.equals(next, "one")) iterator.remove();
30+
}
31+
32+
assertEquals(originalList, asList("zero", "two", "three"));
33+
}
34+
35+
@Test
36+
public void modifyingContentAndIteratingUsingListIteratorAPI() throws Exception {
37+
List<String> originalList = new ArrayList<>(asList("zero", "one", "two", "three"));
38+
39+
ListIterator<String> iterator = originalList.listIterator();
40+
41+
while (iterator.hasNext()) {
42+
String next = iterator.next();
43+
if (Objects.equals(next, "one")) {
44+
iterator.set("another");
45+
}
46+
47+
if (Objects.equals(next, "two")) {
48+
iterator.remove();
49+
}
50+
51+
if (Objects.equals(next, "three")) {
52+
iterator.add("four");
53+
}
54+
}
55+
56+
assertEquals(originalList, asList("zero", "another", "three", "four"));
57+
}
58+
59+
@Test
60+
public void removingElementUsingCopyAndListAPI() throws Exception {
61+
List<String> originalList = new ArrayList<>(asList("zero", "one", "two", "three"));
62+
63+
List<String> listCopy = new ArrayList<>(originalList);
64+
65+
for (String next : listCopy) {
66+
if (Objects.equals(next, "one")) originalList.remove(originalList.indexOf(next) - 1);
67+
}
68+
69+
assertEquals(originalList, asList("one", "two", "three"));
70+
}
71+
72+
@Test
73+
public void copyOnWriteList() throws Exception {
74+
List<String> originalList = new CopyOnWriteArrayList<>(asList("zero", "one", "two", "three"));
75+
76+
for (String next : originalList) {
77+
if (Objects.equals(next, "one")) originalList.remove(originalList.indexOf(next) - 1);
78+
}
79+
80+
assertEquals(originalList, asList("one", "two", "three"));
81+
}
82+
}

0 commit comments

Comments
 (0)