Constructors
Methods
Basic operations
import java.util.*;
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) {
CopyOnWriteArrayList<Integer> numbers= new CopyOnWriteArrayList<>();
// Using the add() method
numbers.add(2);
numbers.add(4);
numbers.addIfAbsent(6);
System.out.println("list: " + numbers);
CopyOnWriteArrayList alphanum = new CopyOnWriteArrayList();
alphanum.add("a");
alphanum.add("b");
alphanum.addAll(numbers);
System.out.println("New list: " + alphanum);
alphanum.set(2,"c");
System.out.println("Final list after updation:" + alphanum);
}
}
You can also try this code with Online Java Compiler
Run Code
Output:
list: [2, 4, 6]
New list: [a, b, 2, 4, 6]
Final list after updation:[a, b, c, 4, 6]
Try it on Java Online Compiler.
import java.util.*;
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) {
CopyOnWriteArrayList<Integer> numbers= new CopyOnWriteArrayList<>();
// Using the add() method
numbers.add(2);
numbers.add(4);
numbers.add(6);
numbers.addIfAbsent(8);
System.out.println("list: " + numbers);
System.out.println("Element at index 2: " + numbers.get(2));
System.out.println("list after updation:");
numbers.forEach((e) -> {
e = e * 10;
System.out.print(e + " ");
});
System.out.println("\nSublist 1-3: " + numbers.subList(0,3));
}
}
You can also try this code with Online Java Compiler
Run Code
Output:
list: [2, 4, 6, 8]
Element at index 2: 6
list after updation:
20 40 60 80
Sublist 1-3: [2, 4, 6]
import java.util.*;
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) {
CopyOnWriteArrayList<Integer> numbers= new CopyOnWriteArrayList<>();
// Using the add() method
numbers.add(2);
numbers.add(4);
numbers.add(6);
numbers.addIfAbsent(8);
System.out.println("list: " + numbers);
System.out.println("Removing Element at index 3.. ");
numbers.remove(3);
System.out.println("Removing Elements less than 5.. ");
numbers.removeIf(e -> e<5);
System.out.println("Final list : " + numbers);
}
}
You can also try this code with Online Java Compiler
Run Code
Output:
list: [2, 4, 6, 8]
Removing Element at index 3..
Removing Elements less than 5..
Final list : [6]
Check out this problem - Duplicate Subtree In Binary Tree
FAQs
-
What is the difference between Synchronized ArrayList and CopyOnWriteArrayList?
Synchronised Arraylist iterators are fail-fast and must be contained within the synchronised block. CopyOnWriteArrayList iterators are fail-safe and can be used outside the synchronised block. The entire ArrayList is blocked for both read and write operations in a Synchronised ArrayList. The ArrayList is blocked only for write operations in a CopyOnWriteArrayList.
-
What is the advantage of using CopyOnWriteArrayList?
Making a fresh copy of an array consumes both time and memory overhead. To avoid this, developers often resort to using a synchronised ArrayList instead. However, every time you iterate across the contents of the collection, there is a need to synchronise all operations, including read and write, to ensure consistency. The CopyOnWriteArrayList easily solves this problem.
Key Takeaways
This article extensively discusses CopyOnWriteArrayList. We hope that this blog has helped you enhance your knowledge about the different methods and ways of implementing the CopyOnWriteArrayList class in Java. If you would like to learn more, check out our articles on Data Structures in Java, Multithreading in Java and lists vs Arrays.
Recommended Articles::
Recommended problems -
Explore our Coding Ninjas Library and upvote our blog to help other ninjas grow. Happy Coding!