In Java, offer(e) adds an element to the tail of the LinkedList and returns true. It is the method that the Queue interface uses for "enqueue".
The Queue interface allows queues that can be full, and for those offer returns false instead of throwing an exception. A LinkedList is never full, so here it always returns true and behaves just like add(e).
An instance method of java.util.LinkedList (since Java 1.5): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by Queue, and LinkedList implements it. Call it on a LinkedList (or Queue) variable: a plain List variable cannot see it.
Syntax
Declare the list as a Queue to show your intent.
Java
Queue<String> queue = new LinkedList<>();
queue.offer("first"); // true
queue.offer("second"); // true
String next = queue.poll(); // "first"
Call
What it does
queue.offer(e)
Adds e at the tail and returns true.
queue.poll()
Takes the element from the head.
queue.peek()
Looks at the head without removing it.
queue.add(e)
Adds too, but would throw if a queue were full.
For a LinkedList the result is always true. The false case matters only for bounded queues, for example ArrayBlockingQueue.
Counting down. The loop feeds 5, 4, 3, 2, 1 into both lists.
Same order in both. Each list prints 5 4 3 2 1, because both methods append to the tail.
The lists are equal.equals compares element by element, and every element matches.
The newest sits last. The final value offered was 1, so getLast() returns 1.
Frequently Asked Questions
It adds the element at the tail (the end) of the list and returns true. It is the queue-style way to add an element.
For a LinkedList nothing practical. Both append and return true. The difference exists for bounded queues, where add throws an exception when full and offer returns false.
No. A LinkedList has no capacity limit, so offer always returns true.
poll() removes the head, peek() looks at it, and element() or remove() do the same with an exception on an empty queue.
Use Queue when you only need queue behavior. It documents your intent and hides the list methods you do not need.