In the previous article we have discussed about Java ArrayList contains() Method with Example
In this article we are going to see the use Java ArrayList containsAll() method along with suitable examples.
Java ArrayList containsAll() Method with Example
containsAll():
This java.util.ArrayList.containsAll()
method is used to checks if all the elements of a specified collection is present in the arraylist or not.
It returns true if all the elements are present else it returns false.
Syntax:
arrayListName.containsAll(Collection Col)
Where,
arrayListName
refers to the name of your ArrayList.Collection col
refers to specified collection, whose elements will be checked in another arraylist.
Let’s see different examples to understand it more clearly.
Method-1: Java ArrayList containsAll() Method – Example with String Type ArrayList
Approach:
- Create a new ArrayList of type String.
- Add string elements into the ArrayList using the
add()
method. - Display the ArrayList elements.
- Now, using the
containsAll()
method we can check if all the element are present or not in the arrayList. - Print the result as true/false.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create an ArrayList of string datatype ArrayList<String> arr = new ArrayList<String>(); // Adding some elements to the ArrayList arr.add("vivo"); arr.add("samsung"); arr.add("nokia"); // Prints the ArrayList elements System.out.println("The elements of ArrayList 1 are: "+arr); // Create a ArrayList of string datatype ArrayList<String> arr1 = new ArrayList<String>(); // Adding some elements to the ArrayList arr1.add("vivo"); arr1.add("htc"); arr1.add("samsung"); arr1.add("realme"); arr1.add("nokia"); // Prints the ArrayList elements System.out.println("The elements of ArrayList 2 are: "+arr1); // check if it contains all the elements or not System.out.println("are all the elements of ArrayList 1 present in Arraylist 2: " +arr1.containsAll(arr)); System.out.println("are all the elements of ArrayList 2 present in Arraylist 1: " +arr.containsAll(arr1)); } }
Output: The elements of ArrayList 1 are: [vivo, samsung, nokia] The elements of ArrayList 2 are: [vivo, htc, samsung, realme, nokia] are all the elements of ArrayList 1 present in Arraylist 2: true are all the elements of ArrayList 2 present in Arraylist 1: false
Method-2: Java ArrayList containsAll() Method – Example with Integer Type ArrayList
Approach:
- Create a new ArrayList of type Integer.
- Add Integer elements into the ArrayList using the add() method.
- Display the ArrayList elements.
- Now, using the
containsAll()
method we can check if all the element are present or not in the arrayList. - Print the result as true/false.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create an ArrayList of Integer datatype ArrayList<Integer> arr = new ArrayList<Integer>(); // Adding some elements to the ArrayList arr.add(17); arr.add(1); // Prints the ArrayList elements System.out.println("The elements of ArrayList 1 are: "+arr); // Create a ArrayList of string datatype ArrayList<Integer> arr1 = new ArrayList<Integer>(); // Adding some elements to the ArrayList arr1.add(1); arr1.add(100); arr1.add(84); arr1.add(17); arr1.add(0); // Prints the ArrayList elements System.out.println("The elements of ArrayList 2 are: "+arr1); // check if it contains all the elements or not System.out.println("are all the elements of ArrayList 1 present in Arraylist 2: " +arr1.containsAll(arr)); System.out.println("are all the elements of ArrayList 2 present in Arraylist 1: " +arr.containsAll(arr1)); } }
Output: The elements of ArrayList 1 are: [17, 1] The elements of ArrayList 2 are: [1, 100, 84, 17, 0] are all the elements of ArrayList 1 present in Arraylist 2: true are all the elements of ArrayList 2 present in Arraylist 1: false
Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.
Related Java Programs: