This example shows how to copy, merge, or clone LinkedHashSet in Java. This example also shows how to copy, merge, or clone LinkedHashSet using constructor, addAll method, and clone method.
How to copy one LinkedHashSet to another LinkedHashSet object (clone LinkedHashSet)?
There are several ways using which we can copy one linked hash set object to another as given below.
1. Using the constructor
The LinkedHashSet class in Java provides a constructor that accepts a collection object.
1 |
public LinkedHashSet(Collection<? extends E> collection) |
This constructor creates a new LinkedHashSet object containing all elements of the specified collection object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.LinkedHashSet; public class CopyMergeCloneLinkedHashSet { public static void main(String[] args) { LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("yellow"); lhSetColors.add("white"); lhSetColors.add("black"); /* * Copy LinkedHashSet using the constructor */ LinkedHashSet<String> lhSetColorsCopy = new LinkedHashSet<String>( lhSetColors ); System.out.println("Original LinkedHashSet: " + lhSetColors); System.out.println("Copied LinkedHashSet: " + lhSetColorsCopy); } } |
Output
1 2 |
Original LinkedHashSet: [yellow, white, black] Copied LinkedHashSet: [yellow, white, black] |
2. Using the addAll method
We can also create an empty LinkedHashSet object and then use the addAll
method to add all elements of one linked hash set object to another as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("yellow"); lhSetColors.add("white"); lhSetColors.add("black"); //create empty object LinkedHashSet<String> lhSetColorsCopy = new LinkedHashSet<String>(); //use addAll method to add all elements of the original object lhSetColorsCopy.addAll(lhSetColors); System.out.println("Original LinkedHashSet: " + lhSetColors); System.out.println("Copied LinkedHashSet: " + lhSetColorsCopy); |
Output
1 2 |
Original LinkedHashSet: [yellow, white, black] Copied LinkedHashSet: [yellow, white, black] |
3. Using the clone method
The LinkedHashSet clone
method can be used to clone the linked hash set object.
1 |
public Object clone() |
The clone
method returns a shallow copy of this linked hash set object.
1 2 3 4 5 6 7 8 9 10 11 |
LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("yellow"); lhSetColors.add("white"); lhSetColors.add("black"); //use the clone method LinkedHashSet<String> lhSetColorsCloned = (LinkedHashSet<String>) lhSetColors.clone(); System.out.println("Original LinkedHashSet: " + lhSetColors); System.out.println("Cloned LinkedHashSet: " + lhSetColorsCloned); |
Output
1 2 |
Original LinkedHashSet: [yellow, white, black] Cloned LinkedHashSet: [yellow, white, black] |
Important Note:
Above given all three approaches create a shallow copy of the LinkedHashSet object. That means only the element references are copied, not the actual objects. That is why it is called a shallow copy, not a deep copy or deep clone.
Let’s see an example to understand what is a shallow copy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import java.util.LinkedHashSet; class Student{ private int id; private String name; public Student(int id, String name){ this.id = id; this.name = name; } public void setName(String name){ this.name = name; } public String toString(){ return "[" + this.id + " -> " + this.name + "]"; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (id != other.id) return false; return true; } } public class CopyMergeCloneLinkedHashSet { public static void main(String[] args) { LinkedHashSet<Student> lhSetStudents = new LinkedHashSet<Student>(); Student student1 = new Student(1, "Emily"); Student student2 = new Student(2, "Raj"); Student student3 = new Student(3, "John"); lhSetStudents.add(student1); lhSetStudents.add(student2); lhSetStudents.add(student3); //copy to another LinkedHashSet<Student> lhSetStudentsCloned = (LinkedHashSet<Student>) lhSetStudents.clone(); System.out.println("Original: " + lhSetStudents); System.out.println("Cloned: " + lhSetStudentsCloned); /* * Adding or removing elements to either * of the objects does not change the other */ //adding an element to original lhSetStudents.add(new Student(4, "Ryan")); //removing an element from the clone lhSetStudentsCloned.remove(student1); System.out.println("After adding, removing elements"); System.out.println("Original: " + lhSetStudents); System.out.println("Cloned: " + lhSetStudentsCloned); /* * But when you change the original element object, the * change will be reflected in both. * * That is because only the reference to the original * object is copied, not the actual element object */ //change element object student2.setName("AAAAAAAA"); System.out.println("After changing element object"); System.out.println("Original: " + lhSetStudents); System.out.println("Cloned: " + lhSetStudentsCloned); } } |
Output
1 2 3 4 5 6 7 8 |
Original: [[1 -> Emily], [2 -> Raj], [3 -> John]] Cloned: [[1 -> Emily], [2 -> Raj], [3 -> John]] After adding, removing elements Original: [[1 -> Emily], [2 -> Raj], [3 -> John], [4 -> Ryan]] Cloned: [[2 -> Raj], [3 -> John]] After changing element object Original: [[1 -> Emily], [2 -> AAAAAAAA], [3 -> John], [4 -> Ryan]] Cloned: [[2 -> AAAAAAAA], [3 -> John]] |
How to merge two LinkedHashSet objects (append one LinkedHashSet to another)?
We can use the addAll
method to merge two LinkedHashSet objects or append elements of one LinkedHashSet object to another LinkedHashSet object.
1 |
boolean addAll(Collection<? extends E> collection) |
The addAll
method adds all the elements of the specified collection to this set object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
LinkedHashSet<String> lhSetColors1 = new LinkedHashSet<String>(); lhSetColors1.add("yellow"); lhSetColors1.add("white"); lhSetColors1.add("black"); LinkedHashSet<String> lhSetColors2 = new LinkedHashSet<String>(); lhSetColors2.add("yellow"); lhSetColors2.add("red"); lhSetColors2.add("green"); /* * To merge two LinkedHashSet objects or * append LinkedHashSet object to another, use the * addAll method */ lhSetColors1.addAll(lhSetColors2); System.out.println("Merged LinkedHashSet: " + lhSetColors1); |
Output
1 |
Merged LinkedHashSet: [yellow, white, black, red, green] |
As we can see from the output, all the elements of the lhSetColors2 linked hash set object were appended to the lhSetColors1 object. Please also note that the element “yellow” was not appended when we merged the two because it was already present in the set object.
This example is a part of the LinkedHashSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashSet