Java Utility Library

Java LinkedList - clone() Method



The java.util.LinkedList.clone() method returns a shallow copy of the given LinkedList.

Syntax

public Object clone()

Parameters

No parameter is required.

Return Value

Returns a shallow copy of the given LinkedList instance.

Exception

NA

Example:

In the example below, the java.util.LinkedList.clone() method is used to create a shallow copy of the LinkedList called MyList.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a linkedlist
    LinkedList<Integer> MyList = new LinkedList<Integer>();

    //populating linkedlist
    MyList.add(10);
    MyList.add(20);
    MyList.add(30);
    
    LinkedList NewList = new LinkedList();
    NewList = (LinkedList) MyList.clone();

    //printing linkedlist
    System.out.println("NewList contains: " + NewList);
  }
}

The output of the above code will be:

NewList contains: [10, 20, 30]

❮ Java.util - LinkedList