Stack removeAll() method in Java with Example Last Updated : 24 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Java // 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); } } Output: Stack: [Geeks, for, Geeks, 10, 20] Collection removed Final Stack: [10, 20] Program 2: Java // 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); } } Output: Stack: [1, 2, 3, 10, 20] Collection not removed Final Stack: [1, 2, 3, 10, 20] Comment More infoAdvertise with us Next Article Stack listIterator(int) method in Java with Example C code_r Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-Stack +1 More Practice Tags : JavaJava-Collections Similar Reads Stack removeAllElements() method in Java with Example The Java.util.Stack.removeAllElements() method is used to removes all components from this Stack and sets its size to zero. Syntax: Stack.removeAllElements() Parameters: The method does not take any parameter Return Value: The function does not returns any value. Below programs illustrate the Java.u 2 min read Stack remove(Object) method in Java with Example The Java.util.Stack.remove(Object o) method is used to remove any particular element from the Stack. Syntax: Stack.remove(Object o) Parameters: This method accepts a mandatory parameter o is of the object type of Stack and specifies the element to be removed from the Stack. Return Value: Returns Tru 2 min read Stack addAll(int, Collection) method in Java with Example The addAll(int, Collection) method of Stack Class is used to append all of the elements from the collection passed as a parameter to this function at a specific index or position of a Stack. Syntax: boolean addAll(int index, Collection C) Parameters: This function accepts two parameters as shown in 2 min read Stack listIterator() method in Java with Example The listIterator() method of Java.util.Stack class is used to return a list iterator over the elements in this stack (in proper sequence). The returned list iterator is fail-fast. Syntax: public ListIterator listIterator() Return Value: This method returns a list iterator over the elements in this s 2 min read Stack listIterator(int) method in Java with Example The listIterator(int) method of Stack Class is used to return a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to pre 2 min read Stack trimToSize() method in Java with Example The trimToSize() method of Stack in Java trims the capacity of an Stack instance to be the list's current capacity. This method is used to trim an Stack instance to the number of elements it contains. Syntax: public void trimToSize() Parameter: It does not accepts any parameter. Return Value: It doe 2 min read Stack lastIndexOf(Object, int) method in Java with Example The Java.util.Stack.lastIndexOf(Object element, int last_index) method is used to the last index of the first occurrence of the specified element in this Stack, searching forwards from last index, or returns -1 if the element is not found. More formally, returns the lowest last index i such that (i 3 min read Stack toString() method in Java with Example The toString() method of Java Stack is used to return a string representation of the elements of the Collection. The String representation comprises a set representation of the elements of the Collection in the order they are picked by the iterator closed in square brackets[].This method is used mai 2 min read Stack size() method in Java with Example The Java.util.Stack.size() method in Java is used to get the size of the Stack or the number of elements present in the Stack. Syntax: Stack.size() Parameters: The method does not take any parameter. Return Value: The method returns the size or the number of elements present in the Stack. Below prog 2 min read Stack capacity() method in Java with Example The capacity() method of Stack Class is used to get the capacity of this Stack. That is the number of elements present in this stack container. Syntax: public int capacity() Parameters: This function accepts a parameter E obj which is the object to be added at the end of the Stack. Return Value: The 2 min read Like