Repaso
Español
Java CoreCoreJV-11

In practice, which is the better default for a general-purpose list: ArrayList or LinkedList?

Options

  1. LinkedList, because it is always faster for every operation
  2. ArrayList: fast index access and compact in memory (Correct option)
  3. They behave exactly the same in speed and memory
  4. LinkedList, because it is thread-safe and ArrayList is not

Answer

ArrayList: fast access by index and compact in memory; LinkedList is rarely better

Why

ArrayList stores items in a resizable array: get(i) is instant (O(1)), appending is fast, and items sit together in memory, which CPUs like.

LinkedList stores each item in a separate node with links. Insertion in the middle is only cheap if you already hold the position; finding it takes O(n), and each node costs extra memory.

Use ArrayList by default, and ArrayDeque when you need a queue or stack.

Remember

Default to ArrayList.

Key terms

Practice this question in Repaso