Last Updated : 11 Jul, 2025
The
addAllAbsent(E e)method of
CopyOnWriteArrayListappends all of the elements in the specified collection that are not already contained in this list, to the end of this list, in the order that they are returned by the specified collection's iterator.
Syntax:public int addAllAbsent(Collection<E> collection)Parameters:
The function accepts a single mandatory parameter
collectionwhich specifies the collection to be appended to the list if it is not present.
Return Value:The function returns
an integer valuewhich is the number if elements added in the List.
Exception:This method throws
NullPointerExceptionif the specified collection is null. Below programs illustrate the addAllAbsent() function:
Program 1: Java
// Java Program to illustrate the CopyOnWriteArrayList
// addAllAbsent() method in Java
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create object of CopyOnWriteArrayList
CopyOnWriteArrayList<Integer> ArrLis
= new CopyOnWriteArrayList<Integer>();
// Add elements
ArrLis.add(2);
ArrLis.add(3);
ArrLis.add(4);
ArrLis.add(7);
// print CopyOnWriteArrayList
System.out.println("CopyOnWriteArrayList: "
+ ArrLis);
// creating an Empty Integer ArrayList
ArrayList<Integer> arr = new ArrayList<Integer>(4);
// using add() to initialize values
// [1, 2, 3, 4]
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);
// list initially
System.out.println("The collection to be appended: "
+ arr);
// Append the list arr in the list ArrLis
// using addAllAbsent() method
System.out.println("\nNumber of elements appended"
+ " using addAllAbsent() method: "
+ ArrLis.addAllAbsent(arr));
// print CopyOnWriteArrayList
System.out.println("Modified CopyOnWriteArrayList: "
+ ArrLis);
}
}
Output:
CopyOnWriteArrayList: [2, 3, 4, 7] The collection to be appended: [1, 2, 3, 4] Number of elements appended using addAllAbsent() method: 1 Modified CopyOnWriteArrayList: [2, 3, 4, 7, 1]Program 2:
To demonstrate NullPointerException
Java
// Java Program to illustrate the CopyOnWriteArrayList
// addAllAbsent() method in Java
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create object of CopyOnWriteArrayList
CopyOnWriteArrayList<Integer> ArrLis
= new CopyOnWriteArrayList<Integer>();
// Add elements
ArrLis.add(2);
ArrLis.add(3);
ArrLis.add(4);
ArrLis.add(7);
// print CopyOnWriteArrayList
System.out.println("CopyOnWriteArrayList: "
+ ArrLis);
// creating a null Integer ArrayList
ArrayList<Integer> arr = null;
// list initially
System.out.println("The collection to be appended: "
+ arr);
try {
// Append the list arr in the list ArrLis
// using addAllAbsent() method
System.out.println("\nTrying to append null collection\n");
ArrLis.addAllAbsent(arr);
}
catch (Exception e) {
System.out.println(e);
}
}
}
Output:
CopyOnWriteArrayList: [2, 3, 4, 7] The collection to be appended: null Trying to append null collection java.lang.NullPointerExceptionReference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/CopyOnWriteArrayList.html#addAllAbsent-java.util.Collection-
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4