This example shows how to copy one TreeSet to another TreeSet, merge two TreeSet objects and clone a TreeSet object using constructor, the addAll method and the clone method.
How to copy one TreeSet object to another TreeSet object?
We can copy one TreeSet object to another using the copy constructor.
1 |
public TreeSet(Collection<? extends E> collection) |
This TreeSet constructor creates a new TreeSet object containing all the elements of the specified collection object. Since the TreeSet class is a child class of the Collection interface, we can pass the TreeSet object in this constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.TreeSet; public class CopyMergeCloneTreeSetExample { public static void main(String[] args) { TreeSet<Integer> tSet1 = new TreeSet<Integer>(); tSet1.add(1); tSet1.add(2); tSet1.add(3); /* * To copy one TreeSet to another, use * the copy constructor */ TreeSet<Integer> tSet2 = new TreeSet<Integer>( tSet1 ); System.out.println("TreeSet contains: " + tSet1); System.out.println("Copied TreeSet contains: " + tSet2); } } |
Output
1 2 |
TreeSet contains: [1, 2, 3] Copied TreeSet contains: [1, 2, 3] |
As we can see from the output, all the elements of the TreeSet object are copied into the new TreeSet object.
We can also create a new TreeSet using the default constructor, and then use the addAll
method to add all elements of one TreeSet to another.
How to merge two TreeSet objects?
The addAll
method of the TreeSet class adds all the elements of the specified collection object to this TreeSet object.
1 |
public boolean addAll(Collection<? extends E> collection) |
We can use the addAll
method to merge two TreeSet objects as given below.
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 |
import java.util.TreeSet; public class CopyMergeCloneTreeSetExample { public static void main(String[] args) { TreeSet<Integer> tSet1 = new TreeSet<Integer>(); tSet1.add(1); tSet1.add(2); tSet1.add(3); TreeSet<Integer> tSet2 = new TreeSet<Integer>(); tSet1.add(3); tSet1.add(4); tSet1.add(5); /* * To merge two TreeSet objects, use * the addAll method */ tSet1.addAll(tSet2); System.out.println("Merged TreeSet contains: " + tSet1); } } |
Output
1 |
Merged TreeSet contains: [1, 2, 3, 4, 5] |
As we can see from the output, the element “3” is not added while merging. Please note that the elements that are already present in the first TreeSet object will not be added from the second TreeSet. The merged TreeSet object will only contain unique elements.
How to clone a TreeSet object using the clone method?
The clone
method of the TreeSet class creates a shallow copy of the TreeSet object.
1 |
public Object clone() |
1 2 3 4 5 6 7 8 9 10 11 12 |
TreeSet<Integer> tSet = new TreeSet<Integer>(); tSet.add(1); tSet.add(2); tSet.add(3); /* * Use the clone method to clone a TreeSet object */ TreeSet<Integer> tSetCloned = (TreeSet<Integer>) tSet.clone(); System.out.println("Cloned TreeSet contains: " + tSetCloned); |
Output
1 |
Cloned TreeSet contains: [1, 2, 3] |
Please note that the clone
method creates a shallow copy of a TreeSet object, not a deep copy. In other words, only the element references are copied, not the actual objects.
Let’s see an example of 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 |
import java.util.TreeSet; class File implements Comparable<File>{ private int fileId; private String fileName; public File(int fileId, String fileName){ this.fileId = fileId; this.fileName = fileName; } public int getFileId(){ return this.fileId; } public void setFileName(String fileName){ this.fileName = fileName; } public String toString(){ return "[" + this.fileId + "->" + this.fileName + "]"; } public int compareTo(File otherFile) { return this.getFileId() - otherFile.getFileId(); } } public class CopyMergeCloneTreeSetExample { public static void main(String[] args) { TreeSet<File> tSetFiles = new TreeSet<File>(); File file1 = new File(1, "one.txt"); File file2 = new File(2, "two.txt"); tSetFiles.add( file1 ); tSetFiles.add( file2 ); //clone the TreeSet object TreeSet<File> tSetFilesCloned = (TreeSet<File>) tSetFiles.clone(); System.out.println("Original TreeSet: " + tSetFiles); System.out.println("Cloned TreeSet: " + tSetFilesCloned); /* * Adding or removing objects from one does * not affect the other */ tSetFilesCloned.remove(file2); System.out.println("After removing an element from clone"); System.out.println("Original TreeSet: " + tSetFiles); System.out.println("Cloned TreeSet: " + tSetFilesCloned); /* * However, if you change the element object * itself, the change will reflect in both. * * It is because only object references are copied, they * still point to the same objects */ //change the actual object file1.setFileName("changed.txt"); System.out.println("After changing the actual element object"); System.out.println("Original TreeSet: " + tSetFiles); System.out.println("Cloned TreeSet: " + tSetFilesCloned); } } |
Output
1 2 3 4 5 6 7 8 |
Original TreeSet: [[1->one.txt], [2->two.txt]] Cloned TreeSet: [[1->one.txt], [2->two.txt]] After removing an element from clone Original TreeSet: [[1->one.txt], [2->two.txt]] Cloned TreeSet: [[1->one.txt]] After changing the actual element object Original TreeSet: [[1->changed.txt], [2->two.txt]] Cloned TreeSet: [[1->changed.txt]] |
This example is a part of the TreeSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 TreeSet