ArrayList, ArrayDeque and locality
Definition:
Array-backed collections store references in indexed slots; linked collections store links between nodes.
Explanation:
ArrayList grows an internal array and shifts references for middle inserts.
Illustration image:
Example:
var tasks = new java.util.ArrayDeque<String>();
tasks.addLast("normal");
tasks.addFirst("urgent");
System.out.println(tasks.removeFirst());Expected behavior. The deque returns urgent without shifting the other element.
Internal working:
Its append cost is amortized, with occasional copy at growth. ArrayDeque uses an array and moving endpoints rather than shifting on normal endpoint operations. LinkedList costs a node allocation per element and pointer traversal for position queries. Compare actual footprint and cache locality, not just big-O.
Interview questions:
Why is LinkedList seldom faster for indexed access?
Finding the index requires traversal. Its cheap node relinking helps only after the position is known.