The CopyOnWriteArrayList.addAll()
method in Java is used to add all the elements from a specified collection to the CopyOnWriteArrayList
.
Table of Contents
- Introduction
addAll
Method Syntax- Examples
- Adding All Elements from Another Collection
- Adding All Elements at a Specific Position
- Real-World Use Case
- Example: Merging User Lists in a Concurrent Application
- Conclusion
Introduction
The CopyOnWriteArrayList
is a thread-safe variant of ArrayList
in Java. It is part of the java.util.concurrent
package and is designed for scenarios where read operations are more frequent than write operations. The addAll
method allows you to add all the elements from another collection to the CopyOnWriteArrayList
. The CopyOnWriteArrayList
achieves thread safety by creating a new copy of the array whenever it is modified.
addAll() Method Syntax
There are two variations of the addAll
method:
Basic Add All
public boolean addAll(Collection<? extends E> c)
- The method takes one parameter:
c
of typeCollection<? extends E>
, which represents the collection containing elements to be added to the list.
- The method returns
true
if the list changed as a result of the call.
Add All at a Specific Position
public boolean addAll(int index, Collection<? extends E> c)
- The method takes two parameters:
index
of typeint
, which represents the position at which to insert the first element from the specified collection.c
of typeCollection<? extends E>
, which represents the collection containing elements to be added to the list.
- The method returns
true
if the list changed as a result of the call.
Examples
Adding All Elements from Another Collection
The basic addAll
method can be used to add all elements from another collection to a CopyOnWriteArrayList
.
Example
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
public class AddAllExample {
public static void main(String[] args) {
// Creating a CopyOnWriteArrayList with String elements
CopyOnWriteArrayList<String> names = new CopyOnWriteArrayList<>();
// Adding elements to the CopyOnWriteArrayList
names.add("Ravi");
names.add("Priya");
// Creating another collection with String elements
ArrayList<String> newNames = new ArrayList<>();
newNames.add("Vijay");
newNames.add("Anita");
// Adding all elements from newNames to names
names.addAll(newNames);
// Printing the CopyOnWriteArrayList
System.out.println("CopyOnWriteArrayList: " + names);
}
}
Output:
CopyOnWriteArrayList: [Ravi, Priya, Vijay, Anita]
Adding All Elements at a Specific Position
The addAll
method can also be used to add elements from another collection at a specific position in the CopyOnWriteArrayList
.
Example
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
public class AddAllAtIndexExample {
public static void main(String[] args) {
// Creating a CopyOnWriteArrayList with String elements
CopyOnWriteArrayList<String> names = new CopyOnWriteArrayList<>();
// Adding elements to the CopyOnWriteArrayList
names.add("Ravi");
names.add("Priya");
// Creating another collection with String elements
ArrayList<String> newNames = new ArrayList<>();
newNames.add("Vijay");
newNames.add("Anita");
// Adding all elements from newNames to names at index 1
names.addAll(1, newNames);
// Printing the CopyOnWriteArrayList
System.out.println("CopyOnWriteArrayList: " + names);
}
}
Output:
CopyOnWriteArrayList: [Ravi, Vijay, Anita, Priya]
Real-World Use Case
Example: Merging User Lists in a Concurrent Application
A common real-world use case for CopyOnWriteArrayList
is managing thread-safe lists of users and merging multiple user lists.
Example
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
public class UserListManager {
public static void main(String[] args) {
// Creating a CopyOnWriteArrayList to manage user names
CopyOnWriteArrayList<String> userList = new CopyOnWriteArrayList<>();
// Adding user names to the CopyOnWriteArrayList
userList.add("Ravi");
userList.add("Priya");
// Creating another collection with new user names
ArrayList<String> newUserList = new ArrayList<>();
newUserList.add("Vijay");
newUserList.add("Anita");
// Simulating concurrent operations
Thread writerThread1 = new Thread(() -> {
userList.addAll(newUserList);
System.out.println("New users added.");
});
Thread writerThread2 = new Thread(() -> {
userList.addAll(1, newUserList);
System.out.println("New users added at index 1.");
});
// Starting the threads
writerThread1.start();
writerThread2.start();
// Waiting for the threads to finish
try {
writerThread1.join();
writerThread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
// Printing the final user list
System.out.println("Final user list: " + userList);
}
}
Output:
New users added.
New users added at index 1.
Final user list: [Ravi, Vijay, Anita, Vijay, Anita, Priya]
In this example, CopyOnWriteArrayList
is used to manage a thread-safe list of user names, allowing concurrent operations while merging multiple user lists.
Conclusion
The CopyOnWriteArrayList.addAll()
method in Java provides a way to add all elements from a specified collection to a CopyOnWriteArrayList
in a thread-safe manner. By understanding how to use this method, you can efficiently manage collections of elements in your Java applications, especially in concurrent environments. The method allows you to merge collections and insert elements at specific positions, making it a versatile tool for data management in multi-threaded scenarios.