English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Java ArrayList containsAll() method checks whether the ArrayList contains all elements of the specified collection.
The syntax of containsAll() method is:
arraylist.containsAll(Collection c);
collection - Check if all elements of the collection appear in the ArrayList.
If the ArrayList contains all elements of the specified collection, then return true
If the class of the existing elements in the ArrayList is not compatible with the class of the elements in the specified collection, then a ClassCastException is thrown
If the collection contains null elements and the ArrayList does not allow null values, then a NullPointerException is thrown
Note:The containsAll() method checks whether the collection is a subset of the ArrayList.
import java.util.ArrayList; class Main { public static void main(String[] args) { //Create ArrayList ArrayList<String> languages1 = new ArrayList<>(); //Add element to ArrayList languages1.add("JavaScript"); languages1.add("Python"); languages1.add("Java"); System.out.println("ArrayList 1: " + languages1; //Create another ArrayList ArrayList<String> languages2 = new ArrayList<>(); //Add element to ArrayList languages2.add("Java"); languages2.add("Python"); System.out.println("ArrayList 2: " + languages2; // Check the ArrayList 1Whether the ArrayList 2 boolean result1 = languages1.containsAll(languages2; System.out.println("ArrayList 1contains ArrayList 2All elements of : " + result1; // Check the ArrayList 2Whether the ArrayList 1 boolean result2 = languages2.containsAll(languages1; System.out.println("ArrayList 2contains ArrayList 1All elements of : " + result2; } }
Output result
ArrayList 1: [JavaScript, Python, Java] ArrayList 2: [Java, Python] ArrayList 1contains ArrayList 2All elements of : true ArrayList 2contains ArrayList 1elements: false
In the above example, we created two named languages1and languages2of the ArrayList. Note the expression
// Returns true languages1.containsAll(languages2)
Here, the containsAll() method checks languages1whether it contains languages2elements. Therefore, this method returns true. However, please note the following expression:
// Returns false languages2.containsAll(languages1)
Here, the containsAll() method checks languages2whether it contains languages1elements. Therefore, it returns false
Note:The containsAll() method is not specific to the ArrayList class. It inherits from the List interface.
import java.util.ArrayList; import java.util.HashSet; class Main { public static void main(String[] args) { //Create ArrayList ArrayList<Integer> numbers = new ArrayList<>(); //Add element to ArrayList numbers.add(1; numbers.add(2; numbers.add(3; System.out.println("ArrayList: " + numbers); //Create HashSet HashSet<Integer> primeNumbers = new HashSet<>(); //Add element to 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("ArrayList contains all elements of HashSet: " + result1; // Check if HashSet contains all elements of ArrayList boolean result2 = primeNumbers.containsAll(numbers); System.out.println("HashSet contains all elements of ArrayList: " + result2; } }
Output result
ArrayList: [1, 2, 3] HashSet: [2, 3] ArrayList contains all elements of HashSet : true HashSet contains all elements of ArrayList : false
In the above example, we created an ArrayList named numbers and a HashSet named primeNumbers. Note these expressions,
// Check if ArrayList contains HashSet // Returns true numbers.containsAll(primeNumbers) // Check if HashSet contains ArrayList // Returns false primeNumbers.containsAll(numbers)
NoteWe can use Java ArrayList retainAll() MethodGet the common elements between ArrayList and HashSet.