HashSet Object clone() method in java

Let’s learn HashSet Object clone() method in java.

HashSet Object clone() method in java

clone() method returns a shallow copy of this HashSet instance: the elements themselves are not cloned.

Syntax:

public Object clone()

Parameters:

This method does not take any parameters.

Returns:

a shallow copy of this set.

Also read – HashSet in java

Now let’s see example on HashSet Object clone() method.

import java.util.HashSet;
public class HashSetObjectCloneMethodExample
{
   public static void main(String[] args)
   {
      HashSet<String> hs = new HashSet<String>();
      hs.add("Welcome");
      hs.add("hello");
      hs.add("world");
      hs.add("core");
      hs.add("java");
      System.out.println("HashSet before using clone() method: " + hs);
      // create new cloned HashSet
      HashSet<String> objClone = new HashSet<String>();
      // clone HashSet using clone() method
      objClone = (HashSet)hs.clone();
      // print new HashSet after cloning
      System.out.println("HashSet after using clone() method: " + objClone);
   }
}


Output:

HashSet before using clone() method: [core, world, java, Welcome, hello]
HashSet after using clone() method: [core, Welcome, world, java, hello]