Home Java ArrayDeque Methods clone() Java ArrayDeque clone() Method 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.
ArrayDeque<String> original = new ArrayDeque<>(Arrays.asList("a", "b"));
ArrayDeque<String> copy = original.clone(); // [a, b], a separate deque Call What 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.util.ArrayDeque<E>
public ArrayDeque<E> clone()
// Overrides Object.clone(); ArrayDeque implements Cloneable Parameters Parameter Description None clone() takes no parameters.
Return Value Return Value Description ArrayDeque<E>A new deque with the same elements, head to tail.
Compare with Similar Methods Ways to copy a deque:
Method How 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 1. Independent copy 2. Shallow copy 3. Work on a copy Example 1 — Clone, Then Change Both Change the original and the copy separately and compare them.
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));
}
} original: [b, c]
copy: [a, b, c, d]
same object? false How It Works ArrayDeque variables. Both are typed as ArrayDeque<String>, because clone() is not part of the Deque interface.Change the copy. copy.addLast("d") affects only the copy.Change the original. original.pollFirst() removes a from the original only.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.
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());
}
} notes: [draft v2]
backup: [draft v2]
true How It Works A mutable element. The deque holds one StringBuilder with the text draft.Clone copies references. backup is a new deque, but its single slot points to the same builder object.Edit through one deque. Appending " v2" via notes changes that shared builder, so both deques show [draft v2].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.
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()));
}
} sum = 18
queue still [5, 6, 7]
equals? false
same text? true How It Works Drain the copy. Polling work until it is empty adds 5 + 6 + 7 = 18.Original intact. queue was never polled, so it still prints [5, 6, 7].equals is identity here. ArrayDeque inherits equals from Object, so a clone with the same contents is still "not equal".Compare the text instead. Both deques print as [5, 6, 7], so comparing their toString() results gives true.Frequently Asked Questions What does ArrayDeque clone() do? It returns a new ArrayDeque that contains the same elements in the same order.
Is ArrayDeque clone() a deep or shallow copy? Shallow. The deque is new, but the element objects are shared with the original.
Why can I not call clone() on a Deque variable? clone() is declared in ArrayDeque, not in the Deque interface. Use an ArrayDeque variable, or the copy constructor new ArrayDeque<>(deque).
Does copy.equals(original) return true? No. ArrayDeque does not override equals, so it compares object identity. Compare toString() or toArray() results instead.
Should I use clone() or the copy constructor? Both make a shallow copy. Many developers prefer new ArrayDeque<>(deque) because it works with any Collection type and needs no ArrayDeque variable.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion