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.
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:
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
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
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.