Code for BAEL-1437 - thakursantosh/st1972@gmail.com (#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
This commit is contained in:
parent
72854479e7
commit
6f386f79e5
|
@ -0,0 +1,80 @@
|
||||||
|
package com.baeldung.iterators;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Source code https://github.com/eugenp/tutorials
|
||||||
|
*
|
||||||
|
* @author Santosh Thakur
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Iterators {
|
||||||
|
|
||||||
|
public static int failFast1() {
|
||||||
|
ArrayList<Integer> numbers = new ArrayList<>();
|
||||||
|
|
||||||
|
numbers.add(10);
|
||||||
|
numbers.add(20);
|
||||||
|
numbers.add(30);
|
||||||
|
numbers.add(40);
|
||||||
|
|
||||||
|
Iterator<Integer> iterator = numbers.iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
Integer number = iterator.next();
|
||||||
|
numbers.add(50);
|
||||||
|
}
|
||||||
|
|
||||||
|
return numbers.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int failFast2() {
|
||||||
|
ArrayList<Integer> numbers = new ArrayList<>();
|
||||||
|
numbers.add(10);
|
||||||
|
numbers.add(20);
|
||||||
|
numbers.add(30);
|
||||||
|
numbers.add(40);
|
||||||
|
|
||||||
|
Iterator<Integer> iterator = numbers.iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
if (iterator.next() == 30) {
|
||||||
|
// will not throw Exception
|
||||||
|
iterator.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("using iterator's remove method = " + numbers);
|
||||||
|
|
||||||
|
iterator = numbers.iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
if (iterator.next() == 40) {
|
||||||
|
// will throw Exception on
|
||||||
|
// next call of next() method
|
||||||
|
numbers.remove(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return numbers.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int failSafe1() {
|
||||||
|
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
map.put("First", 10);
|
||||||
|
map.put("Second", 20);
|
||||||
|
map.put("Third", 30);
|
||||||
|
map.put("Fourth", 40);
|
||||||
|
|
||||||
|
Iterator<String> iterator = map.keySet()
|
||||||
|
.iterator();
|
||||||
|
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
String key = iterator.next();
|
||||||
|
map.put("Fifth", 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
return map.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package com.baeldung.iterators;
|
||||||
|
|
||||||
|
import static com.baeldung.iterators.Iterators.failFast1;
|
||||||
|
import static com.baeldung.iterators.Iterators.failFast2;
|
||||||
|
import static com.baeldung.iterators.Iterators.failSafe1;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
|
import java.util.ConcurrentModificationException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Source code https://github.com/eugenp/tutorials
|
||||||
|
*
|
||||||
|
* @author Santosh Thakur
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class IteratorsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenFailFast_ThenThrowsException() {
|
||||||
|
assertThatThrownBy(() -> {
|
||||||
|
failFast1();
|
||||||
|
}).isInstanceOf(ConcurrentModificationException.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenFailFast_ThenThrowsExceptionInSecondIteration() {
|
||||||
|
assertThatThrownBy(() -> {
|
||||||
|
failFast2();
|
||||||
|
}).isInstanceOf(ConcurrentModificationException.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenFailSafe_ThenDoesNotThrowException() {
|
||||||
|
assertThat(failSafe1()).isGreaterThanOrEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue