Thursday, October 9, 2025
HomeLanguagesJavaStack ensureCapacity() method in Java with Example

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 the desired minimum capacity as a parameter.

Below are the examples to illustrate the ensureCapacity() method.

Example 1:




// 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 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.
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32345 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6714 POSTS0 COMMENTS
Nicole Veronica
11877 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11940 POSTS0 COMMENTS
Shaida Kate Naidoo
6834 POSTS0 COMMENTS
Ted Musemwa
7094 POSTS0 COMMENTS
Thapelo Manthata
6789 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS