Friday, September 5, 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
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6636 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS