The ensureCapacity()
method is an advanced feature of the Java ArrayList
class that allows developers to manually increase the internal capacity of an ArrayList
before adding new elements. This method helps optimize performance in scenarios where a large number of elements will be added, reducing the need for repeated internal array resizing.
What Is the ensureCapacity() Method in ArrayList?
- The
ensureCapacity()
method increases the capacity of anArrayList
so it can accommodate a minimum number of elements without needing to resize again. - It is a way to prepare the list for bulk additions and avoid multiple incremental capacity expansions.
- This method does not change the size of the list or add any elements—only the internal array capacity changes.
Syntax
public void ensureCapacity(int minCapacity)
Parameters
Parameter | Description |
---|---|
minCapacity | The minimum capacity to be ensured for the list. |
- If
minCapacity
is greater than the current capacity, the capacity is increased. - If
minCapacity
is less than or equal to the current capacity, nothing happens.
Return Value
void
– The method does not return any value.
Exceptions
- There are no checked exceptions for standard
ArrayList
use. - May throw
OutOfMemoryError
if the JVM cannot allocate enough memory to increase capacity.
How Does ensureCapacity() Work Internally?
- Internally, the method checks the current capacity against
minCapacity
. - If
minCapacity
is greater, a new backing array with greater capacity is allocated, and existing elements are copied over. - This reduces the number of array resizes and memory reallocations when many elements will be added in bulk.
- The current list size is not changed, nor are new elements added—only the storage buffer is expanded.
Examples of the ensureCapacity() Method
1. Allocating Capacity Before Adding Elements
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); // Preallocate space for 100 elements numbers.ensureCapacity(100); // Now add elements (avoids repeated resizing) for (int i = 0; i < 100; i++) { numbers.add(i); } System.out.println("List size: " + numbers.size()); // Output: 100 } }
2. Using ensureCapacity() with Growing Lists
import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayList<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); // Prepare capacity for more names in one go names.ensureCapacity(10); names.add("Charlie"); names.add("Diana"); // No new memory allocation required until 10 elements added } }
Important Notes
- Use
ensureCapacity()
when you know in advance how many elements will be added, especially in data processing, importing, or bulk-insertion tasks. - It is not required for normal
ArrayList
operations because resizing is handled automatically, but it boosts performance for large, predictable insertions. - The method only affects capacity, not content or list length.
- The default capacity of an
ArrayList
is 10. Resizing typically multiplies capacity by 1.5 or 2 each time the current size limit is exceeded.
Summary
Aspect | Details |
---|---|
Method Purpose | Ensure internal array can hold a minimum number of elements |
Syntax | list.ensureCapacity(int minCapacity) |
Return Value | void |
Parameters | minCapacity (desired minimum capacity) |
Affects List Size? | No (only the underlying buffer) |
When to Use | Before adding a large number of elements |
Typical Use Cases | Performance-sensitive, bulk-loading scenarios |
Typical Use Cases
- Bulk data insertions: Avoid repeated resizing costs when adding large lists of items at once.
- Performance tuning: Optimize for big initial loads, database imports, or batch processing.
- Reusable lists: Pre-size lists for repeated batch operations throughout an application.