Java ArrayDeque clone() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, clone() returns a new ArrayDeque that holds the same elements, in the same order, as the original. After cloning, adding to or removing from one deque does not affect the other.

The copy is shallow: the element objects themselves are not copied. If the elements are mutable (such as StringBuilder), changing one through either deque is visible through both. clone() is declared in ArrayDeque, not in Deque, so the variable must have type ArrayDeque to call it.

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 clone on an ArrayDeque variable. No cast is needed.

Java
ArrayDeque<String> original = new ArrayDeque<>(Arrays.asList("a", "b"));
ArrayDeque<String> copy = original.clone();    // [a, b], a separate deque
CallWhat it does
arrayDeque.clone()New ArrayDeque with the same elements.
new ArrayDeque<>(deque)Copy constructor; works with any Collection or Deque variable.
copy == originalfalse: two different objects.
dequeVariable.clone()Does not compile if the variable type is Deque.

ArrayDeque does not override equals, so a.equals(b) is only true when a and b are the same object. To compare contents, compare toString() results or use Arrays.equals(a.toArray(), b.toArray()).

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

// Overrides Object.clone(); ArrayDeque implements Cloneable

Parameters

ParameterDescription
Noneclone() takes no parameters.

Return Value

Return ValueDescription
ArrayDeque<E>A new deque with the same elements, head to tail.

Compare with Similar Methods

Ways to copy a deque:

MethodHow it differs
new ArrayDeque<>(deque)Also shallow, and works when the variable is typed as Deque.
addAll(deque)Copies elements into an existing deque.
toArray()Copies the elements into an array instead.
Deque<E> b = a;No copy at all: both variables point to the same deque.

Java Examples

Example 1 — Clone, Then Change Both

Change the original and the copy separately and compare them.

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

public class ArrayDequeCloneDemo {
    public static void main(String[] args) {
        ArrayDeque<String> original = new ArrayDeque<>(Arrays.asList("a", "b", "c"));
        ArrayDeque<String> copy = original.clone();

        copy.addLast("d");
        original.pollFirst();

        System.out.println("original: " + original);
        System.out.println("copy:     " + copy);
        System.out.println("same object? " + (copy == original));
    }
}

How It Works

  1. ArrayDeque variables. Both are typed as ArrayDeque<String>, because clone() is not part of the Deque interface.
  2. Change the copy. copy.addLast("d") affects only the copy.
  3. Change the original. original.pollFirst() removes a from the original only.
  4. Two objects. == is false, confirming that clone() made a separate deque.

Example 2 — Shallow Copy with Mutable Elements

Clone a deque of StringBuilder objects and edit an element through the original.

Java
import java.util.ArrayDeque;

public class ArrayDequeShallowClone {
    public static void main(String[] args) {
        ArrayDeque<StringBuilder> notes = new ArrayDeque<>();
        notes.add(new StringBuilder("draft"));

        ArrayDeque<StringBuilder> backup = notes.clone();
        notes.peekFirst().append(" v2");

        System.out.println("notes:  " + notes);
        System.out.println("backup: " + backup);
        System.out.println(notes.peekFirst() == backup.peekFirst());
    }
}

How It Works

  1. A mutable element. The deque holds one StringBuilder with the text draft.
  2. Clone copies references. backup is a new deque, but its single slot points to the same builder object.
  3. Edit through one deque. Appending " v2" via notes changes that shared builder, so both deques show [draft v2].
  4. Same element. == between the two heads is true. For a deep copy you would create new builders yourself.

Example 3 — Process a Copy and Compare Contents

Drain a clone to compute a sum while the original stays intact, then see why equals does not compare contents.

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

public class ArrayDequeCloneSnapshot {
    public static void main(String[] args) {
        ArrayDeque<Integer> queue = new ArrayDeque<>(Arrays.asList(5, 6, 7));
        ArrayDeque<Integer> work = queue.clone();

        int sum = 0;
        while (!work.isEmpty()) {
            sum += work.poll();
        }

        System.out.println("sum = " + sum);
        System.out.println("queue still " + queue);
        System.out.println("equals? " + queue.equals(queue.clone()));
        System.out.println("same text? " + queue.toString().equals(queue.clone().toString()));
    }
}

How It Works

  1. Drain the copy. Polling work until it is empty adds 5 + 6 + 7 = 18.
  2. Original intact. queue was never polled, so it still prints [5, 6, 7].
  3. equals is identity here. ArrayDeque inherits equals from Object, so a clone with the same contents is still "not equal".
  4. Compare the text instead. Both deques print as [5, 6, 7], so comparing their toString() results gives true.

Frequently Asked Questions

It returns a new ArrayDeque that contains the same elements in the same order.
Shallow. The deque is new, but the element objects are shared with the original.
clone() is declared in ArrayDeque, not in the Deque interface. Use an ArrayDeque variable, or the copy constructor new ArrayDeque<>(deque).
No. ArrayDeque does not override equals, so it compares object identity. Compare toString() or toArray() results instead.
Both make a shallow copy. Many developers prefer new ArrayDeque<>(deque) because it works with any Collection type and needs no ArrayDeque variable.

Next: contains()

Check whether the deque holds a value (uses equals).

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

12 people found this page helpful