Stack clear() method in Java with Example Last Updated : 24 Dec, 2018 Comments Improve Suggest changes Like Article Like Report The Java.util.Stack.clear() method is used to remove all the elements from a Stack. Using the clear() method only clears all the element from the Stack and does not delete the Stack. In other words, we can say that the clear() method is used to only empty an existing Stack. Syntax: Stack.clear() Parameters: The method does not take any parameter Return Value: The function does not returns any value. Below programs illustrate the Java.util.Stack.clear() method. Example 1: Java // Java code to illustrate clear() import java.util.*; public class GFG { public static void main(String args[]) { // Creating an empty Stack Stack<String> stack = new Stack<String>(); // Use add() method to add elements into the Stack stack.add("Welcome"); stack.add("To"); stack.add("Geeks"); stack.add("4"); stack.add("Geeks"); // Displaying the Stack System.out.println("Stack: " + stack); // Clearing the Stack using clear() method stack.clear(); // Displaying the final Stack after clearing; System.out.println("The final Stack: " + stack); } } Output: Stack: [Welcome, To, Geeks, 4, Geeks] The final Stack: [] Example 2: Java // Java code to illustrate clear() import java.util.*; public class GFG { public static void main(String args[]) { // Creating an empty Stack Stack<Integer> stack = new Stack<Integer>(); // Use add() method to add elements into the Queue stack.add(10); stack.add(15); stack.add(30); stack.add(20); stack.add(5); // Displaying the Stack System.out.println("Stack: " + stack); // Clearing the Stack using clear() method stack.clear(); // Displaying the final Stack after clearing; System.out.println("The final Stack: " + stack); } } Output: Stack: [10, 15, 30, 20, 5] The final Stack: [] Comment More infoAdvertise with us Next Article Stack clear() 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 hashCode() method in Java with Example The Java.util.Stack.hashCode() method in Java is used to get the hashcode value of this Stack. Syntax: Stack.hashCode() Parameters: The method does not take any parameter. Return Value: The method returns hash code value of this Stack which is of Integer type. Below programs illustrate the Java.util 2 min read 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 2 min read Stack lastIndexOf() method in Java with Example The Java.util.Stack.lastIndexOf(Object element) method is used to check and find the occurrence of a particular element in the Stack. If the element is present in the Stack then the lastIndexOf() method returns the index of last occurrence of the element otherwise it returns -1. This method is used 2 min read Stack firstElement() method in Java with Example The Java.util.Stack.firstElement() method in Java is used to retrieve or fetch the first element of the Stack. It returns the element present at the 0th index of the Stack Syntax: Stack.firstElement() Parameters: The method does not take any parameter. Return Value: The method returns the first elem 2 min read Stack lastElement() method in Java with Example The Java.util.Stack.lastElement() method in Java is used to retrieve or fetch the last element of the Stack. It returns the element present at the last index of the Stack. Syntax: Stack.lastElement() Parameters: The method does not take any parameter. Return Value: The method returns the last elemen 2 min read Stack ensureCapacity() method in Java with Example The ensureCapacity() method of Java.util.Stack class increases the capacity of this Stack instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. Syntax: public void ensureCapacity(int minCapacity) Parameters: This method takes t 2 min read Stack elements() method in Java with Example The Java.util.Stack.elements() method of Stack class in Java is used to get the enumeration of the values present in the Stack. Syntax: Enumeration enu = Stack.elements() Parameters: The method does not take any parameters. Return value: The method returns an enumeration of the values of the Stack. 2 min read Stack clone() method in Java with Example The clone() method of Stack class is used to return a shallow copy of this Stack. It just creates a copy of the Stack. The copy will have a reference to a clone of the internal data array but not a reference to the original internal data array. Syntax: Stack.clone()Parameters: The method does not ta 2 min read Stack iterator() method in Java with Example The Java.util.Stack.iterator() method is used to return an iterator of the same elements as that of the Stack. The elements are returned in random order from what was present in the stack. Syntax: Iterator iterate_value = Stack.iterator(); Parameters: The function does not take any parameter. Return 2 min read Stack copyInto() method in Java with Example The java.util.Stack.copyInto() method is used to copy all of the components from this Stack to another Stack, having enough space to hold all of the components of the Stack. It is to be noted that the index of the elements remains unchanged. The elements present in the Stack are replaced by the elem 2 min read Like