Skip to content

Commit 6f386f7

Browse files
thakursantoshpivovarit
authored andcommitted
Code for BAEL-1437 - thakursantosh/st1972@gmail.com (eugenp#3294)
* New code for First Article 'Types of Bean Injection' * Adding code for BAEL-1306 * Code changes for BAEL-1306 * Removed code of my evaluation article * Simplified code to use number arrays BAEL-1306 * Simplified code for BAEL-1306 * Code commit for BAEL-1437 * Removed code that was added for BAEL-1437 * Code commit for BAEL-1437
1 parent 7285447 commit 6f386f7

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.iterators;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.concurrent.ConcurrentHashMap;
6+
7+
/**
8+
* Source code https://github.com/eugenp/tutorials
9+
*
10+
* @author Santosh Thakur
11+
*/
12+
13+
public class Iterators {
14+
15+
public static int failFast1() {
16+
ArrayList<Integer> numbers = new ArrayList<>();
17+
18+
numbers.add(10);
19+
numbers.add(20);
20+
numbers.add(30);
21+
numbers.add(40);
22+
23+
Iterator<Integer> iterator = numbers.iterator();
24+
while (iterator.hasNext()) {
25+
Integer number = iterator.next();
26+
numbers.add(50);
27+
}
28+
29+
return numbers.size();
30+
}
31+
32+
public static int failFast2() {
33+
ArrayList<Integer> numbers = new ArrayList<>();
34+
numbers.add(10);
35+
numbers.add(20);
36+
numbers.add(30);
37+
numbers.add(40);
38+
39+
Iterator<Integer> iterator = numbers.iterator();
40+
while (iterator.hasNext()) {
41+
if (iterator.next() == 30) {
42+
// will not throw Exception
43+
iterator.remove();
44+
}
45+
}
46+
47+
System.out.println("using iterator's remove method = " + numbers);
48+
49+
iterator = numbers.iterator();
50+
while (iterator.hasNext()) {
51+
if (iterator.next() == 40) {
52+
// will throw Exception on
53+
// next call of next() method
54+
numbers.remove(2);
55+
}
56+
}
57+
58+
return numbers.size();
59+
}
60+
61+
public static int failSafe1() {
62+
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
63+
64+
map.put("First", 10);
65+
map.put("Second", 20);
66+
map.put("Third", 30);
67+
map.put("Fourth", 40);
68+
69+
Iterator<String> iterator = map.keySet()
70+
.iterator();
71+
72+
while (iterator.hasNext()) {
73+
String key = iterator.next();
74+
map.put("Fifth", 50);
75+
}
76+
77+
return map.size();
78+
}
79+
80+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.iterators;
2+
3+
import static com.baeldung.iterators.Iterators.failFast1;
4+
import static com.baeldung.iterators.Iterators.failFast2;
5+
import static com.baeldung.iterators.Iterators.failSafe1;
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
8+
9+
import java.util.ConcurrentModificationException;
10+
11+
import org.junit.Test;
12+
13+
/**
14+
* Source code https://github.com/eugenp/tutorials
15+
*
16+
* @author Santosh Thakur
17+
*/
18+
19+
public class IteratorsTest {
20+
21+
@Test
22+
public void whenFailFast_ThenThrowsException() {
23+
assertThatThrownBy(() -> {
24+
failFast1();
25+
}).isInstanceOf(ConcurrentModificationException.class);
26+
}
27+
28+
@Test
29+
public void whenFailFast_ThenThrowsExceptionInSecondIteration() {
30+
assertThatThrownBy(() -> {
31+
failFast2();
32+
}).isInstanceOf(ConcurrentModificationException.class);
33+
}
34+
35+
@Test
36+
public void whenFailSafe_ThenDoesNotThrowException() {
37+
assertThat(failSafe1()).isGreaterThanOrEqualTo(0);
38+
}
39+
40+
}

0 commit comments

Comments
 (0)