Saturday 19 April 2014

TreeSet : clone : Clone the TreeSet

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

import java.util.*;
class TreeSetClone{
 public static void main(String args[]){
  TreeSet<Integer> mySet = new TreeSet<> ();
  TreeSet<Integer> mySet1;
  
  /* Add Elements to the set */
  mySet.add(100);
  mySet.add(10);
  mySet.add(120);
  mySet.add(21);
  mySet.add(1);
  mySet.add(-5);
  
  System.out.println("Elements in mySet are");
  System.out.println(mySet);
 
  mySet1 = (TreeSet<Integer>)mySet.clone();
  
  System.out.println("Elements in mySet1 are");
  System.out.println(mySet1);
 }
}

Output

Elements in mySet are
[-5, 1, 10, 21, 100, 120]
Elements in mySet1 are
[-5, 1, 10, 21, 100, 120]



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment