Wednesday, July 3, 2024
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.

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments