Open In App

LinkedHashSet clone() Method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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




// Java Program to illustrate clone() Method
// of LinkedHashSet Class
  
// Importing required classes
import java.io.*;
import java.util.LinkedHashSet;
  
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
        // Creating an empty LinkedHashSet
        LinkedHashSet<String> set
            = new LinkedHashSet<String>();
  
        // Adding elements into the Set
        // using add() method
        set.add("Welcome");
        set.add("To");
        set.add("Geeks");
        set.add("4");
        set.add("Geeks");
  
        // Displaying the LinkedHashSet
        System.out.println("LinkedHashSet: " + set);
  
        // Creating a new cloned Set
        LinkedHashSet cloned_set = new LinkedHashSet();
  
        // Cloning the above Set
        // using clone() method
        cloned_set = (LinkedHashSet)set.clone();
  
        // Displaying the new Set after Cloning
        System.out.println("The new set: " + cloned_set);
    }
}


Output: 

LinkedHashSet: [Welcome, To, Geeks, 4]
The new set: [Welcome, To, Geeks, 4]

 



Last Updated : 18 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads