Home Java LinkedList Methods getFirst() Java LinkedList getFirst() Method 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.
LinkedList<String> list = new LinkedList<>();
list.add("A");
list.add("B");
String head = list.getFirst(); // "A"
// new LinkedList<String>().getFirst() -> NoSuchElementException Call What 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.util.LinkedList
public E getFirst() Parameters Parameter Description None getFirst() takes no parameters.
Return Value Return Value Description EThe first element of the list.
Important
An empty list throws NoSuchElementException, not null. Check isEmpty() first, or use peekFirst() when an empty list is a normal situation. In Java 21 and later, List also has a getFirst() method, but on Java 8 only LinkedList and Deque variables can call it.
Compare with Similar Methods Ways to read the first element:
Java Examples 1. Look at the head 2. Empty list 3. Choose the safe method 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.
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)));
}
} first item: milk
list still: [milk, bread, eggs]
same as get(0): true How It Works Three items. "milk" was added first, so it is the head of the list.getFirst returns the head. The variable first holds "milk".The list is unchanged. All three items are still in the list, and nothing was removed.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.
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());
}
} empty list -> NoSuchElementException
after add: 99 How It Works Nothing to read. The queue is empty, so there is no first element to return.An exception, not null. Java throws NoSuchElementException, and the catch block reports it.The print line is skipped. Because the call failed, "head: ..." never appears in the output.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.
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());
}
} empty: (none)
peekFirst: null
filled: report.pdf
peekFirst: report.pdf How It Works A helper with a guard. It checks isEmpty() before calling getFirst(), so the exception can never happen.Empty list. The helper returns the text "(none)", while peekFirst() returns null, printed as null.After adding a file. Both ways now give "report.pdf".Pick by situation. Use getFirst when empty means a bug, and use peekFirst or a guard when empty is normal.Frequently Asked Questions How do I get the first element of a LinkedList in Java? Call list.getFirst(). It returns the first element and keeps it in the list.
What happens if I call getFirst() on an empty LinkedList? It throws a NoSuchElementException. Use peekFirst() if you prefer a null result.
What is the difference between getFirst() and peekFirst()? Both return the first element. On an empty list getFirst throws NoSuchElementException, while peekFirst returns null.
Does getFirst() remove the element? No. To remove the first element as well, use removeFirst() or poll().
Is getFirst() the same as get(0)? It returns the same element, and it is clearer. On an empty list it throws NoSuchElementException instead of IndexOutOfBoundsException.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion