Java HashSet.clone() – Examples
In this tutorial, we will learn about the Java HashSet.clone() method, and learn how to use this method to make a shallow copy of this HashSet, with the help of examples.
clone()
HashSet.clone() returns a shallow copy of this HashSet instance: the elements themselves are not cloned, but a new HashSet instance is.
Syntax
The syntax of clone() function is
HashSet.clone()
Returns
The method returns Object. You may need to type cast it to HashSet<T>.
Example 1 – clone()
In this example, we will create a HashSet with String values, and clone this HashSet using clone() method.
Java Program
import java.util.Arrays;
import java.util.HashSet;
public class Example {
public static void main(String[] args) {
HashSet<String> hashSet = new HashSet<String>(Arrays.asList("a", "b", "c"));
System.out.println("Original HashSet : " + hashSet);
HashSet<String> hashSetCloned = (HashSet<String>) hashSet.clone();
System.out.println("Cloned HashSet : " + hashSetCloned);
}
}
Output
Original HashSet : [a, b, c]
Cloned HashSet : [a, b, c]
Any modifications done to the cloned HashSet will not reflect in the original HashSet, since these original and cloned HashSet s are two different instances.
In the following program, we will add elements to the cloned HashSet, and this should not affect the original hashSet.
Java Program
import java.util.Arrays;
import java.util.HashSet;
public class Example {
public static void main(String[] args) {
HashSet<String> hashSet = new HashSet<String>(Arrays.asList("a", "b", "c"));
System.out.println("Original HashSet : " + hashSet);
HashSet<String> hashSetCloned = (HashSet<String>) hashSet.clone();
hashSetCloned.add("d");
hashSetCloned.add("e");
System.out.println("Cloned HashSet : " + hashSetCloned);
}
}
Output
Original HashSet : [a, b, c]
Cloned HashSet : [a, b, c, d, e]
Example 2 – clone() – HashSet with User-defined Objects
In this example, we will define and initialize a HashSet with objects of type Car
. We will clone this HashSet, using clone() method. And following this, we will provide another program to understand the “shallow copy”.
Java Program
import java.util.HashSet;
public class Example {
public static void main(String[] args) {
Car car1 = new Car(1, "Tesla");
Car car2 = new Car(2, "BMW");
Car car3 = new Car(3, "Toyota");
HashSet<Car> hashSet = new HashSet<Car>();
hashSet.add(car1);
hashSet.add(car2);
hashSet.add(car3);
System.out.println("Original HashSet : " + hashSet);
HashSet<Car> hashSetCloned = (HashSet<Car>) hashSet.clone();
System.out.println("Cloned HashSet : " + hashSetCloned);
}
}
class Car {
int id;
String name;
public Car(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return id + "-" + name;
}
}
Output
Original HashSet : [1-Tesla, 3-Toyota, 2-BMW]
Cloned HashSet : [1-Tesla, 3-Toyota, 2-BMW]
If any of the Car objects is modified, the changes effect both original HashSet and cloned HashSet.
In the following program, we will modify the name of Car in car3
, and let us observe how this effects original and cloned HashSet.
Java Program
import java.util.HashSet;
public class Example {
public static void main(String[] args) {
Car car1 = new Car(1, "Tesla");
Car car2 = new Car(2, "BMW");
Car car3 = new Car(3, "Toyota");
HashSet<Car> hashSet = new HashSet<Car>();
hashSet.add(car1);
hashSet.add(car2);
hashSet.add(car3);
HashSet<Car> hashSetCloned = (HashSet<Car>) hashSet.clone();
car3.name = "Audi";
System.out.println("Original HashSet : " + hashSet);
System.out.println("Cloned HashSet : " + hashSetCloned);
}
}
class Car {
int id;
String name;
public Car(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return id + "-" + name;
}
}
Output
Original HashSet : [1-Tesla, 3-Audi, 2-BMW]
Cloned HashSet : [1-Tesla, 3-Audi, 2-BMW]
Hence, the shallow copy by clone() method.
Conclusion
In this Java Tutorial, we have learnt the syntax of Java HashSet.clone() method, and also learnt how to use this method with the help of examples.