The Wayback Machine - https://web.archive.org/web/20240618020157/https://www.geeksforgeeks.org/set-containsall-method-in-java-with-examples/
Open In App

Set containsAll() method in Java with Examples

Last Updated : 31 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The containsAll() method of Java Set 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 Set.containsAll() method:

Program 1:




// Java code to illustrate
// Set containsAll()
  
import java.util.*;
  
class SetDemo {
    public static void main(String args[])
    {
  
        // Creating an empty set
        Set<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("Set 1: "
                           + set);
  
        // Creating another empty set
        Set<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("Set 2: "
                           + set2);
  
        // Check if the set
        // contains same elements
        System.out.println("\nDoes set 1 contains set 2?: "
                           + set.containsAll(set2));
    }
}


Output:

Set 1: [Geeks, for, 20, 10]
Set 2: [Geeks, for, 20, 10]

Does set 1 contains set 2?: true

Program 2:




// Java code to illustrate boolean containsAll()
  
import java.util.*;
  
class SetDemo {
    public static void main(String args[])
    {
  
        // Creating an empty set
        Set<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("Set 1: "
                           + set);
  
        // Creating another empty set
        Set<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("Set 2: "
                           + set2);
  
        // Check if the set
        // contains same elements
        System.out.println("\nDoes set 1 contains set 2: "
                           + set.containsAll(set2));
    }
}


Output:

Set 1: [Geeks, for]
Set 2: [20, 10]

Does set 1 contains set 2: false

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#containsAll(java.util.Collection)



Previous Article
Next Article

Similar Reads

AbstractCollection containsAll() Method in Java with Examples
The containsAll() method of Java AbstractCollection is used to check whether two Collections contain the same elements or not. It takes one collection as a parameter and returns True if all of the elements of this collection is present in the other collection. Syntax: AbstractCollection.containsAll(Collection C) Parameters: The parameter C is a Col
2 min read
List containsAll() method in Java with Examples
The containsAll() method of List interface in Java is used to check if this List contains all of the elements in the specified Collection. So basically it is used to check if a List contains a set of elements or not. Syntax: boolean containsAll(Collection col) Parameters: This method accepts a mandatory parameter col which is of the type of collect
2 min read
SortedSet containsAll() method in Java with Examples
The containsAll() method of Java SortedSet 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
2 min read
Java Collection containsAll() Method with Examples
The Collection interface in Java is the fundamental part of the Collection hierarchy. It is found in the java.util package and offers a wide range of built-in classes and methods. One such useful method is containsAll(). Java containsAll() methodThe purpose of this containsAll() is to check whether a collection contains all the elements present in
3 min read
Vector containsAll() Method in Java
The java.util.vector.containsAll() method is used to check to check if this Vector contains all of the elements in the specified Collection. So basically it is used to check if a vector contains a set of elements or not. Syntax: Vector.containsAll(Collection col) Parameters: This method accepts a mandatory parameter col which is of the type of vect
2 min read
CopyOnWriteArrayList containsAll() method in Java
The containsAll() method of CopyOnWriteArrayList class in Java is used to check if this List contains all of the elements in the specified Collection. So basically it is used to check if a List contains a set of elements or not. Syntax: boolean containsAll(Collection col) Parameters: This method accepts a mandatory parameter col which is of the typ
3 min read
Stack containsAll() method in Java with Example
The containsAll() method of Java Stack is used to check whether two stacks contain the same elements or not. It takes one stack as a parameter and returns True if all of the elements of this stack is present in the other stack. Syntax: public boolean containsAll(Collection C) Parameters: The parameter C is a Collection. This parameter refers to the
2 min read
LinkedHashSet containsAll() method in Java with Example
The containsAll() method of Java LinkedHashSet 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
2 min read
HashSet containsAll() method in Java with Example
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 w
2 min read
AbstractSet containsAll() method in Java with Example
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 s
2 min read