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: