Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

Queue.offer()

Adds an element to the tail of a queue.

Available since: Java 1.5

Syntax

boolean offer(E e)

Parameters

ParameterTypeDescription
eEThe element to add.

Return value

boolean`true` if the element was added; `false` if a bounded queue is full.

Examples

Adding to a queue:

void main() {
    Queue<Integer> jobs = new ArrayDeque<>();
    jobs.offer(10);
    jobs.offer(20);
    IO.println(jobs);
}

Output:

[10, 20]

Notes

For ordinary queues offer() and add() behave the same. The difference shows in bounded queues (for example ArrayBlockingQueue): add() throws IllegalStateException, while offer() returns false. ArrayDeque does not accept null.

See also

  • Queue.poll() — Takes and removes the head of a queue.
  • Queue.peek() — Returns the head of a queue without removing it.
  • Queue — A queue: elements are added at the tail and taken from the head.

In the tutorial

  • Queues and deques — Queues in Java: Queue and ArrayDeque, a stack built on Deque, the offer, poll and peek methods and the PriorityQueue.

Thanks for reading!

Donate to the site — we work hard for you.