Java Zero to All All questionsPractise free
Interview questions › Core & Modern Java (8 -> 21) › Collections & Equals/HashCode Contracts

Why prefer ArrayDeque over Stack and LinkedList, and List.of over Collections.unmodifiableList?

Level II · JuniorCollections & Equals/HashCode Contracts
Short answer

Stack extends Vector and synchronizes every operation for no benefit in single-threaded code, while LinkedList suffers cache-unfriendly pointer chasing and per-node object overhead; ArrayDeque is a contiguous, unsynchronized ring buffer that is faster for both stack and queue use.

Explanation

For immutability, List.of returns a genuinely immutable list that also rejects nulls, whereas Collections.unmodifiableList returns a read-only view over a backing list, so if someone holds the backing list they can still mutate it underneath you. Prefer the truly immutable factory when you want a real guarantee.

Common mistakes
  • Using Stack or LinkedList for a stack or queue.
  • Thinking unmodifiableList copies the data — it is a view.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Collections & Equals/HashCode Contracts.

Practise free

More on Collections & Equals/HashCode Contracts