VOOZH about

URL: https://www.geeksforgeeks.org/java/stack-trimtosize-method-in-java-with-example/

⇱ Stack trimToSize() method in Java with Example - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Stack trimToSize() method in Java with Example

Last Updated : 24 Dec, 2018
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:
Output:
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:
Output:
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
Comment