
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a Java HashSet Collection contains another Collection
HashSet is a class in Java that implements the Set interface. It is used for storing unique elements. Our task is to check if a HashSet contains another collection in Java. Let's look at some scenarios to understand the problem better:
Scenario 1
Input: {1, 2, 3, 4, 5} Collection to check: {2, 3} Output: true Explanation: The HashSet contains all the elements of the collection {2, 3}.
Scenario 2
Input: {1, 2, 3, 4, 5} Collection to check: {6, 7} Output: false Explanation: The HashSet does not contain all the elements of the collection {6, 7}.
Checking if HashSet Contains another Collection
The following are the ways we can check if a HashSet contains another collection in Java:
Using containsAll() Method
The containsAll() method is a method of the Collection interface, which is implemented by the HashSet class. This method accepts a collection object as a parameter and verifies whether the given object exists in the current HashSet. If it does, this method returns TRUE; otherwise, it returns FALSE.
Syntax
Following is the syntax of the containsAll() method:
containsAll(collection);
Example
In the below example, we will create a HashSet and an ArrayList, and then we will check if the HashSet contains all the elements of an ArrayList using the containsAll() method:
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class CheckIfContainsCollection { public static void main(String[] args){ Set<Integer> hashSet = new HashSet<>(); hashSet.add(1); hashSet.add(2); hashSet.add(3); hashSet.add(4); hashSet.add(5); List<Integer> collectionToCheck = new ArrayList<>(); collectionToCheck.add(2); collectionToCheck.add(3); if(hashSet.containsAll(collectionToCheck)) { System.out.println("HashSet contains all elements of the collection."); } else { System.out.println("HashSet does not contain all elements of the collection."); } } }
When you run the above code, it will produce the following output:
HashSet contains all elements of the collection.
Using Streams API
Java Stream API was introduced in Java8. It provides methods that help us to process collections. We can use the allMatch() method of the Stream interface to check if a HashSet contains all the elements of another collection.
Example
In the below example, we will create a HashSet, next we will convert the HashSet to a Stream using the stream() method, and then we will use the allMatch() method to check if all elements of the collection are present in the HashSet:
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Stream; public class CheckIfContainsCollection { public static void main(String[] args){ Set<Integer> hashSet = new HashSet<>(); hashSet.add(1); hashSet.add(2); hashSet.add(3); hashSet.add(4); hashSet.add(5); List<Integer> collectionToCheck = new ArrayList<>(); collectionToCheck.add(2); collectionToCheck.add(3); boolean exists = collectionToCheck.stream().allMatch(hashSet::contains); if(exists) { System.out.println("HashSet contains all elements of the collection."); } else { System.out.println("HashSet does not contain all elements of the collection."); } } }
When you run the above code, it will produce the following output:
HashSet contains all elements of the collection.
In this article, we have learned how to check if a HashSet contains another collection in Java using the containsAll() method and the Streams API.