Quick revision
The short answer
Each pair does the same job but reports failure differently: add, remove and element throw an exception (IllegalStateException when the queue is full, NoSuchElementException when it is empty), while offer, poll and peek return false or null. BlockingQueue adds put and take, which wait as long as needed, and timed versions offer(e, timeout, unit) and poll(timeout, unit) that wait up to a limit.
What to remember
In this answer, we saw that each Queue pair differs only in how it reports failure: add, remove and element throw, while offer, poll and peek return false or null. On a bounded queue we should prefer offer and check the result. Because null means empty, queues other than LinkedList refuse null elements. BlockingQueue adds put and take, which wait, and timed offer and poll, which wait up to a limit, and Deque adds First and Last versions of every method plus push and pop for stack use.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Each pair does the same job but reports failure differently: add, remove and element throw an exception (IllegalStateException when the queue is full, NoSuchElementException when it is empty), while offer, poll and peek return false or null. BlockingQueue adds put and take, which wait as long as needed, and timed versions offer(e, timeout, unit) and poll(timeout, unit) that wait up to a limit.
Why this question is asked
We meet queues everywhere in Java backends: task queues in executors, buffers between threads, work lists in algorithms. The method names look like synonyms, and mixing them up causes real bugs, such as an exception on a full queue in production or a worker thread that spins at 100 percent CPU. Interviewers ask this to check that we know how each method reports failure and which ones block. In this answer, we will go through the three pairs on Queue, see why queues forbid null, look at the blocking methods BlockingQueue adds, and finish with the extra methods of Deque.
1A Quick Recap of the Queue Interface
A queue holds elements waiting to be processed. Most queues are FIFO, first in, first out, like a line at a counter: we add at the tail and take from the head. PriorityQueue is the common exception, taking the smallest element first.
The Queue interface gives us three operations: insert an element, remove the head, and look at the head without removing it. For each operation it offers two methods. The difference between the two is only what happens when the operation cannot be done, for example when a bounded queue is full or when any queue is empty.
2Exceptions or Special Values: The Three Pairs
Here is each pair on an ArrayBlockingQueue with room for two elements. It is bounded, so we can see both the full case and the empty case.
| Operation | Throws on failure | Returns a special value |
|---|---|---|
| Insert at the tail | add(e): IllegalStateException if full | offer(e): false if full |
| Remove the head | remove(): NoSuchElementException | poll(): null if empty |
| Look at the head | element(): NoSuchElementException | peek(): null if empty |
LinkedList or ConcurrentLinkedQueue never fail to insert, so add and offer behave the same there.Notice the pattern. add, remove and element come from the Collection world, where failing to do what we asked is unusual, so they throw. offer, poll and peek were designed for queues, where a full or empty queue is a normal situation, so they return a special value: false for a failed insert, null for an empty queue.
This is the reason to prefer offer on a bounded queue and to check its result. A full queue is something we expect to happen under load, and we usually want to decide what to do, such as dropping, retrying or rejecting the request, instead of letting an exception bubble up.
3Why Most Queues Forbid null
If poll() returns null to mean the queue is empty, then a null element would be impossible to tell apart from an empty queue. So almost every queue implementation refuses null elements and throws NullPointerException from offer(null) or add(null). ArrayDeque, PriorityQueue, ConcurrentLinkedQueue and all the BlockingQueue implementations work this way.
LinkedList is the odd one out. It existed as a List before Queue was added in Java 5, and lists allow null, so it still accepts null elements. That is one more reason to use ArrayDeque rather than LinkedList when we need a plain queue: a null that slips in will fail at once instead of being mistaken for an empty queue later.
4What BlockingQueue Adds
A BlockingQueue is a queue that threads share. Its extra methods let a thread wait instead of failing. put(e) waits until there is room, and take() waits until there is an element. The timed versions wait up to a limit and then report failure in the usual way.
All four waiting methods throw InterruptedException, because a waiting thread must be able to stop when someone interrupts it, for example during shutdown. So the full family has four styles: throw, return a special value, block, and block with a timeout. BlockingQueue also adds drainTo(collection, max), which moves up to max elements in one call and is useful for processing in batches.
5Deque: Both Ends, and a Stack
A Deque, short for double-ended queue, lets us insert and remove at both ends. Each Queue method gets a First and a Last version, with the same two failure styles: addFirst and addLast throw, offerFirst and offerLast return false; removeFirst and pollFirst, removeLast and pollLast; getFirst and peekFirst, getLast and peekLast.
push and pop make a Deque work as a stack, last in, first out, at the head. The Javadoc recommends ArrayDeque over the old Stack class, which extends Vector and synchronizes every call for no benefit in single-threaded code.
6Follow-up questions
Which method does an executor use to put tasks into its work queue?
ThreadPoolExecutor uses offer(). If the queue is bounded and full, offer returns false, and the executor then tries to start an extra thread up to maximumPoolSize; if it cannot, it hands the task to the rejection handler. It never blocks the submitting thread by default, which is exactly the special-value style at work.
Is peek() on a ConcurrentLinkedQueue safe to combine with poll()?
Each call is thread-safe, but the pair is not atomic. Another thread may take the element between our peek and our poll, so the element we poll may not be the one we peeked. When we want to take the head, we should just call poll() and check for null.
What does Java 21 add to Deque?
Java 21 introduced sequenced collections, and Deque now extends SequencedCollection. It gains reversed(), which returns a reverse-ordered view, while getFirst, getLast, addFirst, addLast, removeFirst and removeLast now come from that common interface, so List and Deque share them.
7Conclusion
In this answer, we saw that each Queue pair differs only in how it reports failure: add, remove and element throw, while offer, poll and peek return false or null. On a bounded queue we should prefer offer and check the result. Because null means empty, queues other than LinkedList refuse null elements. BlockingQueue adds put and take, which wait, and timed offer and poll, which wait up to a limit, and Deque adds First and Last versions of every method plus push and pop for stack use.
Open with
The three pairs do the same thing and differ in how they report failure. add, remove and element throw: add throws IllegalStateException if a bounded queue is full, and remove and element throw NoSuchElementException if it is empty. offer, poll and peek return a special value instead: offer returns false, and poll and peek return null. peek and element only look at the head, while poll and remove take it out.
Then, if they want more
On a bounded queue like ArrayBlockingQueue I prefer offer and check the boolean, because a full queue is a normal condition under load and I want to decide what to do rather than catch an exception. Because null is the empty signal from poll, almost all queues refuse null elements; LinkedList is the exception because it was a List first.
BlockingQueue adds the waiting versions: put blocks until there is space, take blocks until there is an element, and offer and poll have timed overloads that wait up to a limit and then return false or null. All of those throw InterruptedException so a waiting thread can be stopped. It also adds drainTo for batches.
Deque adds first and last variants of each method, like offerFirst, pollLast and peekFirst, and push and pop for using it as a stack, where ArrayDeque is the recommended choice over the old Stack class.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 5
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "The Queue interface has add/offer, remove/poll and element/peek. Explain the difference...": https://scrivio-interview-library.netlify.app/java/queues-and-deques/e31a4a5cc4/