Java ArrayDeque add() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, add(e) puts an element at the tail (the end) of an ArrayDeque and returns true. It does exactly what addLast(e) does; the only difference is that add reports success with a boolean, because it comes from the Collection and Queue interfaces.

Used as a queue, add is "join the back of the line". Elements come out of the head in the order they were added. Duplicates are allowed, so adding the same value twice stores it twice.

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 add on the deque with the element to append.

Java
Deque<String> deque = new ArrayDeque<>();
boolean ok = deque.add("first");    // true, deque = [first]
deque.add("second");                // deque = [first, second]
CallWhat it does
deque.add(e)Appends e at the tail and returns true.
deque.addLast(e)Same effect, but returns nothing (void).
deque.offer(e)Same effect for ArrayDeque, also returns true.
deque.add(null)Throws NullPointerException; nothing is added.

On a capacity-limited queue, add throws IllegalStateException when the queue is full. An ArrayDeque has no capacity limit, so for it that case never happens and add always returns true.

Library declarations
Java
// java.util.ArrayDeque<E>
public boolean add(E e)

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

Parameters

ParameterDescription
eRequired. The element to add at the tail. Its type is the deque's element type E. Must not be null.

Return Value

Return ValueDescription
booleanAlways true for an ArrayDeque, meaning the deque changed.

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

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 — Add People to a Line

Three names join a line with add(). The first name added stays at the front.

Java
import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeAddDemo {
    public static void main(String[] args) {
        Deque<String> line = new ArrayDeque<>();
        line.add("Ana");
        line.add("Ben");
        boolean added = line.add("Cara");

        System.out.println("Added Cara? " + added);
        System.out.println("Line: " + line);
        System.out.println("Front: " + line.peekFirst());
        System.out.println("Back: " + line.peekLast());
    }
}

How It Works

  1. Start empty. new ArrayDeque<>() creates a deque with no elements, stored in a variable of the interface type Deque<String>.
  2. Each add goes to the back. Ana is added first, then Ben behind her, then Cara behind Ben, so the deque prints as [Ana, Ben, Cara].
  3. The return value. The third call stores its result in added. For an ArrayDeque it is always true.
  4. Both ends. peekFirst() shows Ana, the earliest addition, and peekLast() shows Cara, the latest one.

Example 2 — Duplicates Are Allowed

Add the same score twice. Unlike a set, a deque keeps both copies, and add() returns true every time.

Java
import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeAddDuplicates {
    public static void main(String[] args) {
        Deque<Integer> scores = new ArrayDeque<>();
        System.out.println(scores.add(10));
        System.out.println(scores.add(10));
        System.out.println(scores.add(7));

        System.out.println(scores + " size=" + scores.size());
    }
}

How It Works

  1. Autoboxing. The deque holds Integer objects, so the int literal 10 is boxed to an Integer before it is stored.
  2. A second 10 is still added. A deque does not check for duplicates, so the second add(10) also changes the deque and prints true.
  3. Insertion order is kept. The deque prints [10, 10, 7]: the values in the order they were added, head to tail.
  4. Contrast with a set. A HashSet would return false for the repeated 10 and keep only two values; here size() reports 3.

Example 3 — What Happens When You Add null

Try to add null and catch the exception. The deque is left exactly as it was.

Java
import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeAddNull {
    public static void main(String[] args) {
        Deque<String> tasks = new ArrayDeque<>();
        tasks.add("write");

        try {
            tasks.add(null);
        } catch (NullPointerException e) {
            System.out.println("Rejected: " + e.getClass().getSimpleName());
        }

        System.out.println("Tasks: " + tasks);
        System.out.println("Size: " + tasks.size());
    }
}

How It Works

  1. One valid element. tasks starts with a single task, "write".
  2. null is checked first. add(null) throws NullPointerException before anything is stored. The catch block prints the exception's simple class name.
  3. Nothing changed. After the failed call the deque still prints [write] with size 1.
  4. Why the rule exists. Methods like poll() and peek() return null to mean "empty". If null could be an element, that answer would be ambiguous.

Frequently Asked Questions

It appends the element at the tail (end) of the deque and returns true.
They put the element in the same place. add() returns a boolean (always true for ArrayDeque) and addLast() returns void.
No. ArrayDeque has no capacity limit, so add() always returns true. If it cannot add the element, it throws an exception instead.
No. add(null) throws NullPointerException. Use LinkedList if you really need null elements.
For ArrayDeque they behave the same. offer() is the usual choice in queue code because, on capacity-limited queues, it returns false instead of throwing.

Next: addAll()

Append every element of another collection at the tail.

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

7 people found this page helpful