In Java, offer(e) inserts an element at the tail of an ArrayDeque and returns true. It is the queue-style insert from the Queue interface and does the same thing as offerLast(e).
In queue code it pairs with poll() to take elements from the head and peek() to look at the head, giving first-in, first-out (FIFO) order.
An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety.
Syntax
Call offer on a Queue or Deque variable.
Java
Queue<String> queue = new ArrayDeque<>();
boolean ok = queue.offer("job1"); // true, queue = [job1]
queue.offer("job2"); // queue = [job1, job2]
Call
What it does
queue.offer(e)
Inserts e at the tail and returns true.
queue.add(e)
Same result for ArrayDeque.
deque.offerLast(e)
The Deque name for the same operation.
queue.offer(null)
Throws NullPointerException. It does not return false.
The difference between offer and add only shows on queues with a size limit: there, offer returns false when the queue is full and add throws IllegalStateException. An ArrayDeque is never full, so both always succeed.
Library declarations
Java
// java.util.ArrayDeque<E>
public boolean offer(E e)
// Declared in Queue<E> and Deque<E>
Parameters
Parameter
Description
e
Required. The element to insert at the tail. Must not be null.
Return Value
Return Value
Description
boolean
Always true for an ArrayDeque.
Throw or Return a Special Value?
Each end has a method that throws when it cannot work (such as on an empty deque) and one that returnsnull or false instead. The highlighted cell matches offer().