Java LinkedList offer() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

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"
CallWhat 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.

Library declarations
Java
// java.util.LinkedList
public boolean offer(E e)

Parameters

ParameterDescription
e (E)The element to add. A LinkedList accepts null.

Return Value

Return ValueDescription
booleantrue, always, for a LinkedList.

Compare with Similar Methods

Ways to add at the tail:

MethodHow it differs
add(e)Same result for a LinkedList. Prefer offer when the variable is a Queue.
offerLast(e)The Deque name for the same action.
poll()The matching way to take an element from the head.
ArrayDeque.offer()The array-based version; it rejects null.

Java Examples

Example 1 — Add Customers to a Waiting Queue

Declare the list as a Queue, offer three customers, and read the head.

Java
import java.util.LinkedList;
import java.util.Queue;

public class LinkedListOfferDemo {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();

        boolean r1 = queue.offer("Asha");
        boolean r2 = queue.offer("Bilal");
        queue.offer("Chen");

        System.out.println("queue: " + queue);
        System.out.println("results: " + r1 + ", " + r2);
        System.out.println("next in line: " + queue.peek());
    }
}

How It Works

  1. A Queue variable. The object is a LinkedList, but the variable only exposes the queue methods.
  2. Customers line up at the tail. Asha joins first, then Bilal, then Chen.
  3. Always true. Both stored results are true, because this queue never says it is full.
  4. The head is the oldest. peek() shows Asha without removing her.

Example 2 — Process Jobs in Arrival Order

Offer some job names, then poll them one by one to see first-in, first-out order.

Java
import java.util.LinkedList;
import java.util.Queue;

public class LinkedListOfferJobs {
    public static void main(String[] args) {
        Queue<String> jobs = new LinkedList<>();
        jobs.offer("compile");
        jobs.offer("test");
        jobs.offer("deploy");

        while (!jobs.isEmpty()) {
            String job = jobs.poll();
            System.out.println("running " + job + ", waiting: " + jobs.size());
        }
    }
}

How It Works

  1. Three jobs offered. They enter the queue as compile, test, deploy.
  2. poll removes the head. Each loop takes the oldest job off the front.
  3. First in, first out. The jobs run in exactly the order they were offered.
  4. The count shrinks. The waiting number drops from 2 to 1 to 0, and the loop stops once the queue is empty.

Example 3 — Compare offer with add and a Deque Position

Show that offer and add build the same list, and that new items always go to the tail.

Java
import java.util.LinkedList;

public class LinkedListOfferSame {
    public static void main(String[] args) {
        LinkedList<Integer> byOffer = new LinkedList<>();
        LinkedList<Integer> byAdd = new LinkedList<>();

        for (int n = 5; n >= 1; n--) {
            byOffer.offer(n);
            byAdd.add(n);
        }

        System.out.println("offer: " + byOffer);
        System.out.println("add:   " + byAdd);
        System.out.println("equal: " + byOffer.equals(byAdd));
        System.out.println("last:  " + byOffer.getLast());
    }
}

How It Works

  1. Counting down. The loop feeds 5, 4, 3, 2, 1 into both lists.
  2. Same order in both. Each list prints 5 4 3 2 1, because both methods append to the tail.
  3. The lists are equal. equals compares element by element, and every element matches.
  4. 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.

Next: offerFirst()

Insert an element at the front and return true.

offerFirst() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful