-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFailSafeInteration.java
More file actions
63 lines (52 loc) · 1.62 KB
/
Copy pathFailSafeInteration.java
File metadata and controls
63 lines (52 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package interviewQuestions1;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* 1. Fail-safe iterators means they will not throw any exception even
* if the collection is modified while iterating over it.
*
* 2. In case of CopyOnWriteArrayList the original collections is passed and is stored in the iterator
*
* */
public class FailSafeInteration {
public static void main(String[] args) {
List<Integer> integers= new CopyOnWriteArrayList<>();
integers.add(1);
integers.add(2);
integers.add(3);
Iterator<Integer> itr2 = integers.iterator();
while (itr2.hasNext()) {
int a = itr2.next();
if (a == 1) {
integers.remove(Integer.valueOf(a));
}
System.out.println(a);
}
// the iterator will print all the elements 1,2,3
//because it traverses over the snapshot of the collection elements
System.out.println("***"+ integers); //collection now has only elements 2 and 3
System.out.println("*************Second Example**********");
List<Integer> integers2= new CopyOnWriteArrayList<>();
integers2.add(1);
integers2.add(2);
integers2.add(3);
Iterator<Integer> itr5 = integers2.iterator();
while(itr5.hasNext()) {
Integer a = itr5.next();
//integers2.remove(a);
if(a==1) {
itr5.remove();
}
}
Iterator<Integer> itr6 = integers2.iterator();
while(itr6.hasNext()) {
Integer a = itr6.next();
System.out.println(a);
}
}
}
/* Downsides of this iterator are:
1. They will not reflect the latest state of the collection.
2. It requires extra memory as it clones the collection.
*/