Online Java Compiler By
JavaTpoint.com
import java.util.*; public class HashSetCloneExample2 { public static void main(String[] args) { //Create hash set HashSet
hset = new HashSet
(); //clone elements to hash set hset.add(121); hset.add(111); hset.add(151); //Checking elements in hash set System.out.println("Hash set Elements: "+ hset); //Creating a new cloned set HashSet
clonedSet = new HashSet
(); //Clone the HashSet clonedSet = (HashSet)hset.clone(); //Displaying the new Set after Cloning; System.out.println("The new clone set elements: " + clonedSet); } }
Output