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 and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Vector trimToSize() method in Java with Example
- Stack trimToSize() method in Java with Example
- StringBuilder trimToSize() method in Java with Examples
- StringBuffer trimToSize() method in Java with Examples
- ArrayList of ArrayList in Java
- Arraylist lastIndexOf() in Java with example
- ArrayList isEmpty() in Java with example
- Java.util.ArrayList.add() Method in Java
- Java.util.ArrayList.addall() method in Java
- Java.util.Arraylist.indexOf() in Java
- Array vs ArrayList in Java
- ArrayList in Java
- ArrayList vs LinkedList in Java
- How to remove an element from ArrayList in Java?
- ArrayList and LinkedList remove() methods in Java with Examples
- Vector vs ArrayList in Java
- Conversion of Array To ArrayList in Java
- Synchronization of ArrayList in Java
- Custom ArrayList in Java
- Initialize an ArrayList in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.