Java TreeSet clone() method

The clone() is a method of TreeSet class. This method is used to create a copy of an existing object. This method returns a copy of the set.

Syntax:

Return:

It returns a shallow copy of the set.

Example 1

Output:

TreeSet: [1, 2, 5, 8, 10]
Elements in Cloned TreeSet: [1, 2, 5, 8, 10]

Example 2

Output:

TreeSet: [1, 3, 6, 9, A, B, a, b]
Elements in Cloned TreeSet: [1, 3, 6, 9, A, B, a, b]