HashSet is used to store distinct values in Java. HashSet stores the elements in random order, so there is no guarantee of the elements’ order. The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance.
We can copy or append a HashSet to another HashSet. There is a couple of ways to copy HashSet or append HashSet to another HashSet in Java:
- Using HashSet constructor
- Using clone() method
- Using the addAll() method
Method 1: Using the HashSet constructor
Using the constructor, we can copy the original HashSet to another HashSet bypassing the original HashSet to the constructor.
// passing the original HashSet to the constructor HashSet<Integer> copySet = new HashSet<>(originalSet)
Code:
Java
// Java program to copy a HashSet to // another HashSet using the constructor import java.util.*; public class GFG { public static void main(String[] args) { // New HashSet HashSet<Integer> set = new HashSet<>(); // Add elements to original set set.add( 10 ); set.add( 20 ); set.add( 30 ); set.add( 10 ); set.add( 50 ); set.add( 20 ); // Make a new HashSet and copy all elements of // original HashSet using constructor HashSet<Integer> copyOfSet = new HashSet<>(set); // Print original HashSet System.out.println( "Original HashSet: " + set); // Print Copy HashSet System.out.println( "Copy HashSet: " + copyOfSet); } } |
Original HashSet: [50, 20, 10, 30] Copy HashSet: [50, 20, 10, 30]
Method 2: Using the clone method
Using the clone method, we can copy all elements of the original HashSet to another HashSet in Java.
Note: The order of elements may be the same or may not be the same. So there is no guarantee of the order.
Code:
Java
// Java program to copy a HashSet to // another HashSet using clone method in Java import java.util.*; public class GFG { public static void main(String[] args) { // New empty HashSet HashSet<Integer> set = new HashSet<>(); // Add elements to set set.add( 10 ); set.add( 20 ); set.add( 30 ); set.add( 10 ); set.add( 50 ); set.add( 20 ); // Create a new HashSet to copy the original HashSet HashSet copyOfSet = new HashSet(); // Copy HashSet using clone method copyOfSet = (HashSet)set.clone(); // Print original HashSet System.out.println( "Original HashSet: " + set); // Print Copy HashSet System.out.println( "Copy HashSet: " + copyOfSet); } } |
Original HashSet: [50, 20, 10, 30] Copy HashSet: [50, 10, 20, 30]
Method 3: Using the addAll method
We can use the addAll() method to copy or to append a HashSet to a another HashSet. addAll method in Java add all elements to the HashSet.
Note: The order of elements may be the same or may not be the same. So there is no guarantee of the order.
Code:
Java
// Java program to copy a HashSet to // another HashSet using addAll method in Java import java.util.*; public class GFG { public static void main(String[] args) { // New empty HashSet HashSet<Integer> set = new HashSet<>(); // Add elements to set set.add( 10 ); set.add( 20 ); set.add( 30 ); set.add( 10 ); set.add( 50 ); set.add( 20 ); // Create a new HashSet to copy the original HashSet HashSet<Integer> copyOfSet = new HashSet<>(); // Copy HashSet using addAll method copyOfSet.addAll(set); // Print original HashSet System.out.println( "Original HashSet: " + set); // Print Copy HashSet System.out.println( "Copy HashSet: " + copyOfSet); } } |
Original HashSet: [50, 20, 10, 30] Copy HashSet: [50, 20, 10, 30]
Append using addAll method in the already existing HashSet:
Code:
Java
// Java program to copy a HashSet to another // HashSet using addAll method in Java import java.util.*; public class GFG { public static void main(String[] args) { // New empty HashSet HashSet<Integer> set = new HashSet<>(); // Add elements to set set.add( 10 ); set.add( 20 ); set.add( 30 ); set.add( 10 ); set.add( 50 ); set.add( 20 ); // Create a new HashSet to append a HashSet HashSet<Integer> appendedSet = new HashSet<>(); // Add elements to appendedSet appendedSet.add( 100 ); appendedSet.add( 200 ); System.out.println( "Before appending :" ); // Print original HashSet System.out.println( "Original HashSet: " + set); // Print appendedSet before append System.out.println( "Appended HashSet: " + appendedSet); // Add all elements of set HashSet to appendedSet // using addAll method appendedSet.addAll(set); System.out.println( "After appending" ); // Print appendedSet after append System.out.println( "Appended HashSet: " + appendedSet); } } |
Before appending : Original HashSet: [50, 20, 10, 30] Appended HashSet: [100, 200] After appending Appended HashSet: [50, 100, 20, 200, 10, 30]