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"
Call
What 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
Parameter
Description
None
element() takes no parameters.
Return Value
Return Value
Description
E
The head of the queue, which is the first element of the list.
peek is gentle. It returns null, which prints as the word null.
element is strict. It cannot return a head that does not exist, so it throws NoSuchElementException.
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");
}
}
Output
head is print
head is scan
head is email
all done
How It Works
Three jobs. They are stored in the order print, scan, email.
Look first. At the start of each loop element() reports the current head.
Then take.remove() with no arguments deletes the head, so the next job becomes the head.
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.