Java HashSet clone() Method

30 Dec 2024 | 2 min read

The clone() method of Java HashSet class is used to return a shallow copy of the specified HashSet.

Syntax

Following is the declaration of clone() method:

Parameter

This method does not accept any parameter.

Returns

The clone() method returns a shallow copy of this HashSet.

Exceptions

NA

Compatibility Version

Java 1.2 and above

Example 1

Output:

HashSet elements: [Google, 101, Hindi100, JavaTpoint, Facebook] The new clone set elements: [Google, 101, JavaTpoint, Facebook, Hindi100] 

Example 2

Output:

Hash set Elements: [151, 121, 111] The new clone set elements: [121, 151, 111] 

Example 3

Output:

Elements of the hset1: DFC SSSIT JTP Elements of the hset2 after clone() operation: DFC SSSIT JTP