Queue has two parallel method families: add/remove/element throw on failure, while offer/poll/peek report failure by returning false or null, which suits code that expects an empty queue as a normal case.
Queue<String> q = new LinkedList<>();
System.out.println("poll on empty: " + q.poll());
System.out.println("peek on empty: " + q.peek());
try {
q.remove();
} catch (NoSuchElementException e) {
System.out.println("remove on empty throws: " + e.getClass().getSimpleName());
}
q.offer("task-1");
System.out.println("element(): " + q.element());
poll on empty: null
peek on empty: null
remove on empty throws: NoSuchElementException
element(): task-1
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-09-27