Stack ensureCapacity() method in Java with Example Last Updated : 24 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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 the desired minimum capacity as a parameter. Below are the examples to illustrate the ensureCapacity() method. Example 1: Java // Java program to demonstrate // ensureCapacity() 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> stack = new Stack<Integer>(); // adding element to stack stack.add(10); stack.add(20); stack.add(30); stack.add(40); // Print the Stack System.out.println("Stack: " + stack); // ensure that the Stack // can hold upto 5000 elements // using ensureCapacity() method stack.ensureCapacity(5000); // Print System.out.println("Stack can now" + " surely store upto" + " 5000 elements."); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } Output: Stack: [10, 20, 30, 40] Stack can now surely store upto 5000 elements. Example 2: Java // Java program to demonstrate // ensureCapacity() method for String value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of Stack<Integer> Stack<String> stack = new Stack<String>(); // adding element to stack stack.add("A"); stack.add("B"); stack.add("C"); stack.add("D"); // Print the Stack System.out.println("Stack: " + stack); // ensure that the Stack // can hold upto 400 elements // using ensureCapacity() method stack.ensureCapacity(400); // Print System.out.println("Stack can now" + " surely store upto" + " 400 elements."); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } Output: Stack: [A, B, C, D] Stack can now surely store upto 400 elements. Comment More infoAdvertise with us Next Article Stack ensureCapacity() 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 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 Stack setElementAt() method in Java with Example The setElementAt() method of Java Stack is used to set the component at the specified index of this vector to be the specified object. The previous component at that position is discarded. The index must be a value greater than or equal to 0 and less than the current size of the vector. Syntax: publ 2 min read Stack retainAll() method in Java with Example 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 3 min read 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 Like