Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which implementation of List to use?

In my program I often use collections to store lists of objects. Currently I use ArrayList to store objects. My question is: is this a best choice? May be its better to use LinkedList? Or something else?

Criteria to consider are:

  • Memory usage
  • Performance

Operations which I need are:

  • Add element to collection
  • Iterate through the elements

Any thoughts?

Update: my choice is : ArrayList :) Basing on this discussion as well as the following ones:

  • When to use LinkedList over ArrayList?
  • List implementations: does LinkedList really perform so poorly vs. ArrayList and TreeList?
like image 967
Andriy Sholokh Avatar asked Sep 30 '10 13:09

Andriy Sholokh


1 Answers

I always default to ArrayList, and would in your case as well, except when

  • I need thread safety (in which case I start looking at List implementations in java.util.concurrent)
  • I know I'm going to be doing lots of insertion and manipulation to the List or profiling reveals my usage of an ArrayList to be a problem (very rare)

As to what to pick in that second case, this SO.com thread has some useful insights: List implementations: does LinkedList really perform so poorly vs. ArrayList and TreeList?

like image 177
whaley Avatar answered Oct 12 '22 23:10

whaley