Java LinkedList clone() Method with Examples

In the previous article, we have discussed about Java LinkedList clear() Method with Examples

In this article we are going to see the use of Java LinkedList clone() method along with suitable examples.

Java LinkedList clone() Method with Examples

This java.util.LinkedList.clone() method is used to make copy of all the elements of the same LinkedList.

It return the same value as it copies all the elements from the LinkedList and makes a clone.

Syntax:

LinkedListName.clone()

Where,

  • LinkedListName refers to the name of your LinkedList.

Let’s see different examples to understand it more clearly.

Example-1: Java LinkedList clone() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add string elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Clone the same LinkedList using clone( ) method.
  • Print the cloned LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("ram");
        l.add("shyam");
        l.add("gyan");
        l.add("dhyan");
        l.add("vyan");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // cloning the same above LinkedList
       LinkedList<String> clone = (LinkedList<String>)l.clone();
       System.out.println("Cloned LinkedList: " + clone);
    }
}
Output:

The elements of LinkedList are: [ram, shyam, gyan, dhyan, vyan]
Cloned LinkedList: [ram, shyam, gyan, dhyan, vyan]

Example-2: Java LinkedList clone() Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Clone the same LinkedList using clone( ) method.
  • Print the cloned LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(1);
        l.add(2);
        l.add(3);
        l.add(4);
        l.add(5);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
       // cloning the same above LinkedList
       LinkedList<Integer> clone = (LinkedList<Integer>)l.clone();
       System.out.println("Cloned LinkedList: " + clone);
    }
}
Output:

The elements of LinkedList are: [1, 2, 3, 4, 5]
Cloned LinkedList: [1, 2, 3, 4, 5]

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output.

Related Java Programs: