Java ArrayList ensureCapacity()

The ArrayList ensureCapacity() method increases the capacity of the given ArrayList instance, if necessary, to ensure that it can hold at least the number of items specified by the method argument minCapacity.

We need to use ensureCapacity() method in cases when there is a huge number of add() operations in the ArrayList. In such cases, ArrayList will be frequently resized which is an expensive operation.

1. Syntax

public void ensureCapacity(int minCapacity)

If the value of minCapacity is less than 10 which is the default capacity of ArrayList, then the capacity ensured will be 10.

2. Why ArrayList Resizing is Expensive?

Java ArrayList internally uses a backing array object elementData which is used to store the list items. All ArrayList methods operate on this elementData and items stored in it.

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable,
    private static final int DEFAULT_CAPACITY = 10;

    transient Object[] elementData;

    //more code...

This is the reason why ArrayList is an ordered collection and provides index-based access to items.

Note that Arrays are fixed-size collections whereas ArrayList grows in runtime as soon as the backing array elementData is full and more elements are added to the list.

Increasing the size of elementData array is called resizing. This resizing is done in two steps:

  1. Create a new backing array with more size than previous array.
  2. Copy all elements from old array to this new array.

So basically, before adding any new item to the ArrayList with add() method, ArrayList performs a check whether there is any space left in the backing array using ensureCapacity() method.

If there is any space available in the backing array then the new element is added into the array; else a new backing array is created first.

Read More: ArrayList Sourcecode

3. ArrayList ensureCapacity() Example

Java program to use ensureCapacity() method to increase the size of an ArrayList after its initialization.

In given example, we have first created an ArrayList with size 2. Lets suppose we want to add 20 more elements to it, then during the addition resizing will happen a few times.

First-time resizing will grow the list size to 10. Then subsequent add() operations will cause the array to resize a few more times.

To avoid this resizing many times, we can call ensureCapacity() method with a size of 25. This will make enough room in the array to store all the additional 20 items that we are going to add. It improves the overall performance of the whole program.

public class ArrayListExample
    public static void main(String[] args)
        ArrayList<> list = new ArrayList<>(2);

Program output.

[A, B]
[A, B, C, D, E]

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

2 thoughts on “Java ArrayList ensureCapacity()”

  1. Hi Lokesh,

    I have one doubt related to growing size of ArrayList. For example, we have 10 elements in current arraylist and we are going to add one more element. Initial default size is 10. Now by giving below statements in actual implementation of arrayList new capacity will be 15.

    int oldCapacity = elementData.length; // 10
    int newCapacity = oldCapacity + (oldCapacity >> 1); // New Value will be 15

    Now size has increased and we have add more elements. In such cases size will be calculated.

    My Doubt is .. Assume we have 16000 elements in arrayList. and size is also 16000. Now i want to add one more element. As per above new size will be calculated and it will become near about 24000. For one element we have wasted 7999 memory allocation.

    is this way ArrayList works or there is any other implementation provided by Java to overcome such issue. Please provide your input on same.

    • Unfortunately, yes, that is how the ArrayList has been designed. The best solution, in this case, should be creating an array yourself with the desired size. Or consider using LinkedList.


Leave a Comment


A blog about Java and related technologies, the best practices, algorithms, and interview questions.