Ch. 7 · Java

Java ArrayList Capacity and Performance

Java ArrayList Capacity and Performance. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readbeginnerupdated Oct 3, 2026

ArrayList stores elements in a resizable array. Appending is amortized efficient, while insertion near the front shifts later elements.

Before you start

You should know Java classes, methods, collections and exceptions. Trace object identity separately from the contents of an object. For concurrent examples, state which thread owns or shares the data; a single successful execution is not proof that every interleaving is safe.

The practical goal is to reason through this situation: Pre-sizing a known large result can reduce resizing and copying. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Separate size from capacity

Preallocation reserves space without inserting logical elements.

Step 2: Match operations

Appending differs from repeatedly shifting elements during front insertion.

Step 3: Choose from workload

Use deque operations for queue-like access when that matches requirements.

Worked scenario

Pre-sizing a known large result can reduce resizing and copying.

An ArrayList created with capacity 100 still reports size zero. Adding one item increases size to one. Repeated insertion at index zero shifts the existing contents each time; preallocating avoids some growth copies but does not remove those shifts.

Common mistake

Capacity is not size; a preallocated list still has no inserted elements.

Verify the behavior

Compare append and front-insert workloads at increasing sizes; inspect size independently of capacity.

Interview exercise

Choose a structure for repeated front inserts.

Answer and reasoning

Evaluate ArrayDeque or another structure matching required operations instead of treating every List implementation as equivalent.

Continue learning

Compare the scenario with the Java interview questions and test your understanding with the Java MCQs. For terminology and implementation details, consult the reference material.

More in Java

read ✓Java · hard

Java Time Types and Zone-Aware Decisions

Java Time Types and Zone-Aware Decisions. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readread →
read ✓Java · hard

Java Generics Wildcards and PECS

Use ? extends T for producers and ? super T for consumers, and understand why List<String> is not a List<Object>.

~2 min readread →
read ✓Java · mid

Java Comparators and Ordered Collections

Java Comparators and Ordered Collections. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readread →
esc