Stack removeAll() method in Java with Example

The Java.util.Stack.removeAll(Collection col) method is used to remove all the elements from the Stack, present in the collection specified.

Syntax:

Stack.removeAll(Collection col)

Parameters: This method accepts a mandatory parameter col which is the collection whose elements are to be removed from the Stack.

Return Value: This method returns true if the Stack is altered due to the operation at all, else False.

Exception: The method throws NullPointerException if the specified collection is null.

Below programs illustrate the Java.util.Stack.removeAll(Collection col) method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate removeAll()
import java.util.*;
  
public 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");
  
        // Output the Stack
        System.out.println("Stack: " + stack);
  
        // Creating an empty Stack
        Stack<String> colstack = new Stack<String>();
  
        // Use add() method to add elements in the Stack
        colstack.add("Geeks");
        colstack.add("for");
        colstack.add("Geeks");
  
        // Remove the head using remove()
        boolean changed = stack.removeAll(colstack);
  
        // Print the result
        if (changed)
            System.out.println("Collection removed");
        else
            System.out.println("Collection not removed");
  
        // Print the final Stack
        System.out.println("Final Stack: " + stack);
    }
}

chevron_right


Output:

Stack: [Geeks, for, Geeks, 10, 20]
Collection removed
Final Stack: [10, 20]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate removeAll()
import java.util.*;
  
public class StackDemo {
    public static void main(String args[])
    {
  
        // Creating an empty Stack
        Stack<Integer> stack = new Stack<Integer>();
  
        // Use add() method to add elements in the Stack
        stack.add(1);
        stack.add(2);
        stack.add(3);
        stack.add(10);
        stack.add(20);
  
        // Output the Stack
        System.out.println("Stack: " + stack);
  
        // Creating an empty Stack
        Stack<Integer> colstack = new Stack<Integer>();
  
        // Use add() method to add elements in the Stack
        colstack.add(30);
        colstack.add(40);
        colstack.add(50);
  
        // Remove the head using remove()
        boolean changed = stack.removeAll(colstack);
  
        // Print the result
        if (changed)
            System.out.println("Collection removed");
        else
            System.out.println("Collection not removed");
  
        // Print the final Stack
        System.out.println("Final Stack: " + stack);
    }
}

chevron_right


Output:

Stack: [1, 2, 3, 10, 20]
Collection not removed
Final Stack: [1, 2, 3, 10, 20]


My Personal Notes arrow_drop_up

Image
Check out this Author's contributed articles.

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.