The trimToSize() method of ArrayList in Java trims the capacity of an ArrayList instance to be the list’s current size. This method is used to trim an ArrayList 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 ArrayList instance to the number of the element it contains.
Errors and Exceptions: It does not have any errors and exceptions.
Below program illustrate the trimTosize() method:
The List elements are: Number = 2 Number = 4 Number = 5 Number = 6 Number = 11
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.