I haven't seen any mention of this yet (maybe I missed it among the 30+ answers...), but you should ALWAYS prefer an ArrayList
over a LinkedList
if you know what the final size of your collection will be. You can take advantage of the new ArrayList<>(size)
to ensure all insertions are O(1) (as long as you only add up to size
elements).
And yes, I'm aware ImmutableCollection
s are technically the correct choice for fixed size collections - but they become quite the pain in the ass if you ever decide to modify your list down the line (remove()
, sublist()
modifications, etc.)