Friday, October 3, 2025
HomeLanguagesJavaStack containsAll() method in Java with Example

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 stack whose elements occurrence is needed to be checked in this stack.

Return Value: The method returns True if this stack contains all the elements of other stack otherwise it returns False.

Below programs illustrate the Stack.containsAll() method:

Program 1:




// Java code to illustrate
// Stack containsAll()
  
import java.util.*;
  
class StackDemo {
    public static void main(String args[])
    {
  
        // Creating an empty stack
        Stack<String>
            stack = new Stack<String>();
  
        // Use add() method to
        // add elements in the stack
        stack.add("Geeks");
        stack.add("for");
        stack.add("Geeks");
        stack.add("10");
        stack.add("20");
  
        // prints the stack
        System.out.println("Stack 1: "
                           + stack);
  
        // Creating another empty stack
        Stack<String>
            stack2 = new Stack<String>();
  
        // Use add() method to
        // add elements in the stack
        stack2.add("Geeks");
        stack2.add("for");
        stack2.add("Geeks");
        stack2.add("10");
        stack2.add("20");
  
        // prints the stack
        System.out.println("Stack 2: "
                           + stack2);
  
        // Check if the stack
        // contains same elements
        System.out.println("\nDoes stack 1 contains stack 2: "
                           + stack.containsAll(stack2));
    }
}


Output:

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

Does stack 1 contains stack 2: true

Program 2:




// Java code to illustrate boolean containsAll()
  
import java.util.*;
  
class StackDemo {
    public static void main(String args[])
    {
  
        // Creating an empty stack
        Stack<String>
            stack = new Stack<String>();
  
        // Use add() method to
        // add elements in the stack
        stack.add("Geeks");
        stack.add("for");
        stack.add("Geeks");
  
        // prints the stack
        System.out.println("Stack 1: "
                           + stack);
  
        // Creating another empty stack
        Stack<String>
            stack2 = new Stack<String>();
  
        // Use add() method to
        // add elements in the stack
        stack2.add("10");
        stack2.add("20");
  
        // prints the stack
        System.out.println("Stack 2: "
                           + stack2);
  
        // Check if the stack
        // contains same elements
        System.out.println("\nDoes stack 1 contains stack 2: "
                           + stack.containsAll(stack2));
    }
}


Output:

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

Does stack 1 contains stack 2: false
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32331 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11929 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7080 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS