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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


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-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.