ArrayList containsAll(Collection c) method in java

Let’s learn ArrayList containsAll(Collection c) method in java.

ArrayList containsAll(Collection c) method in java

containsAll(Collection c) method of ArrayList class returns true if this collection contains all of the elements in the specified collection.

Syntax:

arraylist.containsAll(Collection c);

Parameters:

c collection to be checked for containment in this collection.

Throws:

ClassCastException – if the types of one or more elements in the specified collection are incompatible with this collection(optional).

NullPointerException – if the specified collection contains one or more null elements and this collection does not permit nullelements(optional), or if the specified collection is null.

Also read – ArrayList in java

Now let’s see example on ArrayList containsAll(Collection c) method.

import java.util.ArrayList;
public class ArrayListContainsAllMethodExample
{
   public static void main(String[] args)
   {
      ArrayList<String> al1 = new ArrayList<String>();
      al1.add("red");
      al1.add("green");
      al1.add("blue");
      System.out.println("ArrayList 1: " + al1);
      // creating another ArrayList
      ArrayList<String> al2 = new ArrayList<String>();
      // adding elements to second ArrayList
      al2.add("blue");
      al2.add("green");
      System.out.println("ArrayList 2: " + al2);
      // checking if ArrayList 1 contains ArrayList 2
      boolean output1 = al1.containsAll(al2);
      System.out.println("Does ArrayList 1 contains all elements of ArrayList 2?: " + output1);
      // checking if ArrayList 2 contains ArrayList 1
      boolean output2 = al2.containsAll(al1);
      System.out.println("Does ArrayList 2 contains all elements of ArrayList 1?: " + output2);
   }
}


Output:

ArrayList 1: [red, green, blue]
ArrayList 2: [blue, green]
Does ArrayList 1 contains all elements of ArrayList 2?: true
Does ArrayList 2 contains all elements of ArrayList 1?: false