VOOZH about

URL: https://www.geeksforgeeks.org/java/copyonwritearraylist-addallabsent-method-in-java-with-examples/

⇱ CopyOnWriteArrayList addAllAbsent() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CopyOnWriteArrayList addAllAbsent() method in Java with Examples

Last Updated : 11 Jul, 2025
The addAllAbsent(E e) method of CopyOnWriteArrayList appends 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 collection which specifies the collection to be appended to the list if it is not present. Return Value: The function returns an integer value which is the number if elements added in the List. Exception: This method throws NullPointerException if the specified collection is null. Below programs illustrate the addAllAbsent() function: Program 1:
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
Output:
CopyOnWriteArrayList: [2, 3, 4, 7]
The collection to be appended: null

Trying to append null collection

java.lang.NullPointerException
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/CopyOnWriteArrayList.html#addAllAbsent-java.util.Collection-
Comment