The LinkedList.clone()
method in Java is used to create a shallow copy of a LinkedList
. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality.
Table of Contents
- Introduction
clone
Method Syntax- Examples
- Cloning a LinkedList
- Verifying Shallow Copy
- Conclusion
Introduction
The LinkedList.clone()
method is a member of the LinkedList
class in Java. It allows you to create a shallow copy of the LinkedList
, meaning that the new list will have the same elements as the original, but the elements themselves will not be duplicated (only their references will be copied).
clone Method Syntax
The syntax for the clone
method is as follows:
public Object clone()
- The method does not take any parameters.
- The method returns an
Object
which should be cast toLinkedList<E>
.
Examples
Cloning a LinkedList
The clone
method can be used to create a shallow copy of a LinkedList
.
Example
import java.util.LinkedList;
public class CloneExample {
public static void main(String[] args) {
// Creating a LinkedList of Strings
LinkedList<String> list = new LinkedList<>();
// Adding elements to the LinkedList
list.add("Apple");
list.add("Banana");
list.add("Orange");
// Cloning the LinkedList
LinkedList<String> clonedList = (LinkedList<String>) list.clone();
// Printing the original and cloned LinkedLists
System.out.println("Original LinkedList: " + list);
System.out.println("Cloned LinkedList: " + clonedList);
}
}
Output:
Original LinkedList: [Apple, Banana, Orange]
Cloned LinkedList: [Apple, Banana, Orange]
Verifying Shallow Copy
To verify that the clone
method creates a shallow copy, you can check if changes to the elements in the original list are reflected in the cloned list.
Example
import java.util.LinkedList;
public class ShallowCopyExample {
public static void main(String[] args) {
// Creating a LinkedList of custom objects
LinkedList<StringBuilder> list = new LinkedList<>();
// Adding elements to the LinkedList
list.add(new StringBuilder("Apple"));
list.add(new StringBuilder("Banana"));
list.add(new StringBuilder("Orange"));
// Cloning the LinkedList
LinkedList<StringBuilder> clonedList = (LinkedList<StringBuilder>) list.clone();
// Modifying an element in the original list
list.get(0).append(" Pie");
// Printing the original and cloned LinkedLists
System.out.println("Original LinkedList: " + list);
System.out.println("Cloned LinkedList: " + clonedList);
}
}
Output:
Original LinkedList: [Apple Pie, Banana, Orange]
Cloned LinkedList: [Apple Pie, Banana, Orange]
Conclusion
The LinkedList.clone()
method in Java provides a way to create a shallow copy of a LinkedList
. By understanding how to use this method, you can efficiently duplicate collections of objects in your Java applications. However, it is important to note that the clone
method creates a shallow copy, meaning that changes to the objects in the original list will be reflected in the cloned list since both lists hold references to the same objects.