HashSet clone() Method in Java
The Java.util.HashSet.clone() method is used to return a shallow copy of the mentioned hash set. It just creates a copy of the set.
Syntax:
Hash_Set.clone()
Parameters: The method does not take any parameters.
Return Value: The method just returns a copy of the HashSet.
Below program illustrate the Java.util.HashSet.clone() method:
import java.io.*;
import java.util.HashSet;
public class Hash_Set_Demo {
public static void main(String args[])
{
HashSet<String> set = new HashSet<String>();
set.add( "Welcome" );
set.add( "To" );
set.add( "Geeks" );
set.add( "4" );
set.add( "Geeks" );
System.out.println( "HashSet: " + set);
HashSet cloned_set = new HashSet();
cloned_set = (HashSet)set.clone();
System.out.println( "The new set: " + cloned_set);
}
}
|
Output:
HashSet: [4, Geeks, Welcome, To]
The new set: [Geeks, Welcome, To, 4]
Last Updated :
26 Nov, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...