Java ArrayList ensureCapacity() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java, ensureCapacity(minCapacity) asks an ArrayList to make its internal array big enough for at least minCapacity elements. It does not add any elements: the size stays the same.

An ArrayList grows automatically, copying its elements into a bigger array whenever it runs out of room. If you know you are about to add many elements, reserving the room once can save several of those copies. It is only a performance hint; your code works the same without it.

An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe.

Syntax

Call it on a variable declared as ArrayList.

Java
ArrayList<String> list = new ArrayList<>();
list.ensureCapacity(10000);    // room for 10000 elements
// list.size() is still 0
CallWhat it does
list.ensureCapacity(n)Makes room for at least n elements.
new ArrayList<>(n)Sets the initial capacity when creating the list.
list.trimToSize()The opposite: releases unused room.
list.size()Unchanged by ensureCapacity.

ensureCapacity is defined in ArrayList, not in List. If your variable is declared as List<T>, use the new ArrayList<>(n) constructor instead.

Library declarations
Java
// java.util.ArrayList<E>
public void ensureCapacity(int minCapacity)

Parameters

ParameterDescription
minCapacity (int)The number of elements the list should be able to hold without growing.

Return Value

Return ValueDescription
voidNothing; only internal storage may change.

Compare with Similar Methods

Capacity and size methods:

MethodHow it differs
trimToSize()Shrinks the capacity to the current size.
size()The number of elements, which ensureCapacity never changes.
addAll(c)Grows the list once for the whole batch.
isEmpty()Still true after reserving room in an empty list.

Java Examples

Example 1 — Reserve Room Without Adding Elements

Reserve space for 500 names and see that the list is still empty.

Java
import java.util.ArrayList;

public class ArrayListEnsureCapacityDemo {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.ensureCapacity(500);

        System.out.println("size after ensureCapacity(500): " + names.size());
        System.out.println("isEmpty: " + names.isEmpty());

        try {
            names.get(0);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("get(0) still fails: no elements yet");
        }
    }
}

How It Works

  1. ArrayList type. The variable is declared as ArrayList because ensureCapacity is not in the List interface.
  2. Only storage. The call may enlarge the internal array, but no elements are created, so the size stays 0.
  3. Still empty. isEmpty() looks at the size, so it is still true.
  4. No index 0. Reserved slots are not elements; get(0) throws IndexOutOfBoundsException.

Example 2 — Reserve Once Before a Large Loop

Reserve room for 1000 squares, then add them in a loop.

Java
import java.util.ArrayList;

public class ArrayListEnsureCapacityBatch {
    public static void main(String[] args) {
        int count = 1000;
        ArrayList<Integer> squares = new ArrayList<>();
        squares.ensureCapacity(count);

        for (int i = 1; i <= count; i++) {
            squares.add(i * i);
        }

        System.out.println("size: " + squares.size());
        System.out.println("first: " + squares.get(0) + ", last: " + squares.get(count - 1));
    }
}

How It Works

  1. Known size. The loop will add exactly count elements, so that number is reserved up front.
  2. No regrowth. With room for 1000 already in place, none of the adds needs to copy the elements into a bigger array.
  3. Same result. Without ensureCapacity the output would be identical; the hint only affects speed and memory churn.
  4. The values. The first square is 1 * 1 = 1 and the last is 1000 * 1000 = 1000000, at index 999.

Example 3 — Small or Negative Values Do Nothing

Call it with values the list already satisfies, then with a larger one.

Java
import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListEnsureCapacityNoop {
    public static void main(String[] args) {
        ArrayList<String> letters = new ArrayList<>(Arrays.asList("a", "b", "c"));

        letters.ensureCapacity(1);
        letters.ensureCapacity(0);
        letters.ensureCapacity(-5);
        System.out.println("after small values: " + letters + " size=" + letters.size());

        letters.ensureCapacity(50);
        letters.add("d");
        System.out.println("after ensureCapacity(50): " + letters + " size=" + letters.size());
    }
}

How It Works

  1. Already enough room. The list can already hold three elements, so asking for 1 or 0 changes nothing.
  2. No error for negatives. ensureCapacity(-5) is silently ignored instead of throwing.
  3. Bigger request. ensureCapacity(50) may move the three elements into a larger array, but their order and values stay the same.
  4. Normal adds. Adding "d" works as usual; capacity changes are invisible apart from performance.

Frequently Asked Questions

It makes the internal array large enough for at least the given number of elements, so later adds do not need to grow it. The size does not change.
No. The size stays the same and get(0) on an empty list still throws IndexOutOfBoundsException.
Right before adding a large, known number of elements to an existing list. For a new list, new ArrayList<>(n) does the same job.
No. ArrayList has no public method that returns its capacity.
It is declared only in ArrayList. Declare the variable as ArrayList, or pass the capacity to the constructor.

Next: forEach()

Run an action for every element (Java 8+).

forEach() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful