The containsAll() method of Java HashSet is used to check whether two sets contain the same elements or not. It takes one set as a parameter and returns True if all of the elements of this set is present in the other set.
Syntax:
public boolean containsAll(Collection C)
Parameters: The parameter C is a Collection. This parameter refers to the set whose elements occurrence is needed to be checked in this set.
Return Value: The method returns True if this set contains all the elements of other set otherwise it returns False.
Below programs illustrate the HashSet.containsAll() method:
Program 1:
// Java code to illustrate // HashSet containsAll() import java.util.*; class HashSetDemo { public static void main(String args[]) { // Creating an empty set HashSet<String> set = new HashSet<String>(); // Use add() method to // add elements in the set set.add("Geeks"); set.add("for"); set.add("Geeks"); set.add("10"); set.add("20"); // prints the set System.out.println("HashSet 1: " + set); // Creating another empty set HashSet<String> set2 = new HashSet<String>(); // Use add() method to // add elements in the set set2.add("Geeks"); set2.add("for"); set2.add("Geeks"); set2.add("10"); set2.add("20"); // prints the set System.out.println("HashSet 2: " + set2); // Check if the set // contains same elements System.out.println("\nDoes set 1 contains set 2: " + set.containsAll(set2)); } } |
HashSet 1: [Geeks, for, 20, 10] HashSet 2: [Geeks, for, 20, 10] Does set 1 contains set 2: true
Program 2:
// Java code to illustrate boolean containsAll() import java.util.*; class HashSetDemo { public static void main(String args[]) { // Creating an empty set HashSet<String> set = new HashSet<String>(); // Use add() method to // add elements in the set set.add("Geeks"); set.add("for"); set.add("Geeks"); // prints the set System.out.println("HashSet 1: " + set); // Creating another empty set HashSet<String> set2 = new HashSet<String>(); // Use add() method to // add elements in the set set2.add("10"); set2.add("20"); // prints the set System.out.println("HashSet 2: " + set2); // Check if the set // contains same elements System.out.println("\nDoes set 1 contains set 2: " + set.containsAll(set2)); } } |
HashSet 1: [Geeks, for] HashSet 2: [20, 10] Does set 1 contains set 2: false
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- LinkedHashSet containsAll() method in Java with Example
- Stack containsAll() method in Java with Example
- CopyOnWriteArraySet containsAll() method in Java with Example
- Vector containsAll() Method in Java
- Set containsAll() method in Java with Examples
- CopyOnWriteArrayList containsAll() method in Java
- TreeSet containsAll() method in Java with Example
- AbstractSet containsAll() method in Java with Example
- SortedSet containsAll() method in Java with Examples
- List containsAll() method in Java with Examples
- AbstractCollection containsAll() Method in Java with Examples
- HashSet add() Method in Java
- HashSet contains() Method in Java
- HashSet hashCode() method in Java with Example
- HashSet equals() method in Java with Example
- HashSet toArray(T[]) method in Java with Example
- HashSet toArray() method in Java with Example
- HashSet removeAll() method in Java with Example
- HashSet retainAll() method in Java with Example
- HashSet clear() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

