This example shows how to copy HashSet to another HashSet in Java. The example also shows how to append one HashSet to another using the addAll method.
How to copy HashSet to another HashSet in Java?
There are several ways using which you can copy one HashSet to another as given below.
1. Using constructor
You can use the below given HashSet constructor to copy the set object.
1 |
public HashSet(Collection<? extends E> collection) |
This HashSet constructor accepts the Collection object and creates a new HashSet object containing all the elements of the specified collection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.HashSet; public class CopyHashSetJavaExample { public static void main(String[] args) { HashSet<Integer> hSetOriginal = new HashSet<Integer>(); hSetOriginal.add(1); hSetOriginal.add(2); hSetOriginal.add(3); /* * To copy HashSet to another, use the copy constructor */ HashSet<Integer> hSetCopy = new HashSet<Integer>( hSetOriginal ); System.out.println("Original HashSet contains: " + hSetOriginal); System.out.println("Copied HashSet contains: " + hSetCopy); } } |
Output
1 2 |
Original HashSet contains: [1, 2, 3] Copied HashSet contains: [1, 2, 3] |
2. Using the addAll method
We can also use the addAll
method of the HashSet class to copy.
1 |
public boolean addAll(Collection<? extends E> collection) |
The addAll
method adds all the elements of the specified collection object to this set object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
HashSet<Integer> hSetOriginal = new HashSet<Integer>(); hSetOriginal.add(1); hSetOriginal.add(2); hSetOriginal.add(3); //create new empty HashSet HashSet<Integer> hSetCopy = new HashSet<Integer>(); //add all elements of original HashSet to copy hSetCopy.addAll(hSetOriginal); System.out.println("Original HashSet contains: " + hSetOriginal); System.out.println("Copied HashSet contains: " + hSetCopy); |
Output
1 2 |
Original HashSet contains: [1, 2, 3] Copied HashSet contains: [1, 2, 3] |
3. Using the clone method
We can also use the clone
method of the HashSet class to clone the HashSet object.
1 |
public Object clone() |
The clone
method creates a shallow copy of the HashSet object and returns it.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
HashSet<Integer> hSetOriginal = new HashSet<Integer>(); hSetOriginal.add(1); hSetOriginal.add(2); hSetOriginal.add(3); /* * Use the clone method to clone the HashSet */ HashSet<Integer> hSetCopy = (HashSet<Integer>) hSetOriginal.clone(); System.out.println("Original HashSet contains: " + hSetOriginal); System.out.println("Copied HashSet contains: " + hSetCopy); |
Output
1 2 |
Original HashSet contains: [1, 2, 3] Copied HashSet contains: [1, 2, 3] |
Important Note:
All of the above given approaches only copies the element object reference only. None of the methods given above will deep copy the HashSet object. In order words, only the reference to the element objects are copied, not the actual objects.
The copied HashSet elements still point to the original object. That means, if you change the actual element object, the change will be reflected in both the HashSet objects. In order to understand this, let’s have a look at the below example.
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 |
import java.util.HashSet; class Book{ private int bookId; private String bookName; public Book(int bookId, String bookName){ this.bookId = bookId; this.bookName = bookName; } public void setBookName(String bookName){ this.bookName = bookName; } public String toString(){ return "[" + this.bookId + "->" + this.bookName + "]"; } } public class CopyHashSetJavaExample { public static void main(String[] args) { //HashSet of Book class HashSet<Book> hSetOriginal = new HashSet<Book>(); Book b1 = new Book(1, "Java"); Book b2 = new Book(2, "C++"); Book b3 = new Book(3, ".Net"); hSetOriginal.add(b1); hSetOriginal.add(b2); hSetOriginal.add(b3); //copy HashSet using constructor HashSet<Book> hSetCopy = new HashSet<Book>(); hSetCopy.addAll(hSetOriginal); //change the actual element object b1.setBookName("C#"); /* * The change will be reflected in both the HashSet objects * i.e. original as well as copied HashSet. * * It is because only element object references are copied, not * the actual objects (shallow copy or shallow clone) */ System.out.println("Original HashSet contains: " + hSetOriginal); System.out.println("Copied HashSet contains: " + hSetCopy); } } |
Output
1 2 |
Original HashSet contains: [[3->.Net], [1->C#], [2->C++]] Copied HashSet contains: [[3->.Net], [1->C#], [2->C++]] |
As we can see from the output, the change in one of the objects was reflected in both the HashSet objects.
How to append one HashSet to another in Java?
We can use the same addAll
method to append elements of one HashSet to another.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
HashSet<Integer> hSet1 = new HashSet<Integer>(); hSet1.add(1); hSet1.add(2); hSet1.add(3); HashSet<Integer> hSet2 = new HashSet<Integer>(); hSet2.add(4); hSet2.add(5); hSet2.add(6); /* * To append one HashSet to another, use * the addAll method */ //this will append hSet2 to hSet1 hSet1.addAll(hSet2); System.out.println("Appended: " + hSet1); |
Output
1 |
Appended: [1, 2, 3, 4, 5, 6] |
Important Note: Unlike ArrayList or LinkedList, the HashSet class does not guarantee the element order returned by the Iterator so appended elements may come before the elements that were already present in the set when you iterate the HashSet.
This example is a part of the HashSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 HashSet