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.
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:
// 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); } } |
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 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); } } } |
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-
Contact Us