trimToSize
trimToSize is a method found in certain Java collection classes, most notably java.util.ArrayList and java.util.Vector. Its purpose is to reduce the internal storage capacity of the list so that it matches the list’s current size, thereby potentially lowering memory usage by discarding unused capacity.
In ArrayList, the list stores elements in an internal array (often referred to as elementData). The size
Performance and considerations: trimToSize is typically an O(n) operation because it copies elements to a new
When to use: consider trimToSize after bulk removals or when a long-lived list is known to hold