What is ArrayList?
The ArrayList class is a resizable array, which can be found in the java.util package. ArrayList objects are created in the same fashion as other object classes. The primary difference with ArrayLists is that the element type of the ArrayList must be specified using arrows (<>).
Java ArrayList containsAll()
The containsAll() method checks if the arraylist contains all elements of the collection.
containsAll() Return Value
- returns true if the arraylist contains all elements of the collection (no order)
- returns false if the arraylist does not contain all elements of the collection.
Let us create 2 ArrayLists.
package com.example;
import java.util.ArrayList;
import java.util.Arrays;
public class ContainsAll_ArrayList {
public static void main(String[] args) {
ArrayList<String> list1 = new ArrayList<String>();
list1.add("Java");
list1.add("Python");
list1.add("PHP");
list1.add("JavaScript");
list1.add("Ruby");
ArrayList<String> list2 = new ArrayList<>(Arrays.asList("Python", "Ruby"));
ArrayList<String> list3 = new ArrayList<>(Arrays.asList("Python", "Java", "Ruby", "PHP", "JavaScript"));
System.out.println("ArrayList1:" + list1);
System.out.println("ArrayList2:" + list2);
// check if ArrayList 1 contains ArrayList 2 (ArrayList 2 is subset of ArrayList 1)
System.out.println("Check arrayList1 containsAll arrayList2 :" + list1.containsAll(list2));
// check if ArrayList 2 contains ArrayList 1 (ArrayList 2 is subset of ArrayList 1)
System.out.println("Check arrayList1 containsAll arrayList2 :" + list2.containsAll(list1));
// check if ArrayList 1 contains ArrayList 3 (different sequence)
System.out.println("Check arrayList1 containsAll arrayList3 :" + list1.containsAll(list3));
}
}
The output of the above program is

In the above example, we have created three arraylists named list1, list2, and list3.
list1.containsAll(list2));
Here, the containsAll() method checks if lis1 contains all elements of list2. Hence, the method returns true.
list2.containsAll(list1));
Here, the containsAll() method checks if list2 contains all elements of list1. Hence, it returns false.
list1.containsAll(list3));
Here, the containsAll() method checks if list1 contains all elements of list3. Hence, it returns true (sequencing does not matter).
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!






