Array or List in Java. Which is faster?

euphoria83 picture euphoria83 · Apr 4, 2009 · Viewed 278.4k times · Source

I have to keep thousands of strings in memory to be accessed serially in Java. Should I store them in an array or should I use some kind of List ?

Since arrays keep all the data in a contiguous chunk of memory (unlike Lists), would the use of an array to store thousands of strings cause problems ?

Answer

Fortyrunner picture Fortyrunner · Apr 4, 2009

I suggest that you use a profiler to test which is faster.

My personal opinion is that you should use Lists.

I work on a large codebase and a previous group of developers used arrays everywhere. It made the code very inflexible. After changing large chunks of it to Lists we noticed no difference in speed.