CopyOnWriteArrayList containsAll() method in Java

The containsAll() method of CopyOnWriteArrayList class in Java is used to check if this List contains all of the elements in the specified Collection. So basically it is used to check if a List contains a set of elements or not.

Syntax:

boolean containsAll(Collection col)

Parameters: This method accepts a mandatory parameter col which is of the type of collection. This is the collection whose elements are needed to be checked if it is present in the List or not.



Return Value: The method returns True if all elements in the collection col are present in the List otherwise it returns False.

Exception: The method throws NullPointerException if the specified collection is NULL.

Below programs illustrate the containsAll() method of CopyOnWriteArrayList class:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate CopyOnWriteArrayList
//  containsAll() method
  
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] args)
    {
        CopyOnWriteArrayList<String> ArrLis1
            = new CopyOnWriteArrayList<String>();
  
        // Add elements
        ArrLis1.add("geeks");
        ArrLis1.add("shaan");
        ArrLis1.add("gfg");
        ArrLis1.add("programming");
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList1: " + ArrLis1);
  
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList<String> ArrLis2
            = new CopyOnWriteArrayList<String>();
  
        // Add elements
        ArrLis2.add("geeks");
        ArrLis2.add("gfg");
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList2: " + ArrLis2);
  
        // check using function
        if (ArrLis1.containsAll(ArrLis2))
            System.out.println("All list2 elements are present in list1");
        else
            System.out.println("All list2 elements are not present in list1");
  
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList<String> ArrLis3
            = new CopyOnWriteArrayList<String>();
  
        // Add elements
        ArrLis3.add("geeks");
        ArrLis3.add("qwe");
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList3: " + ArrLis3);
  
        // check using function
        if (ArrLis1.containsAll(ArrLis3))
            System.out.println("All list3 elements are present in list1");
        else
            System.out.println("All list3 elements are not present in list1");
    }
}

chevron_right


Output:

CopyOnWriteArrayList1: [geeks, shaan, gfg, programming]
CopyOnWriteArrayList2: [geeks, gfg]
All list2 elements are present in list1
CopyOnWriteArrayList3: [geeks, qwe]
All list3 elements are not present in list1

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate CopyOnWriteArrayList
// containsAll() method
  
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] args)
    {
        CopyOnWriteArrayList<Integer> ArrLis1
            = new CopyOnWriteArrayList<Integer>();
  
        // Add elements
        ArrLis1.add(23);
        ArrLis1.add(65);
        ArrLis1.add(91);
        ArrLis1.add(126);
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList1: " + ArrLis1);
  
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList<Integer> ArrLis2
            = new CopyOnWriteArrayList<Integer>();
  
        // Add elements
        ArrLis2.add(23);
        ArrLis2.add(126);
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList2: " + ArrLis2);
  
        // check using function
        if (ArrLis1.containsAll(ArrLis2))
            System.out.println("All list2 elements are present in list1");
        else
            System.out.println("All list2 elements are not present in list1");
  
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList<Integer> ArrLis3
            = new CopyOnWriteArrayList<Integer>();
  
        // Add elements
        ArrLis3.add(23);
        ArrLis3.add(92);
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList3: " + ArrLis3);
  
        // check using function
        if (ArrLis1.containsAll(ArrLis3))
            System.out.println("All list3 elements are present in list1");
        else
            System.out.println("All list3 elements are not present in list1");
    }
}

chevron_right


Output:

CopyOnWriteArrayList1: [23, 65, 91, 126]
CopyOnWriteArrayList2: [23, 126]
All list2 elements are present in list1
CopyOnWriteArrayList3: [23, 92]
All list3 elements are not present in list1

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CopyOnWriteArrayList.html#containsAll(java.util.Collection)



My Personal Notes arrow_drop_up


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.