|
| 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