Does the capacity of ArrayList decrease when we remove elements?

There are several remove methods within arraylist, I will use the remove by index version as this example

 public E remove(int index) {

     rangeCheck(index);
     modCount++;
     E oldValue = elementData(index);

     int numMoved = size - index - 1;

     if (numMoved > 0)
           System.arraycopy(elementData, index+1, elementData, index,
                    numMoved);

     elementData[--size] = null; // Let gc do its work


     return oldValue;

 }

The most important thing to note is that a new array is never created within elementData so its size does not change, only elements are copied.

If you need to reduce the capacity of the array list (which you usually won't) use trimToSize()


It doesn't decrease this automatically. From the doc.

    public void trimToSize() 

Trims the capacity of this ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance.