Stack retainAll() method in Java with Example Last Updated : 24 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report The retainAll() method of java.util.Stack class is used to retain from this stack all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from this stack. Returns Value: This method returns true if this stack changed as a result of the call. Exception: This method throws NullPointerException if this stack contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null. Below are the examples to illustrate the retainAll() method. Example 1: Java // Java program to demonstrate // retainAll() method for Integer value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of Stack<Integer> Stack<Integer> stack1 = new Stack<Integer>(); // Populating stack1 stack1.add(1); stack1.add(2); stack1.add(3); stack1.add(4); stack1.add(5); // print stack1 System.out.println("Stack before " + "retainAll() operation : " + stack1); // Creating another object of Stack<Integer> Stack<Integer> stack2 = new Stack<Integer>(); stack2.add(1); stack2.add(2); stack2.add(3); // print stack2 System.out.println("Collection Elements" + " to be retained : " + stack2); // Removing elements from stack // specified in stack2 // using retainAll() method stack1.retainAll(stack2); // print stack1 System.out.println("Stack after " + "retainAll() operation : " + stack1); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } Output: Stack before retainAll() operation : [1, 2, 3, 4, 5] Collection Elements to be retained : [1, 2, 3] Stack after retainAll() operation : [1, 2, 3] Example 2: For NullPointerException Java // Java program to demonstrate // retainAll() method for Integer value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of Stack<Integer> Stack<Integer> stack1 = new Stack<Integer>(); // Populating stack1 stack1.add(1); stack1.add(2); stack1.add(3); stack1.add(4); stack1.add(5); // print stack1 System.out.println("Stack before " + "retainAll() operation : " + stack1); // Creating another object of Stack<Integer> Stack<Integer> stack2 = null; // print stack2 System.out.println("Collection Elements" + " to be retained : " + stack2); System.out.println("\nTrying to pass " + "null as a specified element\n"); // Removing elements from stack // specified in stack2 // using retainAll() method stack1.retainAll(stack2); // print stack1 System.out.println("Stack after " + "retainAll() operation : " + stack1); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } Output: Stack before retainAll() operation : [1, 2, 3, 4, 5] Collection Elements to be retained : null Trying to pass null as a specified element Exception thrown : java.lang.NullPointerException Comment More infoAdvertise with us Next Article Stack remove(Object) 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 removeElementAt() method in Java with Example The Java.util.Stack.removeElementAt(int index) method is used to remove an element from a Stack from a specific position or index. In this process the size of the Stack is automatically reduced by one and all other elements after the removed element are shifted downwards by one position. Syntax: Sta 2 min read Stack remove(int) method in Java with Example The Java.util.Stack.remove(int index) method is used to remove an element from a Stack from a specific position or index. Syntax: Stack.remove(int index) Parameters: This method accepts a mandatory parameter index is of integer data type and specifies the position of the element to be removed from t 2 min read 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 Like