Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java LinkedList clone() Method

Java LinkedList clone() Method

Java LinkedList Java LinkedList


clone() The method is in Java's Object is a method of the class used to create and return a copy of the current object. In LinkedList class, this method is overridden to implement a shallow copy of the list.

Key points:

Method Syntax

public

Object

clone

(

)

Return Value:

exception:


How to use the clone() method

Basic Usage Example

Example

import

java.util.LinkedList

;

public

class

LinkedListCloneExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create the original list

        LinkedList

<
String
originalList
=
new
LinkedList
<>
(
)

;

        originalList.

add

(

"Apple"

)

;

        originalList.

add

(

"Banana"

)

;

        originalList.

add

(

"Cherry"

)

;

       

       

// Clone the list

        LinkedList

<
String
clonedList
=
(
LinkedList
<
String
)

originalList.

clone

(

)

;

       

       

// Print both lists

       

System

.

out

.

println

(

"Original List: "

originalList

)

;

       

System

.

out

.

println

(

"Cloned List: "

clonedList

)

;

   

}

}

Output result:

Original List: [Apple, Banana, Cherry]
Cloned List: [Apple, Banana, Cherry]

Shallow copy vs. deep copy

Characteristics of shallow copy

  1. Element references are the same: The elements in the cloned list and the original list are the same objects
  2. Modifying an element affects both: If an object in the list is modified, both lists are affected

Example

import

java.util.LinkedList

;

class

Fruit

{

   

String

name

;

   

    Fruit

(

String

name

)

{

       

this

.

name

=

name

;

   

}

   

    @Override

   

public

String

toString

(

)

{

       

return

name

;

   

}

}

public

class

ShallowCopyExample

{

   

public

static

void

main

(

String

[

]

args

)

{

        LinkedList

<
Fruit
original
=
new
LinkedList
<>
(
)

;

        original.

add

(

new

Fruit

(

"Apple"

)

)

;

       

        LinkedList

<
Fruit
cloned
=
(
LinkedList
<
Fruit
)

original.

clone

(

)

;

       

       

// Modify an element in the original list

        original.

get

(

0

)

.

name

=

"Orange"

;

       

       

System

.

out

.

println

(

"Original: "

original

)

;

       

System

.

out

.

println

(

"Cloned: "

cloned

)

;

   

}

}

Output result:

Original: [Orange]
Cloned: [Orange]

How to implement a deep copy

If you need a completely independent copy (including elements), you need to manually implement a deep copy:

Example

import

java.util.LinkedList

;

class

Fruit

implements

Cloneable

{

   

String

name

;

   

    Fruit

(

String

name

)

{

       

this

.

name

=

name

;

   

}

   

    @Override

   

protected

Object

clone

(

)

throws

CloneNotSupportedException

{

       

return

super

.

clone

(

)

;

   

}

   

    @Override

   

public

String

toString

(

)

{

       

return

name

;

   

}

}

public

class

DeepCopyExample

{

   

public

static

void

main

(

String

[

]

args

)

throws

CloneNotSupportedException

{

        LinkedList

<
Fruit
original
=
new
LinkedList
<>
(
)

;

        original.

add

(

new

Fruit

(

"Apple"

)

)

;

       

        LinkedList

<
Fruit
deepCloned
=
new
LinkedList
<>
(
)

;

       

for

(

Fruit fruit

:

original

)

{

            deepCloned.

add

(

(

Fruit

)

fruit.

clone

(

)

)

;

       

}

       

       

// Modify an element in the original list

        original.

get

(

0

)

.

name

=

"Orange"

;

       

       

System

.

out

.

println

(

"Original: "

original

)

;

       

System

.

out

.

println

(

"Deep Cloned: "

deepCloned

)

;

   

}

}

Output result:

Original: [Orange]
Deep Cloned: [Apple]

Practical Application Scenarios

  1. Protect original data: When you need to manipulate the list but do not want to modify the original data
  2. Quick copy: When you need to quickly create a list with identical content
  3. Multi-threaded environment: When using independent copies of the same data in different threads

Notes

  1. Type Conversion: clone() returns an Object type and needs to be cast to LinkedList
  2. Element immutability: For immutable objects (such as String), a shallow copy is sufficient
  3. Performance considerations: Cloning a large list incurs some performance overhead
  4. Concurrency issues: If the list is modified during cloning, it may lead to inconsistency

Frequently Asked Questions

Q1: What is the difference between clone() and direct assignment?

Direct assignment(such as list2 = list1) only creates a new reference pointing to the same object, while clone() creates a new independent object.

Q2: Why does clone() return Object instead of LinkedList?

This is the clone() standard design of the method in Java, requiring subclasses to override it and return a more specific type.

Q3: How to clone a LinkedList of custom objects?

Make sure the custom class implements Cloneable interface and overrides the clone() method, then follow the approach in the deep copy example.


Summary

LinkedList 's clone() The method provides a quick way to create a copy of a list, but note that it is a shallow copy. Choose between a shallow copy or implementing a deep copy based on your actual needs, especially when dealing with lists containing mutable objects.

Java LinkedList Java LinkedList