Video in progress

When you need a queue, why is ArrayDeque often the better choice over LinkedList?

Both give you O(1) operations at both ends, but ArrayDeque works on a circular array: no separate node is created for each element, the data sits densely in memory, and there is less load on the GC. In benchmarks it usually comes out several times faster.

📖 Answer

A Big-O table shows the two as equal, but the constants differ:

Operation ArrayDeque LinkedList
offerLast / pollFirst O(1) amortized, a write into an array O(1), a new Node every time
memory / element ~4–8 bytes for a reference ~24–32 bytes for a node + a reference
CPU cache contiguous memory nodes scattered across the heap

When ArrayDeque fills up it doubles in size (at small sizes), which happens rarely.

Deque<Long> bfs = new ArrayDeque<>();
bfs.offer(startId);
while (!bfs.isEmpty()) {
    long id = bfs.poll();
    for (long next : graph.neighbors(id)) {
        if (visited.add(next)) bfs.offer(next);
    }
}

When LinkedList still wins: when you need to store null elements (ArrayDeque doesn't allow them), or when you need both the List and the Deque interface at once (index access). Those cases are very rare.

For multiple threads neither fits; there you want ArrayBlockingQueue, LinkedBlockingQueue or ConcurrentLinkedQueue.