LinkedHashSet clone() Method in Java with Examples
Last Updated :
18 Nov, 2021
The clone() method of LinkedHashSet class is used to return a shallow copy of the mentioned hash set. It just creates a copy of the set.
--> java.util Package
--> LinkedHashSet Class
--> clone() Method
Syntax:
linked_hash_set.clone()
Parameters: The method does not take any parameters.
Return Type: The method just returns a copy of the LinkedHashSet.
Example:
Java
import java.io.*;
import java.util.LinkedHashSet;
public class GFG {
public static void main(String args[])
{
LinkedHashSet<String> set
= new LinkedHashSet<String>();
set.add( "Welcome" );
set.add( "To" );
set.add( "Geeks" );
set.add( "4" );
set.add( "Geeks" );
System.out.println( "LinkedHashSet: " + set);
LinkedHashSet cloned_set = new LinkedHashSet();
cloned_set = (LinkedHashSet)set.clone();
System.out.println( "The new set: " + cloned_set);
}
}
|
Output:
LinkedHashSet: [Welcome, To, Geeks, 4]
The new set: [Welcome, To, Geeks, 4]
Share your thoughts in the comments
Please Login to comment...