Collections: ArrayDeque is the modern stack

Stack is a legacy synchronized class. ArrayDeque is faster and is what the Javadoc recommends for LIFO use - push, pop and peek work on the head.

Code
Deque<String> stack = new ArrayDeque<>();
stack.push("first");
stack.push("second");
stack.push("third");

System.out.println("peek = " + stack.peek());
System.out.println("pop  = " + stack.pop());
System.out.println("remaining (top first) = " + stack);
Output
peek = third
pop  = third
remaining (top first) = [second, first]
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-07-30