Java LinkedList clone() Method |
\n\n\n
clone() is a method of the Object class in Java, used to create and return a copy of the current object. In the LinkedList class, this method is overridden to implement a shallow copy of the list.
Key Points:
\n- \n
- Returns a new
LinkedListobject \n - The new list contains the same elements as the original list \n
- The elements themselves are not copied (shallow copy) \n
Method Syntax
\npublic Object clone()\nReturn Value:
\n- \n
- Returns a shallow copy of this
LinkedList\n
Exceptions:
\n- \n
- Does not throw any exceptions \n
\n
How to Use the clone() Method
\nBasic Usage Example
\nInstance
\nimport java.util.LinkedList;\n\npublic class LinkedListCloneExample {\n\npublic static void main(String[] args){\n\n// Create the original linked list\n\n LinkedList originalList =new LinkedList();\n\n originalList.add("Apple");\n\n originalList.add("Banana");\n\n originalList.add("Cherry");\n\n// Clone the linked list\n\n LinkedList clonedList =(LinkedList) originalList.clone();\n\n// Print the two linked lists\n\nSystem.out.println("Original List: "+ originalList);\n\nSystem.out.println("Cloned List: "+ clonedList);\n\n}\n\n}\nOutput:
\nOriginal List: [Apple, Banana, Cherry]
Cloned List: [Apple, Banana, Cherry]
\n
Shallow Copy vs Deep Copy
\nCharacteristics of Shallow Copy
\n- \n
- Same element references: The elements in the cloned list and the original list refer to the same objects \n
- Modifying elements affects both: If an object within the list is modified, both lists will be affected \n
Instance
\nimport java.util.LinkedList;\n\nclass Fruit {\n\nString name;\n\nFruit(String name){\n\nthis.name= name;\n\n}\n\n@Override\n\npublic String toString(){\n\nreturn name;\n\n}\n\n}\n\npublic class ShallowCopyExample {\n\npublic static void main(String[] args){\n\n LinkedList original =new LinkedList();\n\n original.add(new Fruit("Apple"));\n\nLinkedList cloned =(LinkedList) original.clone();\n\n// Modify elements in the original linked list\n\n original.get(0).name="Orange";\n\nSystem.out.println("Original: "+ original);\n\nSystem.out.println("Cloned: "+ cloned);\n\n}\n\n}\nOutput:
\nOriginal:
Cloned:
How to Implement Deep Copy
\nIf you need a completely independent copy (including elements), you need to manually implement deep copy:
\nInstance
\nimport java.util.LinkedList;\n\nclass Fruit implements Cloneable{\n\nString name;\n\nFruit(String name){\n\nthis.name= name;\n\n}\n\n@Override\n\nprotected Object clone()throws CloneNotSupportedException{\n\nreturn super.clone();\n\n}\n\n@Override\n\npublic String toString(){\n\nreturn name;\n\n}\n\n}\n\npublic class DeepCopyExample {\n\npublic static void main(String[] args)throws CloneNotSupportedException{\n\n LinkedList original =new LinkedList();\n\n original.add(new Fruit("Apple"));\n\nLinkedList deepCloned =new LinkedList();\n\nfor(Fruit fruit : original){\n\n deepCloned.add((Fruit) fruit.clone());\n\n}\n\n// Modify elements in the original linked list\n\n original.get(0).name="Orange";\n\nSystem.out.println("Original: "+ original);\n\nSystem.out.println("Deep Cloned: "+ deepCloned);\n\n}\n\n}\nOutput:
\nOriginal:
Deep Cloned:
\n
Practical Application Scenarios
\n- \n
- Protecting original data: When you need to manipulate the list without modifying the original data \n
- Quick duplication: When you need to quickly create a list with identical content \n
- Multithreading environments: Using independent copies of the same data across different threads \n
\n
Precautions
\n- \n
- Type casting:
clone()returns anObjecttype, which needs to be cast toLinkedList\n - Element immutability: For immutable objects (like
String), a shallow copy is sufficient \n - Performance considerations: Cloning large lists incurs some performance overhead \n
- Concurrency issues: Modifying the list during cloning may lead to inconsistencies \n
\n
Frequently Asked Questions
\nQ1: What is the difference between the clone() method and direct assignment?
\nDirect assignment (e.g., list2 = list1) merely creates a new reference pointing to the same object, whereas clone() creates a new, independent object.
Q2: Why does the clone() method return Object instead of LinkedList?
\nThis is the standard design for the clone() method in Java. Subclasses must override it and can return a more specific type.
Q3: How do I clone a LinkedList containing custom objects?
\nEnsure that your custom class implements the Cloneable interface and overrides the clone() method, then proceed with operations as shown in the deep copy example.
\n
Summary
\nThe clone() method of LinkedList provides a quick way to create a copy of the list, but note that it performs a shallow copy. Choose between shallow copying or implementing deep copy based on your actual requirements, especially when dealing with lists containing mutable objects.
YouTip