Home Java LinkedList Methods getLast() Java LinkedList getLast() Method Definition In Java, getLast() returns the last element (the tail) of the LinkedList and leaves it in place. Because the list keeps a direct link to its tail, this is instant, however long the list is.
The method has no index to calculate, which is why list.getLast() is nicer than list.get(list.size() - 1). On an empty list it throws NoSuchElementException.
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");
list.add("C");
String tail = list.getLast(); // "C" Call What it does list.getLast()Returns the tail. Throws if the list is empty. list.peekLast()Returns the tail, or null if the list is empty. list.get(list.size() - 1)Same element, but needs an index and can throw IndexOutOfBoundsException. list.getFirst()Reads the other end.
getLast only looks. To read the tail and remove it, use removeLast() or pollLast().
Library declarations // java.util.LinkedList
public E getLast() Parameters Parameter Description None getLast() takes no parameters.
Return Value Return Value Description EThe last element of the list.
Important
An empty list throws NoSuchElementException. If an empty list is a normal case, call peekLast() and test for null, or check isEmpty() first. As with getFirst, a variable typed as List cannot call getLast() on Java 8.
Compare with Similar Methods Ways to read the last element:
Java Examples 1. Look at the tail 2. Versus get(size - 1) 3. peekLast alternative Example 1 — Read the Last Element of a List Read the most recent entry of a log and compare the two ends.
import java.util.LinkedList;
public class LinkedListGetLastDemo {
public static void main(String[] args) {
LinkedList<String> log = new LinkedList<>();
log.add("server started");
log.add("user logged in");
log.add("file saved");
System.out.println("latest: " + log.getLast());
System.out.println("oldest: " + log.getFirst());
System.out.println("entries: " + log.size());
}
} latest: file saved
oldest: server started
entries: 3 How It Works Entries are appended. Each add places a new message after the previous one.getLast gives the newest. The tail of the list is "file saved", the entry added last.getFirst gives the oldest. The head is "server started".Reading does not remove. The log still holds three entries.Example 2 — Compare getLast with get(size() - 1) Read the last element both ways and show what each one does on an empty list.
import java.util.LinkedList;
import java.util.NoSuchElementException;
public class LinkedListGetLastVersus {
public static void main(String[] args) {
LinkedList<Integer> temps = new LinkedList<>();
temps.add(18);
temps.add(21);
temps.add(19);
System.out.println("getLast: " + temps.getLast());
System.out.println("get(size() - 1): " + temps.get(temps.size() - 1));
temps.clear();
try {
temps.getLast();
} catch (NoSuchElementException e) {
System.out.println("empty getLast -> NoSuchElementException");
}
try {
temps.get(temps.size() - 1);
} catch (IndexOutOfBoundsException e) {
System.out.println("empty get(-1) -> IndexOutOfBoundsException");
}
}
} getLast: 19
get(size() - 1): 19
empty getLast -> NoSuchElementException
empty get(-1) -> IndexOutOfBoundsException How It Works Same answer on a full list. Both ways return 19, the last temperature.getLast is shorter. It needs no size and no subtraction, so there is no room for an off-by-one mistake.Empty list, getLast. After clear() there is no tail, so getLast() throws NoSuchElementException.Empty list, get(-1). The size is 0, so the index becomes -1, and get throws IndexOutOfBoundsException instead.Example 3 — Use peekLast When the List May Be Empty Print the last message of a chat that may be empty, using peekLast and a null check.
import java.util.LinkedList;
public class LinkedListGetLastSafe {
static void showLast(LinkedList<String> chat) {
String last = chat.peekLast();
if (last == null) {
System.out.println("no messages yet");
} else {
System.out.println("last message: " + last);
}
}
public static void main(String[] args) {
LinkedList<String> chat = new LinkedList<>();
showLast(chat);
chat.add("Hello!");
chat.add("Are you there?");
showLast(chat);
}
} no messages yet
last message: Are you there? How It Works A helper that expects empty lists. It calls peekLast(), which gives null when nothing is stored.First call. The chat is empty, so the helper prints "no messages yet".Two messages added. The tail is now "Are you there?".Second call. The helper prints the tail. No exception handling was needed anywhere.Frequently Asked Questions How do I get the last element of a LinkedList in Java? Call list.getLast(). It returns the last element and keeps it in the list.
What happens if I call getLast() on an empty LinkedList? It throws a NoSuchElementException. Use peekLast() to get null instead.
Is getLast() fast? Yes. A LinkedList keeps a direct link to its last node, so getLast runs in constant time.
What is the difference between getLast() and peekLast()? Both return the last element. On an empty list getLast throws NoSuchElementException, and peekLast returns null.
Should I use getLast() or get(size() - 1)? Use getLast(). It is clearer, and it avoids the index arithmetic.
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.
10 people found this page helpful
Helpful Share Copy link Suggestion