In Java, clone() creates a new LinkedList that contains the same elements as the original, in the same order. Adding or removing elements in one list does not change the other.
The copy is shallow: the elements are not cloned, so both lists point to the same objects. For immutable values such as String and Integer this makes no difference. For mutable objects, a change made through one list is visible through the other.
An instance method of java.util.LinkedList (since Java 1.2): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It overrides Object.clone(), and LinkedList implements Cloneable.
Syntax
clone() returns Object, so a cast is needed.
Java
LinkedList<String> list = new LinkedList<>(Arrays.asList("A", "B"));
@SuppressWarnings("unchecked")
LinkedList<String> copy = (LinkedList<String>) list.clone();
LinkedList<String> copy2 = new LinkedList<>(list); // no cast needed
Call
What it does
(LinkedList<E>) list.clone()
A shallow copy; needs a cast and gives an unchecked warning.
new LinkedList<>(list)
The usual way to copy: no cast, no warning.
copy.addAll(list)
Copies into a list that already exists.
list.toArray()
Copies the elements into an array instead.
Most programmers prefer the copy constructor new LinkedList<>(list). It is shorter, needs no cast, and gives the same shallow copy.
Library declarations
Java
// java.util.LinkedList
public Object clone()
Parameters
Parameter
Description
None
clone() takes no parameters.
Return Value
Return Value
Description
Object
A new LinkedList with the same elements. Cast it to LinkedList<E>.
The cast.clone() returns Object, so it is cast to LinkedList<String>. The annotation silences the unchecked warning.
Two lists. The copy starts with red, green and blue, just like the original.
Changes stay separate. Adding "pink" to the original does not add it to the copy, and removing "red" from the copy does not affect the original.
Different objects.original == copy is false, because clone() creates a new list.
Example 2 — See That Mutable Elements Are Shared
Clone a list of StringBuilder objects, change one element, and look at both lists.
Java
import java.util.LinkedList;
public class LinkedListCloneShallow {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
LinkedList<StringBuilder> names = new LinkedList<>();
names.add(new StringBuilder("Ann"));
names.add(new StringBuilder("Bob"));
LinkedList<StringBuilder> copy = (LinkedList<StringBuilder>) names.clone();
copy.get(0).append("-changed");
copy.add(new StringBuilder("Cy"));
System.out.println("names: " + names);
System.out.println("copy: " + copy);
System.out.println("same first element: " + (names.get(0) == copy.get(0)));
}
}
Output
names: [Ann-changed, Bob]
copy: [Ann-changed, Bob, Cy]
same first element: true
How It Works
Mutable elements. A StringBuilder can be changed in place, unlike a String.
The copy shares the objects.copy.get(0) is the very same StringBuilder as names.get(0).
A change shows in both lists. Appending to the shared object makes "Ann-changed" appear in both outputs.
The structure is separate. Adding "Cy" to the copy does not add it to the original, and the last line confirms that the first elements are identical objects.
Example 3 — Copy a List Without clone
Copy a list with the copy constructor and with addAll, and compare the results.
Java
import java.util.Arrays;
import java.util.LinkedList;
public class LinkedListCloneCtor {
public static void main(String[] args) {
LinkedList<Integer> source = new LinkedList<>(Arrays.asList(7, 8, 9));
LinkedList<Integer> viaCtor = new LinkedList<>(source);
LinkedList<Integer> viaAddAll = new LinkedList<>();
viaAddAll.add(1);
viaAddAll.addAll(source);
source.clear();
System.out.println("source: " + source);
System.out.println("viaCtor: " + viaCtor);
System.out.println("viaAddAll: " + viaAddAll);
}
}