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.

1

ArrayList<Integer>aListNumbers=newArrayList<Integer>();

How to specify capacity of ArrayList?

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

1

publicArrayList(intinitialCapacity)

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

1

ArrayList<Integer>aListNumbers=newArrayList<Integer>(20);

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.

1

publicvoidensureCapacity(intminCapacity)

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.

1

newCapacity = (oldCapacity * 3)/2 + 1

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.

1

2

ArrayList<Integer>aListNumbers=newArrayList<Integer>(2);

System.out.println("ArrayList size : "+aListNumbers.size());

Output

1

ArrayList size : 0

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.

About the author

rahimv

rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.