ArrayList trimToSize() method in java

Let’s learn ArrayList trimToSize() method in java.

ArrayList trimToSize() method in java

trimToSize() method trims the capacity of given ArrayList instance to be the list’s current size.

Also read – ArrayList in java

Now let’s see an example on ArrayList trimToSize() method.

import java.util.ArrayList;
public class TrimToSizeMethodJavaExample
{
   public static void main(String[] args)
   {
      // initially size of ArrayList is 10
      ArrayList<Integer> al = new ArrayList<Integer>(10);
      // adding integers to ArrayList using add() method
      al.add(1);
      al.add(3);
      al.add(5);
      al.add(9);
      al.add(11);
      al.add(14);
      // trims the size of ArrayList to 6 using trimToSize() method
      al.trimToSize();
      System.out.println("The ArrayList elements are: ");
      for(Integer number : al)
      {
         System.out.println("Number: " + number);
      }
   }
}


Output:

The ArrayList elements are:
Number: 1
Number: 3
Number: 5
Number: 9
Number: 11
Number: 14