Java ArrayList containsAll() Method

Java ArrayList Java ArrayList

The containsAll() method is used to check whether the arraylist contains all elements of the specified collection.

The syntax of 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 dynamic array contains all elements of the collection.

Throws ClassCastException if the elements present in the arraylist are incompatible with the elements in the specified collection.

Throws NullPointerException if the collection contains null elements and the arraylist does not allow null values.

Note:It can be understood this way: the containsAll() method checks whether the collection is a subset of the dynamic array.

Example

The following example demonstrates how to use containsAll:

Example

import java.util.ArrayList;
class Main {
    public static void main(String[] args){

        // Create a dynamic array
        ArrayList<String> sites = new ArrayList<>();
       
        sites.add("Google");
        sites.add("Example");
        sites.add("Taobao");

        System.out.println("ArrayList 1: " + sites);

        // Create another dynamic array
        ArrayList<String> sites2 = new ArrayList<>();

        // Add elements to the dynamic array
        sites2.add("Example");
        sites2.add("Google");
        System.out.println("ArrayList 2: " + sites2);

        // Check whether dynamic array 1 contains dynamic array 2
        boolean result1 = sites.containsAll(sites2);
        System.out.println("ArrayList 1 contains all elements of ArrayList 2: " + result1);

        // Check whether array 2 contains array 1
        boolean result2 = sites2.containsAll(sites);
        System.out.println("ArrayList 2 contains all elements of ArrayList 1: " + result2);
    }
}

The output of the above program is:

ArrayList 1: [Google, Example, Taobao]
ArrayList 2: [Example, Google]
ArrayList 1 包含了 ArrayList 2 的所有元素: true
ArrayList 2 包含了 ArrayList 1 的所有元素: false

In the above example, we created two dynamic arrays named sites and sites2.

Note these lines:

// 返回 true
sites.containsAll(sites2);

// 返回 false
sites2.containsAll(sites)

The containsAll() method checks whether sites contains all elements of sites2. Since the elements are contained, it returns true.

The containsAll() method checks whether sites2 contains all elements of sites. Since the elements are not contained, it returns 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 array
        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 whether the ArrayList contains all elements of the HashSet
        boolean result1 = numbers.containsAll(primeNumbers);
        System.out.println("Whether ArrayList contains all elements of HashSet: " + result1);

        // Check whether the HashSet contains all elements of the ArrayList
        boolean result2 = primeNumbers.containsAll(numbers);
        System.out.println("Whether HashSet contains all elements of ArrayList: " + result2);
    }
}

The output of the above program is:

ArrayList: [1, 2, 3]
HashSet: [2, 3]
ArrayList 是否 HashSet 中的所有元素: true
HashSet 是否 ArrayList 中的所有元素: false

In the above example, we created an ArrayList named numbers and a HashSet named primeNumbers.

Java ArrayList Java ArrayList

Other Extensions