Online Java Compiler By
JavaTpoint.com
import java.util.*; public class HashSetCloneExample3 { public static void main(String[] args) { //Create an empty HashSet HashSet
hset1 = new HashSet<>(); //Add elements to the HashSet hset1.add("JTP"); hset1.add("SSSIT"); hset1.add("DFC"); //Print the elements of the HashSet System.out.println("Elements of the hset1:"); for(String temp:hset1) { System.out.println(temp); } //Create another HashSet HashSet
hset2 = new HashSet<>(); //Shallow copying of hset1 to hset2 hset2 =(HashSet)hset1.clone(); //Print the elements of the HashSet System.out.println("Elements of the hset2 after clone() operation:"); for(String temp:hset2) { System.out.println(temp); } } }
Output