Collections

Java ArrayList Capacity

Java ArrayList capacity example shows what is capacity of ArrayList in Java. Example also shows how to increase the ArrayList capacity and how to check ArrayList capacity.

What is ArrayList capacity in Java?

ArrayList is a dynamic array implementation of the List interface. We do not have to worry about the size of the ArrayList when we add elements to it. It grows automatically as we add the elements to it and resizes the underlying array accordingly. Size of this internal array is the capacity of the ArrayList.

When the internal array is full and we try to add an element to the ArrayList, a new array is created with more capacity and all existing array items are copied to it.

What is initial or default capacity of ArrayList?

When we first create an ArrayList object, the size of the internal array is 10 i.e. default initial capacity of the ArrayList.

Below given code will create an ArrayList object with initial capacity of 10.

How to specify capacity of ArrayList?

You can use the ArrayList constructor with initial capacity as an argument.

This constructor creates an ArrayList object with the specified initial capacity. For example,

Will create ArrayList object with initial capacity of 20. That means the ArrayList will be able to hold 20 elements before it needs to resize the internal array.

If you know the estimated size of the ArrayList, it is always better to specify the initial capacity when creating the ArrayList. Doing so will increase the performance of your application as it does not have to de-allocate and re-allocate the internal array when ArrayList grows beyond the capacity.

If you want to increase capacity of existing ArrayList, use ensureCapacity method.

This method increases the capacity of the ArrayList, if required, so that it can hold at least number of elements equal to the specified capacity.

How does the capacity calculated?

When the internal array is full, ArrayList needs to allocate the new array with more capacity, copy the existing elements to the new array and de-allocate the existing array. Exact details of the new capacity calculation are not specified but usually it is calculated as below.

That is 150% of the existing capacity plus 1. So, for example, if the ArrayList capacity is 10 and 11th element is added to it, new internal array will be created with size of (10 * 3)/2 + 1 that is 16.

Wondering why + 1? Is 150% not enough? Yes, it is in most cases. But consider scenario of ArrayList having capacity of 1. When you add second element to it, the new capacity calculation would be like (1 * 3)/2 which equals to 1 (i.e. same as old capacity). So 1 is added to cover this edge case scenario.

What is difference between ArrayList size and ArrayList capacity?

ArrayList capacity is the maximum number elements it can hold without resizing the internal array. Size of ArrayList is the number of elements it currently has. See below example for more details.

Output

Even though we created ArrayList with capacity of 2, the size remains 0 because we have not added any elements to it.

How to check ArrayList capacity?

There is no direct way to check the ArrayList capacity. ArrayList class maintains private Object array named elementData. Though it is never required, you may access this private array’s length to check the capacity of the ArrayList using Java reflection for experimental purpose.

Output

Note: Output could be different for you, as exact details on internal array growth policy is not specified by the Java specifications. Different implementation may have different growth policy.

Please let us know your views in the comments section below.

Join 1000+ fellow learners! Enter your email address below: