Quick revision
The short answer
We create the list with its expected size, new ArrayList<>(1_000_000), so the backing array is allocated once instead of growing about 29 times and copying millions of references. If the size becomes known only later, ensureCapacity(n) does the same job on an existing ArrayList, and trimToSize() shrinks the array down to the current size when we have finished adding.
What to remember
In this answer, we saw that a default ArrayList grows about 29 times and copies about 2.4 million references to reach a million elements, ending with spare capacity. Creating it with new ArrayList<>(1_000_000) allocates once and copies nothing, and ensureCapacity does the same for an existing ArrayList when the count arrives late. trimToSize shrinks the array to the size when the list is finished, and over-sizing wastes memory, so we pre-size only from a real estimate.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We create the list with its expected size, new ArrayList<>(1_000_000), so the backing array is allocated once instead of growing about 29 times and copying millions of references. If the size becomes known only later, ensureCapacity(n) does the same job on an existing ArrayList, and trimToSize() shrinks the array down to the current size when we have finished adding.
Why this question is asked
Pre-sizing a collection is one of the cheapest performance fixes in Java, and also one that is easy to get wrong in both directions: too small and we pay for repeated copying, too big and we waste memory in every list. Interviewers ask this to see whether we understand what the capacity is and when it matters. In this answer, we will recap how ArrayList grows, count what a default list pays for a million adds, pre-size it, use ensureCapacity when the size arrives late, and then see what trimToSize does and when over-sizing hurts.
1A Quick Recap of Capacity
An ArrayList stores its elements in an internal Object[] array. The number of elements is the size; the length of the array is the capacity. While size is below capacity, add() just writes into the next free slot. When the array is full, ArrayList allocates a new array about 1.5 times bigger, copies every reference into it, and lets the old array become garbage.
A default new ArrayList<>() starts with no real array and allocates 10 slots on the first add. So a list that ends up with a million elements starts tiny and has to grow its way up.
2What a Default List Pays for a Million Adds
Let us count. Starting at 10 and multiplying by 1.5 each time, the capacity passes 1,000,000 after 29 growths. Each growth copies all the current elements, and those copies add up to about 2.4 million reference copies in total. It also allocates 29 arrays, the largest of about 1.2 million slots, and the intermediate ones all become garbage.
Notice the last capacity: 1,215,487 slots for 1,000,000 elements. So the default list not only copied a lot, it also ends with about 215,000 unused slots, roughly 0.86 MB of empty references with compressed pointers (4 bytes each). The copying is amortised O(1) per add, so this is not a disaster, but it is wasted work, extra garbage collection, and a temporary peak where the old and new arrays both exist during a copy.
3Pre-Sizing With the Constructor
When we know the count, we pass it to the constructor. The list allocates an array of exactly that capacity at once, and every add then writes into a slot that already exists.
6 messages in order. Play them, or step through them one at a time.
Same result, but the pre-sized list allocates once and copies nothing.
Two things are worth noticing. The constructor sets capacity, not size, so the list is still empty and get(0) fails until we add. And the underscores in 1_000_000 are only for readability; Java has allowed them in numeric literals since Java 7.
4When the Size Arrives Later: ensureCapacity
Sometimes the list already exists when we learn how many elements are coming, for example after reading a count header from a file or a result count from a query. ensureCapacity(n) grows the array once to at least n slots, so the following adds do not trigger repeated growth.
But there is a catch: ensureCapacity and trimToSize are methods of ArrayList, not of the List interface. If the variable is declared as List<Order>, they are not visible. That is one of the few places where we deliberately use the concrete type. Also, addAll(collection) already grows only once for the whole collection, so if the new elements arrive as a collection, we do not need ensureCapacity at all.
5What trimToSize Does
trimToSize() is the opposite operation. It replaces the backing array with a new one whose length equals the current size, releasing the unused tail. It is useful for a list that is built once and then kept for a long time, such as a cached lookup table.
Notice the last line: after trimming, the very next add has to grow the array again. So trimToSize makes sense only when the list is finished. On a list that keeps changing, it just causes extra copies.
6The Cost of Over-Sizing
Pre-sizing is a promise about the data, and a wrong promise is expensive in the other direction. new ArrayList<>(1_000_000) allocates about 4 MB of references immediately, even if only 200 elements ever arrive. In a method called for every request, or in a cache of many small lists, that waste multiplies quickly and can fill the heap.
So we pre-size only when the number is known or well estimated, such as the size of an input collection we are transforming, a count from the database, or a fixed batch size. When the estimate is a guess, we either leave the default growth, which wastes at most about a third of the array, or pre-size to a modest value and let the list grow from there.
7Follow-up questions
Is new ArrayList<>(n) the same as Collections.nCopies or an array of size n?
No. new ArrayList<>(n) creates an empty list with room for n elements. Collections.nCopies(n, x) creates an immutable list that already holds n copies of x, and new Object[n] creates an array of n nulls whose length is the size. If we need a list of n placeholder elements, we use new ArrayList<>(Collections.nCopies(n, x)).
Do HashMap and HashSet benefit from pre-sizing too?
Yes, and even more, because resizing a hash table rehashes every entry. But their constructor takes a capacity, not an element count, and they resize at 75 percent full. Since Java 19, HashMap.newHashMap(n) and HashSet.newHashSet(n) take the expected number of elements and compute the right capacity for us.
Does a stream's toList() pre-size its result?
It depends on the stream. A sized source such as a list or an array lets the stream know the count, and the implementation can use it; an unsized source such as a filter cannot. If we need precise control over capacity, a plain loop into a pre-sized ArrayList is the clearest choice.
8Conclusion
In this answer, we saw that a default ArrayList grows about 29 times and copies about 2.4 million references to reach a million elements, ending with spare capacity. Creating it with new ArrayList<>(1_000_000) allocates once and copies nothing, and ensureCapacity does the same for an existing ArrayList when the count arrives late. trimToSize shrinks the array to the size when the list is finished, and over-sizing wastes memory, so we pre-size only from a real estimate.
Open with
If I know a million items are coming, I create the list with new ArrayList<>(1_000_000). ArrayList keeps elements in an internal array, and the default list starts with ten slots and grows by about one and a half times whenever it is full, copying all the references each time.
Then, if they want more
To reach a million it grows about 29 times, copies roughly 2.4 million references in total, creates a lot of short-lived garbage, and ends with a capacity of about 1.2 million, so a fifth of the array is empty. Pre-sizing allocates one array of exactly the right size, and no add ever copies anything.
The constructor sets the capacity, not the size, so the list is still empty afterwards. If the list already exists when I learn the count, I call ensureCapacity with the expected size, which grows it once.
That method, and trimToSize, are only on ArrayList, not on the List interface, so the variable has to be typed as ArrayList there. trimToSize does the opposite: it shrinks the array to the current size, which is useful for a list that is finished and kept around, like a cached table, but not for one that keeps growing, because the next add grows it again.
And I only pre-size from a real estimate, because a list sized for a million that receives two hundred items wastes about four megabytes.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 19, Java 7
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You know you will add one million items to a list. How would you create it and why? What...": https://scrivio-interview-library.netlify.app/java/lists/ced12204bb/