Friday, October 10, 2025
HomeLanguagesJavaLinkedHashSet clone() Method in Java with Examples

LinkedHashSet clone() Method in Java with Examples

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]

 

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS