Java LinkedList element() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, element() returns the head of the LinkedList, which is its first element, and does not remove it. It is the Queue method for "show me who is next".

The name is easy to confuse with "any element". It really means the head of the queue. If the queue is empty, element() throws NoSuchElementException, while peek() would return null.

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

Call it through a Queue or a LinkedList variable.

Java
Queue<String> queue = new LinkedList<>();
queue.offer("first");
queue.offer("second");
String head = queue.element();   // "first"
CallWhat it does
queue.element()Returns the head. Throws if the queue is empty.
queue.peek()Returns the head, or null if the queue is empty.
queue.remove()Returns the head and removes it. Throws if empty.
queue.poll()Returns the head and removes it, or null if empty.

Queue methods come in pairs: a version that throws (add, remove, element) and a version that returns a special value (offer, poll, peek).

Library declarations
Java
// java.util.LinkedList
public E element()

Parameters

ParameterDescription
Noneelement() takes no parameters.

Return Value

Return ValueDescription
EThe head of the queue, which is the first element of the list.

Compare with Similar Methods

Queue methods that look at or take the head:

MethodHow it differs
peek()Looks at the head but returns null when empty.
getFirst()The Deque name for the same operation.
remove()Takes the head out of the queue; throws when empty.
poll()Takes the head out; returns null when empty.

Java Examples

Example 1 — Check Who Is Next in the Queue

Put three patients in a queue and ask for the head twice.

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

public class LinkedListElementDemo {
    public static void main(String[] args) {
        Queue<String> waiting = new LinkedList<>();
        waiting.offer("Dana");
        waiting.offer("Eli");
        waiting.offer("Finn");

        System.out.println("next: " + waiting.element());
        System.out.println("next again: " + waiting.element());
        System.out.println("waiting: " + waiting);
    }
}

How It Works

  1. Patients line up. Dana arrives first, then Eli, then Finn.
  2. element shows the head. The first call returns "Dana".
  3. Calling it again changes nothing. It does not remove Dana, so the second call also returns "Dana".
  4. The queue is intact. All three names are still waiting.

Example 2 — Catch the Exception from an Empty Queue

Compare what element and peek do on an empty queue.

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

public class LinkedListElementEmpty {
    public static void main(String[] args) {
        Queue<Integer> tickets = new LinkedList<>();

        System.out.println("peek: " + tickets.peek());

        try {
            System.out.println("element: " + tickets.element());
        } catch (NoSuchElementException e) {
            System.out.println("element -> NoSuchElementException");
        }
    }
}

How It Works

  1. No tickets yet. The queue has no elements.
  2. peek is gentle. It returns null, which prints as the word null.
  3. element is strict. It cannot return a head that does not exist, so it throws NoSuchElementException.
  4. The text line never prints. The exception happens before the string "element: ..." is complete, so only the catch message appears.

Example 3 — Look at the Head Before Taking It

Use element to read the head, remove to take it, and watch the head change.

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

public class LinkedListElementPair {
    public static void main(String[] args) {
        Queue<String> jobs = new LinkedList<>();
        jobs.add("print");
        jobs.add("scan");
        jobs.add("email");

        while (!jobs.isEmpty()) {
            String head = jobs.element();
            System.out.println("head is " + head);
            jobs.remove();
        }
        System.out.println("all done");
    }
}

How It Works

  1. Three jobs. They are stored in the order print, scan, email.
  2. Look first. At the start of each loop element() reports the current head.
  3. Then take. remove() with no arguments deletes the head, so the next job becomes the head.
  4. Loop ends cleanly. The isEmpty() test stops the loop before element() could ever throw.

Frequently Asked Questions

It returns the head (first element) of the list without removing it. It throws NoSuchElementException if the list is empty.
Both look at the head. On an empty queue, element throws NoSuchElementException and peek returns null.
Yes. They return the same element. element comes from the Queue interface, and getFirst comes from Deque.
No. Use remove() or poll() to remove the head.
No. The name is misleading. It always returns the head, which is the first element.

Next: forEach()

Run a piece of code for every element, using a lambda.

forEach() 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.

15 people found this page helpful