My rule of thumb is if I need a Collection
(i.e. doesn't need to be a List
) then use ArrayList
if you know the size in advance, or can know the size confidently, or know that it won't vary much. If you need random access (i.e. you use get(index)
) then avoid LinkedList
. Basically, use LinkedList
only if you don't need index access and don't know the (approximate) size of the collection you're allocating. Also if you're going to be making lots of additions and removals (again through the Collection
interface) then LinkedList may be preferable.
↧
Answer by Sina Madani for When to use LinkedList over ArrayList in Java?
↧