Java HashSet clone() Method

The HashSet.clone() method in Java is used to create a shallow copy of a HashSet. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. clone Method Syntax
  3. Examples
    • Cloning a HashSet
    • Verifying Shallow Copy
  4. Conclusion

Introduction

The HashSet.clone() method is a member of the HashSet class in Java. It allows you to create a shallow copy of the HashSet, meaning that the new set will have the same elements as the original, but the elements themselves will not be duplicated (only their references will be copied).

clone Method Syntax

The syntax for the clone method is as follows:

public Object clone()
  • The method does not take any parameters.
  • The method returns an Object which should be cast to HashSet<E>.

Examples

Cloning a HashSet

The clone method can be used to create a shallow copy of a HashSet.

Example

import java.util.HashSet;

public class CloneExample {
    public static void main(String[] args) {
        // Creating a HashSet of Strings
        HashSet<String> languages = new HashSet<>();

        // Adding elements to the HashSet
        languages.add("Java");
        languages.add("Python");
        languages.add("C");

        // Cloning the HashSet
        HashSet<String> clonedLanguages = (HashSet<String>) languages.clone();

        // Printing the original and cloned HashSets
        System.out.println("Original HashSet: " + languages);
        System.out.println("Cloned HashSet: " + clonedLanguages);
    }
}

Output:

Original HashSet: [Java, C, Python]
Cloned HashSet: [Java, C, Python]

Verifying Shallow Copy

To verify that the clone method creates a shallow copy, you can check if changes to the elements in the original set are reflected in the cloned set.

Example

import java.util.HashSet;

public class ShallowCopyExample {
    public static void main(String[] args) {
        // Creating a HashSet of StringBuilder objects
        HashSet<StringBuilder> languages = new HashSet<>();

        // Adding elements to the HashSet
        languages.add(new StringBuilder("Java"));
        languages.add(new StringBuilder("Python"));
        languages.add(new StringBuilder("C"));

        // Cloning the HashSet
        HashSet<StringBuilder> clonedLanguages = (HashSet<StringBuilder>) languages.clone();

        // Modifying an element in the original set
        for (StringBuilder sb : languages) {
            if (sb.toString().equals("Java")) {
                sb.append(" Language");
            }
        }

        // Printing the original and cloned HashSets
        System.out.println("Original HashSet: " + languages);
        System.out.println("Cloned HashSet: " + clonedLanguages);
    }
}

Output:

Original HashSet: [Java Language, C, Python]
Cloned HashSet: [Java Language, C, Python]

Conclusion

The HashSet.clone() method in Java provides a way to create a shallow copy of a HashSet. By understanding how to use this method, you can efficiently duplicate collections of objects in your Java applications. However, it is important to note that the clone method creates a shallow copy, meaning that changes to the objects in the original set will be reflected in the cloned set since both sets hold references to the same objects.

Leave a Comment

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

Scroll to Top