The containsAll() method of Java AbstractSet 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 AbstractSet.conatinsAll() method:
Program 1:
// Java code to illustrate // AbstractSet containsAll() import java.util.*; class AbstractSetDemo { public static void main(String args[]) { // Creating an empty set AbstractSet<String> abs = new TreeSet<String>(); // Use add() method to // add elements in the set abs.add("Geeks"); abs.add("for"); abs.add("Geeks"); abs.add("10"); abs.add("20"); // prints the set System.out.println("AbstractSet 1: " + abs); // Creating another empty set AbstractSet<String> abs2 = new TreeSet<String>(); // Use add() method to // add elements in the set abs2.add("Geeks"); abs2.add("for"); abs2.add("Geeks"); abs2.add("10"); abs2.add("20"); // prints the set System.out.println("AbstractSet 2: " + abs2); // Check if the set // contains same elements System.out.println("\nDoes set 1 contains set 2: " + abs.containsAll(abs2)); } } |
AbstractSet 1: [10, 20, Geeks, for] AbstractSet 2: [10, 20, Geeks, for] Does set 1 contains set 2: true
Program 2:
// Java code to illustrate boolean containsAll() import java.util.*; class AbstractSetDemo { public static void main(String args[]) { // Creating an empty set AbstractSet<String> abs = new TreeSet<String>(); // Use add() method to // add elements in the set abs.add("Geeks"); abs.add("for"); abs.add("Geeks"); // prints the set System.out.println("AbstractSet 1: " + abs); // Creating another empty set AbstractSet<String> abs2 = new TreeSet<String>(); // Use add() method to // add elements in the set abs2.add("10"); abs2.add("20"); // prints the set System.out.println("AbstractSet 2: " + abs2); // Check if the set // contains same elements System.out.println("\nDoes set 1 contains set 2: " + abs.containsAll(abs2)); } } |
AbstractSet 1: [Geeks, for] AbstractSet 2: [10, 20] 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:
- AbstractSet add() method in Java with Example
- AbstractSet contains() method in Java with Example
- HashSet containsAll() method in Java with Example
- TreeSet containsAll() method in Java with Example
- LinkedHashSet containsAll() method in Java with Example
- CopyOnWriteArrayList containsAll() method in Java
- CopyOnWriteArraySet containsAll() method in Java with Example
- Stack containsAll() method in Java with Example
- Set containsAll() method in Java with Examples
- Vector containsAll() Method in Java
- AbstractSet toArray() method in Java with Example
- AbstractSet clear() method in Java with Example
- AbstractSet size() method in Java with Example
- AbstractSet retainAll() method in Java with Example
- AbstractSet isEmpty() method in Java with Example
- AbstractSet toString() method in Java with Example
- AbstractSet toArray(T[]) method in Java with Example
- List containsAll() method in Java with Examples
- AbstractCollection containsAll() Method in Java with Examples
- SortedSet containsAll() method in Java with Examples
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.

