The trimToSize() method of Vector in Java trims the capacity of an Vector instance to be the Vector’s current size. This method is used to trim an Vector instance to the number of elements it contains.
Parameter: It does not accepts any parameter.
Return Value: It does not returns any value. It trims the capacity of this Vector instance to the number of the element it contains.
Below program illustrate the trimTosize() method:
Initial Vector is: [2, 4, 5, 6, 11] Initial size is: 5 Size after using trimToSize(): 5
Initial Vector is: [Geeks, 4, Geeks, Welcomes, You] Initial size is: 5 Size after using trimToSize(): 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.