Thursday 17 April 2014

HashSet : clone : Clone Hash Set

public Object clone()
Returns a shallow copy of this HashSet instance.

import java.util.*;

class HashSetClone{
 public static void main(String args[]){
  HashSet<Integer> mySet1 = new HashSet<> ();
  HashSet<Integer> mySet2 = new HashSet<> ();
  
  /* Add Elements to mySet1 */
  mySet1.add(10);
  mySet1.add(20);
  mySet1.add(30);
  mySet1.add(40);
  
  System.out.println("Elements in mySet1 are");
  System.out.println(mySet1);
  
  System.out.println("\nCloning mySet1 to mySet2");
  
  mySet2 = (HashSet)mySet1.clone();
  
  System.out.println("\nElements in mySet2 after cloning");
  System.out.println(mySet2);
 } 
}

Output
Elements in mySet1 are
[20, 40, 10, 30]

Cloning mySet1 to mySet2

Elements in mySet2 after cloning
[20, 10, 40, 30]


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment