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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
HashSet<Integer> set = new HashSet<>();
set.add( 10 );
set.add( 20 );
set.add( 30 );
set.add( 10 );
set.add( 50 );
set.add( 20 );
HashSet<Integer> copyOfSet = new HashSet<>(set);
System.out.println( "Original HashSet: " + set);
System.out.println( "Copy HashSet: " + copyOfSet);
}
}
|
Output
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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
HashSet<Integer> set = new HashSet<>();
set.add( 10 );
set.add( 20 );
set.add( 30 );
set.add( 10 );
set.add( 50 );
set.add( 20 );
HashSet copyOfSet = new HashSet();
copyOfSet = (HashSet)set.clone();
System.out.println( "Original HashSet: " + set);
System.out.println( "Copy HashSet: " + copyOfSet);
}
}
|
Output
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
import java.util.*;
public class GFG {
public static void main(String[] args) {
HashSet<Integer> set = new HashSet<>();
set.add( 10 );
set.add( 20 );
set.add( 30 );
set.add( 10 );
set.add( 50 );
set.add( 20 );
HashSet<Integer> copyOfSet = new HashSet<>();
copyOfSet.addAll(set);
System.out.println( "Original HashSet: " + set);
System.out.println( "Copy HashSet: " + copyOfSet);
}
}
|
Output
Original HashSet: [50, 20, 10, 30]
Copy HashSet: [50, 20, 10, 30]
Append using addAll method in the already existing HashSet:
Code:
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
HashSet<Integer> set = new HashSet<>();
set.add( 10 );
set.add( 20 );
set.add( 30 );
set.add( 10 );
set.add( 50 );
set.add( 20 );
HashSet<Integer> appendedSet = new HashSet<>();
appendedSet.add( 100 );
appendedSet.add( 200 );
System.out.println( "Before appending :" );
System.out.println( "Original HashSet: " + set);
System.out.println( "Appended HashSet: "
+ appendedSet);
appendedSet.addAll(set);
System.out.println( "After appending" );
System.out.println( "Appended HashSet: "
+ appendedSet);
}
}
|
Output
Before appending :
Original HashSet: [50, 20, 10, 30]
Appended HashSet: [100, 200]
After appending
Appended HashSet: [50, 100, 20, 200, 10, 30]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!