YouTip LogoYouTip

Java Arraylist Containsall

[![Image 1: Java ArrayList](#) Java ArrayList](#) The `containsAll()` method is used to check if an ArrayList contains all elements from 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 from the specified collection. Throws a `ClassCastException` if an element in the ArrayList is incompatible with an element in the specified collection. Throws a `NullPointerException` if the collection contains a `null` element and the ArrayList does not permit `null` values. **Note:** You can think of it this way: the `containsAll()` method checks 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 sites =new ArrayList(); sites.add("Google"); sites.add(""); sites.add("Taobao"); System.out.println("ArrayList 1: "+ sites); // Create another ArrayList ArrayList sites2 =new ArrayList(); // Add elements to the ArrayList sites2.add(""); 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); } } The output of the above program is: ArrayList 1: [Google, , Taobao]ArrayList 2: [, Google]ArrayList 1 contains all elements of ArrayList 2: trueArrayList 2 contains all elements of ArrayList 1: false In the example above, we created two ArrayLists named `sites` and `sites2`. Notice these lines: // Returns true sites.containsAll(sites2);// Returns false sites2.containsAll(sites) The `containsAll()` method checks if `sites` contains all elements from `sites2`. Since it does, it returns `true`. The `containsAll()` method checks if `sites2` contains all elements from `sites`. Since it does not, it returns `false`. **The `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 numbers =new ArrayList(); // Insert elements into the ArrayList numbers.add(1); numbers.add(2); numbers.add(3); System.out.println("ArrayList: "+ numbers); // Create a HashSet HashSet 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("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); } } The output of the above program is: ArrayList: [1, 2, 3]HashSet: [2, 3]ArrayList contains all elements of HashSet: trueHashSet contains all elements of ArrayList: false In the example above, we created an ArrayList named `numbers` and a HashSet named `primeNumbers`. [![Image 2: Java ArrayList](#) Java ArrayList](#)
← Python3 Att Dictionary UpdateJava Arraylist Foreach β†’