The containsAll()
method in Java's ArrayList
class is designed to determine if one list contains all elements of another list. This method is particularly useful when you need to verify if a subset of elements is entirely present within a larger collection, making it a common utility in collection manipulation and verification tasks.
In this article, you will learn how to utilize the containsAll()
method effectively. Explore its application through practical examples and understand how to integrate it into Java programs to check for the presence of all elements from one list within another.
Create the main ArrayList
and a sublist whose presence you want to verify.
Use the containsAll()
method to check if all elements of the sublist are present in the main list.
import java.util.ArrayList;
import java.util.Arrays;
ArrayList<Integer> mainList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
ArrayList<Integer> subList = new ArrayList<>(Arrays.asList(2, 3));
boolean result = mainList.containsAll(subList);
System.out.println("All elements found: " + result);
This code initializes mainList
with elements and subList
with a subset of the same elements. Using containsAll()
, it checks whether all elements of subList
are present in mainList
. The output will be true
since elements 2 and 3 exist in the mainList
.
Understand that containsAll()
can be used with ArrayLists
of any data type.
Create ArrayLists
of a non-integer type, such as String
, and perform the check.
ArrayList<String> mainList = new ArrayList<>(Arrays.asList("apple", "banana", "cherry"));
ArrayList<String> subList = new ArrayList<>(Arrays.asList("banana", "cherry"));
boolean result = mainList.containsAll(subList);
System.out.println("All elements found: " + result);
Here, both mainList
and subList
contain strings. The method confirms that both "banana" and "cherry" from subList
are found in mainList
, resulting in a true
output.
Use containsAll()
to filter data based on multiple criteria.
Suppose a scenario where you filter records based on the presence of specified elements.
ArrayList<String> data = new ArrayList<>(Arrays.asList("red", "blue", "green", "yellow"));
ArrayList<String> filterCriteria = new ArrayList<>(Arrays.asList("blue", "green"));
boolean isDataValid = data.containsAll(filterCriteria);
System.out.println("Data meets criteria: " + isDataValid);
In this example, data
represents a dataset and filterCriteria
represents the necessary conditions. The output true
indicates that all required criteria are present in the data.
Implement containsAll()
for integrity checks, ensuring all required fields or items are present in a collection.
Use a checklist scenario where all necessary items must be checked off.
ArrayList<String> checklist = new ArrayList<>(Arrays.asList("passport", "tickets", "visa"));
ArrayList<String> itemsPacked = new ArrayList<>(Arrays.asList("passport", "visa", "tickets", "sunglasses"));
boolean isReadyToTravel = itemsPacked.containsAll(checklist);
System.out.println("Ready to travel: " + isReadyToTravel);
This example uses containsAll()
to verify that all necessary travel items (passport, tickets, visa) are packed. The method returns true
, indicating readiness for travel.
The containsAll()
method in Java's ArrayList
class is a versatile tool for verifying the presence of all elements from one collection within another. By integrating this function into your Java applications, you enhance your ability to perform comprehensive checks and data validations efficiently. Whether for simple presence checks or complex data integrity validations, understanding how to use containsAll()
effectively empowers you to handle collections more proficiently.