HashSet clone() in Java

clone(): This method is available in java.util.HashSet class of Java.

Syntax:

Object java.util.HashSet.clone()

This method returns a shallow copy of this HashSet instance: the elements themselves are not cloned.

Parameters: NA

Returns: a shallow copy of this set.

Exceptions: NA

Approach

Java

import java.util.HashSet;

public class HashSetclone {
    public static void main(String[] args) {

        HashSet<String> hashSet = new HashSet<String>();

        hashSet.add("Hello");
        hashSet.add("Java");
        hashSet.add("C++");
        hashSet.add("Hello");

        System.out.println(hashSet.clone());

    }
}

Output:

[Java, C++, Hello]


No comments:

Post a Comment