Quick question here: why not ALWAYS use ArrayLists in Java? They apparently have equal access speed as arrays, in addition to extra useful functionality. I understand the limitation in that it cannot hold primitives, but this is easily mitigated by use of wrappers.
-
2See http://stackoverflow.com/questions/716597/array-or-list-in-java-which-is-faster for a reasonable thread of discussion on the issue. – euphoria83 Apr 04 '09 at 15:32
5 Answers
Plenty of projects do just use ArrayList
or HashMap
or whatever to handle all their collection needs. However, let me put one caveat on that. Whenever you are creating classes and using them throughout your code, if possible refer to the interfaces they implement rather than the concrete classes you are using to implement them.
For example, rather than this:
ArrayList insuranceClaims = new ArrayList();
do this:
List insuranceClaims = new ArrayList();
or even:
Collection insuranceClaims = new ArrayList();
If the rest of your code only knows it by the interface it implements (List
or Collection
) then swapping it out for another implementation becomes much easier down the road if you find you need a different one. I saw this happen just a month ago when I needed to swap out a regular HashMap
for an implementation that would return the items to me in the same order I put them in when it came time to iterate over all of them. Fortunately just such a thing was available in the Jakarta Commons Collections and I just swapped out A for B with only a one line code change because both implemented Map.

- 1,309
- 2
- 17
- 37

- 19,530
- 8
- 42
- 72
If you need a collection of primitives, then an array may well be the best tool for the job. Boxing is a comparatively expensive operation. For a collection (not including maps) of primitives that will be used as primitives, I almost always use an array to avoid repeated boxing and unboxing.
I rarely worry about the performance difference between an array and an ArrayList
, however. If a List
will provide better, cleaner, more maintainable code, then I will always use a List
(or Collection
or Set
, etc, as appropriate, but your question was about ArrayList
) unless there is some compelling reason not to. Performance is rarely that compelling reason.
Using Collection
s almost always results in better code, in part because arrays don't play nice with generics, as Johannes Weiß already pointed out in a comment, but also because of so many other reasons:
- Collections have a very rich API and a large variety of implementations that can (in most cases) be trivially swapped in and out for each other
- A Collection can be trivially converted to an array, if occasional use of an array version is useful
- Many Collections grow more gracefully than an array grows, which can be a performance concern
- Collections work very well with generics, arrays fairly badly
- As TofuBeer pointed out, array covariance is strange and can act in unexected ways that no object will act in. Collections handle covariance in expected ways.
- arrays need to be manually sized to their task, and if an array is not full you need to keep track of that yourself. If an array needs to be resized, you have to do that yourself.
All of this together, I rarely use arrays and only a little more often use an ArrayList
. However, I do use List
s very often (or just Collection
or Set
). My most frequent use of arrays is when the item being stored is a primitive and will be inserted and accessed and used as a primitive. If boxing and unboxing every become so fast that it becomes a trivial consideration, I may revisit this decision, but it is more convenient to work with something, to store it, in the form in which it is always referenced. (That is, 'int' instead of 'Integer'.)

- 53,828
- 22
- 125
- 145
This is a case of premature unoptimization :-). You should never do something because you think it will be better/faster/make you happier.
ArrayList has extra overhead, if you have no need of the extra features of ArrayList then it is wasteful to use an ArrayList.
Also for some of the things you can do with a List there is the Arrays class, which means that the ArrayList provided more functionality than Arrays is less true. Now using those might be slower than using an ArrayList, but it would have to be profiled to be sure.
You should never try to make something faster without being sure that it is slow to begin with... which would imply that you should go ahead and use ArrayList until you find out that they are a problem and slow the program down. However there should be common sense involved too - ArrayList has overhead, the overhead will be small but cumulative. It will not be easy to spot in a profiler, as all it is is a little overhead here, and a little overhead there. So common sense would say, unless you need the features of ArrayList you should not make use of it, unless you want to die by a thousands cuts (performance wise).
For internal code, if you find that you do need to change from arrays to ArrayList the chance is pretty straight forward in most cases ([i] becomes get(i), that will be 99% of the changes).
If you are using the for-each look (for( value : items) { }) then there is no code to change for that as well.
Also, going with what you said:
1) equal access speed, depending on your environment. For instance the Android VM doesn't inline methods (it is just a straight interpreter as far as I know) so the access on that will be much slower. There are other operations on an ArrayList that can cause slowdowns, depends on what you are doing, regardless of the VM (which could be faster with a stright array, again you would have to profile or examine the source to be sure).
2) Wrappers increase the amount of memory being used.
You should not worry about speed/memory before you profile something, on the other hand you shouldn't choose what you know to be a slower option unless you have a good reason to.

- 60,850
- 18
- 118
- 163
-
While I agree, and +1, I think your answer is overly verbose and too broad. :( Just sayin'... – Stu Thompson Apr 04 '09 at 17:31
-
yeah, I answered it after reading the other posts... so was trying to address a lot at once :-) – TofuBeer Apr 04 '09 at 17:43
Performance should not be your primary concern.
Use List
interface where possible, choose concrete implementation based on actual requirements (ArrayList
for random access, LinkedList
for structural modifications, ...).
You should be concerned about performance.
Use arrays, System.arraycopy
, java.util.Arrays
and other low-level stuff to squeeze out every last drop of performance.

- 16,789
- 6
- 57
- 63
Well don't always blindly use something that is not right for the job. Always start off using Lists, choose ArrayList as your implementation. This is a more OO approach. If you don't know that you specifically need an array, you'll find that not tying yourself to a particular implementation of List will be much better for you in the long run. Get it working first, optimize later.

- 546
- 3
- 11
-
The last part, get it working first, optimize later, applies to arrays as well... – TofuBeer Apr 04 '09 at 15:50
-
Absolutely, apply it to all programming, algo's as well as storage. In most general cases this approach works well. – wentbackward Apr 05 '09 at 06:40