Code for Avoiding ConcurrentModificationException when iterating and removing (#1617)
This commit is contained in:
parent
4e560091ea
commit
1a288b5911
|
@ -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)
|
||||
|
||||
|
|
|
@ -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<Object> array = new ArrayList<>(asList(0, "one", 2, "three"));
|
||||
|
||||
for (Object item : array) {
|
||||
array.set(3, 3);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removingElementUsingIteratorAPI() throws Exception {
|
||||
List<String> originalList = new ArrayList<>(asList("zero", "one", "two", "three"));
|
||||
|
||||
Iterator<String> 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<String> originalList = new ArrayList<>(asList("zero", "one", "two", "three"));
|
||||
|
||||
ListIterator<String> 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<String> originalList = new ArrayList<>(asList("zero", "one", "two", "three"));
|
||||
|
||||
List<String> 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<String> 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"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue