The HashSet.containsAll()
method in Java is used to check if the HashSet
contains all elements from a specified collection. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality.
Table of Contents
- Introduction
containsAll
Method Syntax- Examples
- Checking if HashSet Contains All Elements from Another Collection
- Checking if HashSet Contains All Elements from a List
- Conclusion
Introduction
The HashSet.containsAll()
method is a member of the HashSet
class in Java. It allows you to check if the HashSet
contains all elements from a specified collection. This is useful for verifying the presence of multiple elements in the set at once.
containsAll Method Syntax
The syntax for the containsAll
method is as follows:
public boolean containsAll(Collection<?> c)
- The method takes a single parameter
c
of typeCollection<?>
, which specifies the collection whose elements are to be checked for containment in theHashSet
. - The method returns a boolean value:
true
if theHashSet
contains all elements of the specified collection.false
if theHashSet
does not contain all elements of the specified collection.
Examples
Checking if HashSet Contains All Elements from Another Collection
The containsAll
method can be used to check if a HashSet
contains all elements from another HashSet
.
Example
import java.util.HashSet;
public class ContainsAllExample {
public static void main(String[] args) {
// Creating two HashSets of Strings
HashSet<String> languages1 = new HashSet<>();
HashSet<String> languages2 = new HashSet<>();
// Adding elements to the first HashSet
languages1.add("Java");
languages1.add("Python");
languages1.add("C");
// Adding elements to the second HashSet
languages2.add("Java");
languages2.add("Python");
// Checking if languages1 contains all elements of languages2
boolean containsAll = languages1.containsAll(languages2);
// Printing the result
System.out.println("Does languages1 contain all elements of languages2? " + containsAll);
}
}
Output:
Does languages1 contain all elements of languages2? true
Checking if HashSet Contains All Elements from a List
The containsAll
method can also be used to check if a HashSet
contains all elements from a List
.
Example
import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;
public class ContainsAllFromListExample {
public static void main(String[] args) {
// Creating a HashSet of Strings
HashSet<String> languages = new HashSet<>();
// Adding elements to the HashSet
languages.add("Java");
languages.add("Python");
languages.add("C");
// Creating a List of Strings
List<String> moreLanguages = new ArrayList<>();
moreLanguages.add("Java");
moreLanguages.add("Python");
// Checking if the HashSet contains all elements of the List
boolean containsAll = languages.containsAll(moreLanguages);
// Printing the result
System.out.println("Does the HashSet contain all elements of the List? " + containsAll);
}
}
Output:
Does the HashSet contain all elements of the List? true
Example with Missing Elements
This example shows what happens if the HashSet
does not contain all elements of the specified collection.
Example
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
public class ContainsAllMissingExample {
public static void main(String[] args) {
// Creating a HashSet of Strings
HashSet<String> languages = new HashSet<>();
// Adding elements to the HashSet
languages.add("Java");
languages.add("Python");
languages.add("C");
// Creating a List of Strings with an element not present in the HashSet
List<String> moreLanguages = new ArrayList<>();
moreLanguages.add("Java");
moreLanguages.add("Ruby"); // Not present in the HashSet
// Checking if the HashSet contains all elements of the List
boolean containsAll = languages.containsAll(moreLanguages);
// Printing the result
System.out.println("Does the HashSet contain all elements of the List? " + containsAll);
}
}
Output:
Does the HashSet contain all elements of the List? false
Conclusion
The HashSet.containsAll()
method in Java provides a way to check if a HashSet
contains all elements from a specified collection. By understanding how to use this method, you can efficiently verify the presence of multiple elements in your collections. This method is particularly useful for ensuring that a set contains all necessary elements from another collection, such as another HashSet
or a List
.