In Java, isEmpty() returns true when the LinkedList contains no elements, and false otherwise. It is the same as asking whether size() == 0, but easier to read.
It runs in constant time. It is also the standard guard before methods that throw on an empty list, such as removeFirst(), pop() and getFirst().
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 List, and LinkedList implements it.
Syntax
Call it with no arguments.
Java
LinkedList<String> list = new LinkedList<>();
boolean a = list.isEmpty(); // true
list.add("A");
boolean b = list.isEmpty(); // false
Call
What it does
list.isEmpty()
true if the list has no elements.
list.size() == 0
The same test, written less clearly.
list == null
A different test: is there a list object at all?
list.peek() == null
A way to check a queue that has no null elements.
isEmpty() tells you about the elements, not about the variable. A list that exists but holds nothing is empty. A variable that is null has no list at all.
Library declarations
Java
// java.util.LinkedList (inherited from AbstractCollection)
public boolean isEmpty()
Check a new list, a list with one element, and a list after clear().
Java
import java.util.LinkedList;
public class LinkedListIsEmptyDemo {
public static void main(String[] args) {
LinkedList<String> basket = new LinkedList<>();
System.out.println("new basket empty: " + basket.isEmpty());
basket.add("apple");
System.out.println("with apple empty: " + basket.isEmpty());
basket.clear();
System.out.println("after clear empty: " + basket.isEmpty());
System.out.println("same as size 0: " + (basket.size() == 0));
}
}
Output
new basket empty: true
with apple empty: false
after clear empty: true
same as size 0: true
How It Works
A new list is empty. There are no elements yet, so the answer is true.
One element changes it. After adding "apple", isEmpty() returns false.
clear empties it again. The answer goes back to true.
It matches size() == 0. The two tests always agree, but isEmpty() reads more naturally.
Example 2 — Use isEmpty to Guard a While Loop
Serve all the people in a line with a loop that checks isEmpty each time.
Java
import java.util.LinkedList;
public class LinkedListIsEmptyGuard {
public static void main(String[] args) {
LinkedList<String> line = new LinkedList<>();
line.add("Owen");
line.add("Pia");
int served = 0;
while (!line.isEmpty()) {
String person = line.removeFirst();
served++;
System.out.println("serving " + person);
}
System.out.println("served " + served + ", line empty: " + line.isEmpty());
}
}
Output
serving Owen
serving Pia
served 2, line empty: true
How It Works
Two people in line. Owen is first and Pia is second.
The loop test. The loop continues while the list is not empty, so removeFirst() is only called when there is a head.
Each pass serves one person. Owen and then Pia are removed and counted.
The loop ends on its own. After two passes the line is empty and the final line confirms it.
Example 3 — Tell an Empty List from a null Variable
Handle a list variable that may be null, and show that a list of nulls is not empty.
Java
import java.util.LinkedList;
public class LinkedListIsEmptyNull {
static String describe(LinkedList<String> list) {
if (list == null) {
return "no list at all";
}
return list.isEmpty() ? "empty list" : "list with " + list.size() + " element(s)";
}
public static void main(String[] args) {
LinkedList<String> none = null;
LinkedList<String> empty = new LinkedList<>();
LinkedList<String> nulls = new LinkedList<>();
nulls.add(null);
System.out.println("none: " + describe(none));
System.out.println("empty: " + describe(empty));
System.out.println("nulls: " + describe(nulls));
}
}
Output
none: no list at all
empty: empty list
nulls: list with 1 element(s)
How It Works
Three different situations. One variable holds no list, one holds an empty list, and one holds a list with a single null element.
Check null first. Calling isEmpty() on a null variable would throw NullPointerException, so the helper tests list == null before anything else.
An empty list is not null. The list exists and has no elements, so it is described as an empty list.
A list of nulls is not empty. It contains one element, even though that element is null, so it is reported as having one element.
Frequently Asked Questions
Call list.isEmpty(). It returns true when the list has no elements.
There is no difference in result. isEmpty() is simply clearer to read.
It throws a NullPointerException. Check list == null first, or make sure the variable always refers to a list.
No. A list with a null element has size 1, so isEmpty() returns false.
Those methods throw NoSuchElementException on an empty list. A quick isEmpty() check avoids the exception.