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 does not returns any value. It trims the capacity of this Stack instance to the number of the element it contains.
Below program illustrate the trimToSize() method:
// Java code to demonstrate the working of // trimToSize() method in Stack // for Stack functions import java.util.Stack; public class GFG { public static void main(String[] args) { // 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); // Print the current capacity of Stack System.out.println("Current capacity of Stack: " + stack.capacity()); // Change the capacity to 15 stack.ensureCapacity(15); // Print the current capacity of Stack System.out.println("New capacity of Stack: " + stack.capacity()); // trims the capacity to the number of elements stack.trimToSize(); // Print the current capacity of Stack System.out.println("Current capacity of Stack" + " after use of trimToSize() method: " + stack.capacity()); } } |
Stack: [10, 20, 30, 40] Current capacity of Stack: 10 New capacity of Stack: 20 Current capacity of Stack after use of trimToSize() method: 4
Example 2:
// Java program to demonstrate // Stack toString() method import java.util.*; public class collection { public static void main(String args[]) { // Creating an Empty Stack Stack<String> stack = new Stack<String>(); // Use add() method // to add elements to the Collection stack.add("Welcome"); stack.add("To"); stack.add("Geeks"); stack.add("For"); stack.add("Geeks"); // Print the Stack System.out.println("Stack: " + stack); // Print the current capacity of Stack System.out.println("Current capacity of Stack: " + stack.capacity()); // Change the capacity to 20 stack.ensureCapacity(20); // Print the current capacity of Stack System.out.println("New capacity of Stack: " + stack.capacity()); // trims the capacity to the number of elements stack.trimToSize(); // Print the current capacity of Stack System.out.println("Current capacity of Stack" + " after use of trimToSize() method: " + stack.capacity()); } } |
Stack: [Welcome, To, Geeks, For, Geeks] Current capacity of Stack: 10 New capacity of Stack: 20 Current capacity of Stack after use of trimToSize() method: 5
Recommended Posts:
- Vector trimToSize() method in Java with Example
- StringBuffer trimToSize() method in Java with Examples
- StringBuilder trimToSize() method in Java with Examples
- ArrayList trimToSize() in Java with example
- Stack set() method in Java with Example
- Stack get() method in Java with Example
- Stack contains() method in Java with Example
- Stack pop() Method in Java
- Stack listIterator() method in Java with Example
- Stack listIterator(int) method in Java with Example
- Stack setSize() method in Java with Example
- Stack removeAllElements() method in Java with Example
- Stack removeRange() method in Java with Example
- Stack add(int, Object) method in Java with Example
- Stack remove(int) method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




