Thursday, June 11, 2026
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

3 COMMENTS

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS