Java ArrayList containsAll() Method
The containsAll() method is used to check if an arraylist contains all elements of a specified collection.
The syntax for the containsAll() method is:
arraylist.containsAll(Collection c);
Note: arraylist is an object of the ArrayList class.
Parameter Description:
- collection - the collection parameter.
Return Value
Returns true if the arraylist contains all elements of the collection.
Throws ClassCastException if the types of one or more elements in the specified collection are incompatible with the arraylist.
Throws NullPointerException if the collection contains one or more null elements and the arraylist does not permit null values.
Note: You can think of the containsAll() method as checking if the collection is a subset of the arraylist.
Example
The following example demonstrates the usage of containsAll:
Example
import java.util.ArrayList;
class Main {
public static void main(String[] args){
// Create an arraylist
ArrayList<String> sites = new ArrayList<>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
System.out.println("ArrayList 1: " + sites);
// Create another arraylist
ArrayList<String> sites2 = new ArrayList<>();
// Add elements to the arraylist
sites2.add("tutorialpro");
sites2.add("Google");
System.out.println("ArrayList 2: " + sites2);
// Check if ArrayList 1 contains all elements of ArrayList 2
boolean result1 = sites.containsAll(sites2);
System.out.println("ArrayList 1 contains all elements of ArrayList 2: " + result1);
// Check if ArrayList 2 contains all elements of ArrayList 1
boolean result2 = sites2.containsAll(sites);
System.out.println("ArrayList 2 contains all elements of ArrayList 1: " + result2);
}
}
Executing the above program outputs:
ArrayList 1: [Google, tutorialpro, Taobao]
ArrayList 2: [tutorialpro, Google]
ArrayList 1 contains all elements of ArrayList 2: true
ArrayList 2 contains all elements of ArrayList 1: false
In the above example, we created two arraylists named sites and sites2.
Note these lines:
// Returns true
sites.containsAll(sites2);
// Returns false
sites2.containsAll(sites)
The containsAll() method checks if sites contains all elements of sites2, which it does, returning true.
The containsAll() method checks if sites2 contains all elements of sites, which it does not, returning false.
containsAll() Method Between Java ArrayList and HashSet
Example
import java.util.ArrayList;
import java.util.HashSet;
class Main {
public static void main(String[] args) {
// Create an arraylist
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Google");
arrayList.add("tutorialpro");
arrayList.add("Taobao");
// Create a HashSet
HashSet<String> hashSet = new HashSet<>();
hashSet.add("tutorialpro");
hashSet.add("Google");
// Check if the arraylist contains all elements of the HashSet
boolean result = arrayList.containsAll(hashSet);
System.out.println("ArrayList contains all elements of HashSet: " + result);
}
}
ArrayList<Integer> numbers = new ArrayList<>();
// Insert elements into the array
numbers.add(1);
numbers.add(2);
numbers.add(3);
System.out.println("ArrayList: " + numbers);
// Create a HashSet
HashSet<Integer> primeNumbers = new HashSet<>();
// Add elements to the HashSet
primeNumbers.add(2);
primeNumbers.add(3);
System.out.println("HashSet: " + primeNumbers);
// Check if ArrayList contains all elements of HashSet
boolean result1 = numbers.containsAll(primeNumbers);
System.out.println("Does ArrayList contain all elements of HashSet: " + result1);
// Check if HashSet contains all elements of ArrayList
boolean result2 = primeNumbers.containsAll(numbers);
System.out.println("Does HashSet contain all elements of ArrayList: " + result2);
}
}
Executing the above program outputs:
ArrayList: [1, 2, 3]
HashSet: [2, 3]
Does ArrayList contain all elements of HashSet: true
Does HashSet contain all elements of ArrayList: false
In the above example, we created an ArrayList named numbers and a HashSet named primeNumbers.