In this example, We will discuss about how to get the Clone of java HashSet object.
Get Clone Set from Java HashSet example
In this example, We will discuss about how to get the Clone of java HashSet object. the clone can be obtained by using clone method of Java HashSet class. To get the clone we use Object clone() method. This method returns a shallow copy of this HashSet instance.the elements themselves are not cloned.
The Example is as follows-
CloneOfHashSet.java
package devmanuals.com; import java.util.HashSet; public class CloneOfHashSet { public static void main(String[] args) { HashSet hst = new HashSet(); hst.add("Gyan"); hst.add("Rohit"); hst.add("Anand"); hst.add("Arunesh"); System.out.println("The HashSet elements are: " + hst); System.out.println("The clone of HashSet is: " + hst.clone()); } }
Output:-
The HashSet elements are : [Rohit, Gyan, Arunesh, Anand]
The clone of HashSet is : [Gyan, Rohit, Arunesh, Anand] |
[ 0 ] Comments