Java ArrayDeque element() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, element() returns the head of an ArrayDeque without removing it. If the deque is empty, it throws NoSuchElementException.

It comes from the Queue interface and is the same as getFirst(). Its "special value" partner is peek(), which returns null on an empty deque instead.

An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety.

Syntax

Call element with no arguments.

Java
Queue<String> queue = new ArrayDeque<>();
queue.offer("first");
String head = queue.element();   // "first", still in the queue
CallWhat it does
queue.element()Returns the head; throws if empty.
queue.peek()Returns the head, or null if empty.
deque.getFirst()The Deque name for element().
queue.remove()Returns the head and removes it.

Calling element() twice in a row returns the same object both times, because nothing is removed. Use it when the queue must not be empty at that point in your program.

Library declarations
Java
// java.util.ArrayDeque<E>
public E element()

// Declared in Queue<E> and Deque<E>

Parameters

ParameterDescription
Noneelement() takes no parameters.

Return Value

Return ValueDescription
EThe head of the deque, which stays in place.

Throw or Return a Special Value?

Each end has a method that throws when it cannot work (such as on an empty deque) and one that returns null or false instead. The highlighted cell matches element().

OperationHead: throwsHead: special valueTail: throwsTail: special value
InsertaddFirst()offerFirst()addLast()offerLast()
RemoveremoveFirst()pollFirst()removeLast()pollLast()
ExaminegetFirst()peekFirst()getLast()peekLast()

Queue and stack names: add() = addLast(), offer() = offerLast(), push() = addFirst(), pop() = removeFirst(), remove() = removeFirst(), poll() = pollFirst(), element() = getFirst(), peek() = peekFirst().

Java Examples

Example 1 — Look at the Head of a Queue

Offer two elements and read the head twice with element().

Java
import java.util.ArrayDeque;
import java.util.Queue;

public class ArrayDequeElementDemo {
    public static void main(String[] args) {
        Queue<String> queue = new ArrayDeque<>();
        queue.offer("first");
        queue.offer("second");

        System.out.println("Head: " + queue.element());
        System.out.println("Again: " + queue.element());
        System.out.println("Still there: " + queue);
    }
}

How It Works

  1. Two elements. first was offered first, so it is the head.
  2. Read only. element() returns first without changing the queue.
  3. Same answer twice. The second call returns the same head, because the first call removed nothing.
  4. Proof. Printing the queue still shows both elements.

Example 2 — element() vs peek() on an Empty Queue

Both read the head, but they report "empty" in different ways.

Java
import java.util.ArrayDeque;
import java.util.NoSuchElementException;
import java.util.Queue;

public class ArrayDequeElementEmpty {
    public static void main(String[] args) {
        Queue<Integer> empty = new ArrayDeque<>();

        System.out.println("peek(): " + empty.peek());
        try {
            Integer head = empty.element();
            System.out.println("element(): " + head);
        } catch (NoSuchElementException e) {
            System.out.println("element(): " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. An empty queue. No elements were ever added.
  2. peek returns null. The special-value method simply gives back null.
  3. element throws. The throwing method raises NoSuchElementException, so the line inside try after it never runs.
  4. Pick by intent. Use element() when empty would be a bug, and peek() when empty is expected.

Example 3 — Read, Then Remove, in a Kitchen Queue

The kitchen reads the next order with element() and only removes it when it is done.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;

public class ArrayDequeKitchen {
    public static void main(String[] args) {
        Queue<String> orders = new ArrayDeque<>(Arrays.asList("A-1", "B-2"));

        System.out.println("Preparing " + orders.element());
        orders.remove();
        System.out.println("Preparing " + orders.element());
        orders.remove();

        System.out.println("Done, empty=" + orders.isEmpty());
    }
}

How It Works

  1. Two orders. A-1 is at the head and B-2 behind it.
  2. Read before you remove. element() shows the order being prepared while it stays in the queue.
  3. Finish it. remove() takes the finished order off the head, so B-2 moves up.
  4. All served. After two removals the queue is empty and isEmpty() prints true.

Frequently Asked Questions

It returns the head of the deque without removing it.
Both return the head without removing it. element() throws NoSuchElementException on an empty deque; peek() returns null.
Yes. element() is the Queue name and getFirst() is the Deque name.
No. The deque is unchanged after the call.
When the deque should never be empty at that point, so an exception helps you find a bug quickly.

Next: forEach()

Run an action (lambda) for every element, head to tail.

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

15 people found this page helpful