-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyOnWriteArrayListDemo.java
More file actions
28 lines (23 loc) · 980 Bytes
/
CopyOnWriteArrayListDemo.java
File metadata and controls
28 lines (23 loc) · 980 Bytes
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
// add() method of ArrayList and addAllAbsent() method of CopyOnWriteArrayList.
import java.util.*;
import java.util.concurrent.*;
public class CopyOnWriteArrayListDemo{
public static void main(String[] args){
ArrayList al1 = new ArrayList();
al1.add("A");
al1.add("B");
System.out.println("Array List 1:"+al1); // [A,B]
CopyOnWriteArrayList cowal = new CopyOnWriteArrayList();
cowal.addIfAbsent("A");
cowal.addIfAbsent("C");
System.out.println("Copy On Write Array List Initially:"+cowal); // [A,C]
cowal.addAllAbsent(al1);
System.out.println("Copy On Write Array List After Adding ArrayList 1:"+cowal);
ArrayList al2 = new ArrayList();
al2.add("B");
al2.add("E");
System.out.println("Array List 2:"+al2); // [B,E]
cowal.addAllAbsent(al2);
System.out.println("Copy On Write Array List After Adding ArrayList 2 :"+cowal); //[A,C,B,E]
}
}