The trimToSize() method 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
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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Clone() method in Java
- Java.util.Arraylist.indexOf() in Java
- Collections.sort() in Java with Examples
- Arraylist lastIndexOf() in Java with example
- Collections in Java
- HashSet vs TreeSet in Java
- ShortBuffer allocate() method in Java With Examples
- IntBuffer duplicate() method in Java with Examples
- ShortBuffer compact() method in Java With Examples
- LongBuffer equals() method in Java