10

I want to be clear on something: When using an arraylist, it starts with a size of 10 elements. If it needs to auto-increase, it rewrites the whole arrayList to be 2/3 larger.

If I'm looking at a list that will eventually be size 50-120, is it better to:

  1. create it size 150 right off and have lots of unused space
  2. allow the list to be auto-increased a few times?

Thanks

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Adam
  • 791
  • 1
  • 12
  • 21
  • 2
    Unless you have profiled your code and this is indeed a bottleneck, I would not care about specifying initial size. Those numbers are too small. – gpeche Jun 07 '11 at 15:25

7 Answers7

10

If you know the likely eventual size of the ArrayList, it's usually best to specify it upfront:

ArrayList myList = new ArrayList(150);

This saves you the performance impact of having ArrayList re-allocate the array used to store its content (although, for the array size that you've specified, this impact will be negligible).

Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
  • But the likely eventual size is *not* really known here. Consider for example that 90% of the cases will only require 50 elements, and only 5% will require 120. – Konrad Rudolph Jun 07 '11 at 15:17
  • True, but optimization without actual data is always guesswork, that's why it's usually not recommended. – Tony the Pony Jun 08 '11 at 07:39
5

It is less computationally intensive to make it about as large as you will need right off the bat, but the truth is that java is very efficient, so it really isn't necessary to worry about how the arraylist is increased. However, if you are going for maximum efficiency, then yes, allocating the memory when you create the list is better.

MirroredFate
  • 12,396
  • 14
  • 68
  • 100
2

it rewrites the whole arrayList to be 2/3 larger

No. It makes the array twice as large (although the exact factor is an undocumented implementation detail). I stand corrected.

If I'm looking at a list that will eventually be size 50-120, is it better to: 1. create it size 150 right off

Why 150? Why not 120?

  1. allow the list to be auto-increased a few times?

In such a small range I would use the large size right away. If the span was much larger (e.g. 50–50000) I would reserve the smallest size (or perhaps an intermediate size, depending on the expected value distribution) and let it resize a few times.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    Are you sure about your first point? (I don't have the source handy) – jjnguy Jun 07 '11 at 15:12
  • @jjnguy It’s not specified but that is the typical implementation. – **EDIT** And I was wrong. – Konrad Rudolph Jun 07 '11 at 15:14
  • *The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.* -- 1.6 API – alexcoco Jun 07 '11 at 15:16
  • The source I have for ArrayList uses `increment = size / 2` when growing the array. It makes the list 50% larger, not twice as large and not 2/3 larger (although some might argue that 2/3 means 3/2). Other implementations may do something different. – Ted Hopp Jun 07 '11 at 15:18
  • actually it increments by 50% http://www.docjar.com/html/api/java/util/ArrayList.java.html (line 342 and `add(E)` calls it with `growAtEnd(1)`) – ratchet freak Jun 07 '11 at 15:20
2

Yes, Specifying the size before hand is better, because of auto-sizing. The bigger the ArrayList gets the more it has to resize itself.

RMT
  • 7,040
  • 4
  • 25
  • 37
1

If you roughly know the final size, then it would be more efficient to create it at that size. But for a list size of 150, this seems like a micro-optimization.

jzd
  • 23,473
  • 9
  • 54
  • 76
1

As long as you do not plan to create millions of these lists, it doen't actually matter. Copying array data is pretty fast and increasing the size to reach 50-120 items will not be measureable with a profiler. However, if you know that the list will finally have this size, I'd recommend to use this information when creating the list.

Sebastian Zarnekow
  • 6,609
  • 20
  • 23
1

You can also use nice method from Guava Lists.newArrayListWithExpectedSize.

Here is the javadoc

Creates an ArrayList instance sized appropriately to hold an estimated number of elements without resizing. A small amount of padding is added in case the estimate is low.

Antoine
  • 4,456
  • 4
  • 44
  • 51