CopyOnWriteArrayList addAllAbsent() method in Java with Examples
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.
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:
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
CopyOnWriteArrayList: [2, 3, 4, 7] The collection to be appended: null Trying to append null collection java.lang.NullPointerException
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.