Java HashSet clone() Method Example

The clone() method of java.util.HashSet class will create a shallow copy of the HashSet.

Signature

public Object clone()

This method creates a shallow copy of this HashSet. The elements themselves are not cloned.

Example

The following example shows the usage of java.util.HashSet.clone() method.

import java.util.HashSet;

public class CloneMethodHashSetExample {
    public static void main(String args[])
    {
        // create an empty HashSet
        HashSet hashset1= new HashSet();
        
        // use add() method to add elements to the HashSet
        hashset1.add("Element1");
        hashset1.add("Element2");
        hashset1.add("Element3");

        //Printing the elements of the HashSet hashset1
        System.out.println("**Elements of the hashset1**");
        for(String temp:hashset1)
        {
            System.out.println(temp);
        }
        
        // create another empty HashSet
        HashSet hashset2 = new HashSet();
        //Shallow copying of hashset1 to hashset2
                hashset2 =(HashSet)hashset1.clone();
        
        //Printing the elements of the HashSet hashset2
        System.out.println("**Elements of the hashset2**");
        for(String temp:hashset2)
        {
            System.out.println(temp);
        }
    }
}

Output

**Elements of the hashset1**
Element1
Element2
Element3
**Elements of the hashset2**
Element1
Element2
Element3

Leave a Reply

Your email address will not be published. Required fields are marked *