Java HashSet.clone()

Syntax

HashSet.clone() has the following syntax.

public Object clone()

Example

In the following code shows how to use HashSet.clone() method.


//from   w  w  w  .java2  s  . c om

import java.util.HashSet;

public class Main {
   public static void main(String args[]) {
      // create two hash sets      
    HashSet <String>  cloneset = new HashSet <String> ();
            
      HashSet<String>  newset = new HashSet <String> ();
                  
      // populate hash set
      newset.add("Learning"); 
      newset.add("from");
      newset.add("java2s.com");  
      
      // clone the hash set
      cloneset=(HashSet)newset.clone();

      System.out.println("Hash set values: "+ newset);      
      System.out.println("Clone Hash set values: "+ cloneset);
   }    
}

The code above generates the following result.