Java Utility Library

Java ArrayDeque - clone() Method



The java.util.ArrayDeque.clone() method returns a copy of the given deque.

Syntax

public ArrayDeque<E> clone()

Here, E is the type of element maintained by the container.


Parameters

No parameter is required.

Return Value

Returns a copy of the given deque instance.

Exception

NA

Example:

In the example below, the java.util.ArrayDeque.clone() method is used to create a copy of the deque called MyDeque.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating an ArrayDeque
    ArrayDeque<Integer> MyDeque = new ArrayDeque<Integer>();

    //populating ArrayDeque
    MyDeque.add(10);
    MyDeque.add(20);
    MyDeque.add(30);
    
    ArrayDeque NewDeque = new ArrayDeque();
    NewDeque = MyDeque.clone();

    //printing ArrayDeque
    System.out.println("NewDeque contains: " + NewDeque);
  }
}

The output of the above code will be:

NewDeque contains: [10, 20, 30]

❮ Java.util - ArrayDeque