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
Recommended Posts:
- CopyOnWriteArrayList set() method in Java with Examples
- CopyOnWriteArrayList subList() method in Java with Examples
- CopyOnWriteArrayList equals() method in Java with Examples
- CopyOnWriteArrayList forEach() method in Java with Examples
- CopyOnWriteArrayList addAll() method in Java with Examples
- CopyOnWriteArrayList retainAll() method in Java with Examples
- CopyOnWriteArrayList remove() method in Java with Examples
- CopyOnWriteArrayList removeIf() method in Java with Examples
- CopyOnWriteArrayList removeAll() method in Java with Examples
- CopyOnWriteArrayList contains() method in Java
- CopyOnWriteArrayList add() method in Java
- CopyOnWriteArrayList get() method in Java
- CopyOnWriteArrayList lastIndexOf() method in Java
- CopyOnWriteArrayList clear() method in Java
- CopyOnWriteArrayList containsAll() method in Java
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.



