Home » Java HashSet clone() Method with Examples

Java HashSet clone() Method with Examples

by Online Tutorials Library

Java HashSet clone() Method

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

Test it Now

Output:

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

Example 2

Test it Now

Output:

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

Example 3

Test it Now

Output:

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

You may also like