ArrayList capacity vs size

Java ArrayList capacity example shows what is capacity of ArrayList in Java. The 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 the 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 an initial capacity of 10.

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

How to specify the capacity of ArrayList?

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

public ArrayList(int initialCapacity)

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

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

Will create an ArrayList object with an 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 the capacity of existing ArrayList, use ensureCapacity method.

public void ensureCapacity(int minCapacity)

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

How the ArrayList capacity is 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.

newCapacity = (oldCapacity * 3)/2 + 1

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

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

What is the difference between ArrayList size and ArrayList capacity?

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

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

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

Output

Even though we created ArrayList with a 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 ArrayList capacity. The ArrayList class maintains a 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 purposes.

package com.javacodeexamples.collections.arraylist;

import java.lang.reflect.Field;

import java.util.ArrayList;

public class ArrayListCapacity {

    public static void main(String[] args) throws Exception {

         * This will create ArrayList with capacity of 10.

        ArrayList<Integer> aListNumbers = new ArrayList<Integer>(10);

         * Size = 0 since haven't added any elements to it

        System.out.println("ArrayList size : "

        System.out.println("ArrayList capacity : "

                                + getArrayListCapacity(aListNumbers));

        //add 10 elements to ArrayList

         * size = 10, because we added 10 elements

         * capacity = 10 because internal array of size 10 could fit 10 elements

        System.out.println("ArrayList contains: "

        System.out.println("ArrayList size : "

        System.out.println("ArrayList capacity : "

                                + getArrayListCapacity(aListNumbers));

        //add one more element beyond the initial capacity

         * size = 11, because we added 11 elements

         * capacity = 16 because internal array of size 10 could not fit

         * 11 element so new array has to be created. New array's

         * size is calculated as (10 * 3)/2 + 1 = 16

        System.out.println("ArrayList contains: "

        System.out.println("ArrayList size : "

        System.out.println("ArrayList capacity : "

                                + getArrayListCapacity(aListNumbers));

    private static int getArrayListCapacity(ArrayList<Integer> list) throws Exception{

        //get the elementData field from ArrayList class

        Field arrayField = ArrayList.class.getDeclaredField("elementData");

         * Since the elementData field is private, we need

         * to make it accessible first

        arrayField.setAccessible(true);

        //now get the elementData Object array from our list

        Object[] internalArray = (Object[])arrayField.get(list);

        //Internal array length is the ArrayList capacity!

        return internalArray.length;

Output

ArrayList contains: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

ArrayList contains: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

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

This example is a part of the Java ArrayList tutorial with examples.

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