Java Zero to All All questionsPractise free
Interview questions › Java Foundations — Zero to Basics › Collections Deep Dive — List, Set, Map, Queue

Compare ArrayDeque, Stack, and the various queue choices. What should you actually use for a stack or queue today?

Level II · JuniorCollections Deep Dive — List, Set, Map, Queue
Short answer

Use ArrayDeque for both stacks and queues: it's array-backed, faster than LinkedList, and unlike the legacy Stack class (which extends Vector and is synchronized and slow) it isn't burdened by Vector's design.

Explanation

Prefer the Deque methods (push/pop/offer/poll) which return/insert without throwing on empty, over add/remove which throw. For producer-consumer concurrency reach for BlockingQueue implementations (ArrayBlockingQueue, LinkedBlockingQueue), and for priority ordering use PriorityQueue, remembering it isn't thread-safe and iteration order isn't sorted.

Common mistakes
  • Using java.util.Stack in new code.
  • Using LinkedList as a queue where ArrayDeque is faster.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Collections Deep Dive — List, Set, Map, Queue.

Practise free

More on Collections Deep Dive — List, Set, Map, Queue