The same class serves as a queue: offer adds at the tail, poll takes from the head, and poll returns null instead of throwing when empty.
Deque<String> queue = new ArrayDeque<>();
queue.offer("job1");
queue.offer("job2");
queue.offer("job3");
System.out.println("poll = " + queue.poll());
System.out.println("poll = " + queue.poll());
System.out.println("left = " + queue);
queue.clear();
System.out.println("poll on empty returns " + queue.poll());
poll = job1
poll = job2
left = [job3]
poll on empty returns null
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