Skip to content
Related Articles

Related Articles

Improve Article
CopyOnWriteArrayList containsAll() method in Java
  • Last Updated : 17 Dec, 2018

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:




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




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

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :