It depends largely on whether you know the maximum size of each list up front.
If you do, use ArrayList
; it will certainly be faster.
Otherwise, you'll probably have to profile. While access to the ArrayList
is O(1), creating it is not as simple, because of dynamic resizing.
Another point to consider is that the space-time trade-off is not clear cut. Each Java object has quite a bit of overhead. While an ArrayList
may waste some space on surplus slots, each slot is only 4 bytes (or 8 on a 64-bit JVM). Each element of a LinkedList
is probably about 50 bytes (perhaps 100 in a 64-bit JVM). So you have to have quite a few wasted slots in an ArrayList
before a LinkedList
actually wins its presumed space advantage. Locality of reference is also a factor, and ArrayList
is preferable there too.
In practice, I almost always use ArrayList
.