Java LinkedList getFirst() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, getFirst() returns the first element (the head) of the LinkedList and leaves it in place. It needs no index and runs in constant time.

If the list is empty there is nothing to return, so the method throws NoSuchElementException. That makes it a good fit when an empty list would be a bug in your program.

An instance method of java.util.LinkedList (since Java 1.2): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by Deque, and LinkedList implements it. Call it on a LinkedList (or Deque) variable: a plain List variable cannot see it.

Syntax

Call it on a list that you expect to have elements.

Java
LinkedList<String> list = new LinkedList<>();
list.add("A");
list.add("B");
String head = list.getFirst();   // "A"
// new LinkedList<String>().getFirst() -> NoSuchElementException
CallWhat it does
list.getFirst()Returns the head. Throws if the list is empty.
list.peekFirst()Returns the head, or null if the list is empty.
list.element()The queue name: same result as getFirst().
list.get(0)Same element, but an empty list throws IndexOutOfBoundsException.

getFirst only looks. To read the head and remove it in one step, use removeFirst() or poll().

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

Parameters

ParameterDescription
NonegetFirst() takes no parameters.

Return Value

Return ValueDescription
EThe first element of the list.

Compare with Similar Methods

Ways to read the first element:

MethodHow it differs
peekFirst()Returns null instead of throwing when the list is empty.
element()The Queue name for the same operation.
removeFirst()Returns the first element and also removes it.
ArrayList.getFirst()The ArrayList version, available from Java 21.

Java Examples

Example 1 — Read the First Element of a List

Read the first element of a shopping list and check that it stays in the list.

Java
import java.util.LinkedList;

public class LinkedListGetFirstDemo {
    public static void main(String[] args) {
        LinkedList<String> shopping = new LinkedList<>();
        shopping.add("milk");
        shopping.add("bread");
        shopping.add("eggs");

        String first = shopping.getFirst();
        System.out.println("first item: " + first);
        System.out.println("list still: " + shopping);
        System.out.println("same as get(0): " + first.equals(shopping.get(0)));
    }
}

How It Works

  1. Three items. "milk" was added first, so it is the head of the list.
  2. getFirst returns the head. The variable first holds "milk".
  3. The list is unchanged. All three items are still in the list, and nothing was removed.
  4. Same as index 0. get(0) returns the same element, so equals is true.

Example 2 — See What Happens on an Empty List

Call getFirst on an empty list and catch the exception, then add an element and try again.

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

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

        try {
            int head = queue.getFirst();
            System.out.println("head: " + head);
        } catch (NoSuchElementException e) {
            System.out.println("empty list -> NoSuchElementException");
        }

        queue.add(99);
        System.out.println("after add: " + queue.getFirst());
    }
}

How It Works

  1. Nothing to read. The queue is empty, so there is no first element to return.
  2. An exception, not null. Java throws NoSuchElementException, and the catch block reports it.
  3. The print line is skipped. Because the call failed, "head: ..." never appears in the output.
  4. Now it works. After add(99) the list has a head, and getFirst() returns 99.

Example 3 — Guard getFirst with isEmpty

Write a small helper that reads the first element safely, and compare it with peekFirst.

Java
import java.util.LinkedList;

public class LinkedListGetFirstSafe {
    static String firstOrDefault(LinkedList<String> list) {
        if (list.isEmpty()) {
            return "(none)";
        }
        return list.getFirst();
    }

    public static void main(String[] args) {
        LinkedList<String> files = new LinkedList<>();
        System.out.println("empty:    " + firstOrDefault(files));
        System.out.println("peekFirst: " + files.peekFirst());

        files.add("report.pdf");
        System.out.println("filled:   " + firstOrDefault(files));
        System.out.println("peekFirst: " + files.peekFirst());
    }
}

How It Works

  1. A helper with a guard. It checks isEmpty() before calling getFirst(), so the exception can never happen.
  2. Empty list. The helper returns the text "(none)", while peekFirst() returns null, printed as null.
  3. After adding a file. Both ways now give "report.pdf".
  4. Pick by situation. Use getFirst when empty means a bug, and use peekFirst or a guard when empty is normal.

Frequently Asked Questions

Call list.getFirst(). It returns the first element and keeps it in the list.
It throws a NoSuchElementException. Use peekFirst() if you prefer a null result.
Both return the first element. On an empty list getFirst throws NoSuchElementException, while peekFirst returns null.
No. To remove the first element as well, use removeFirst() or poll().
It returns the same element, and it is clearer. On an empty list it throws NoSuchElementException instead of IndexOutOfBoundsException.

Next: getLast()

Read the last element, or throw if the list is empty.

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

9 people found this page helpful