Saturday, 19 April 2014

TreeSet (SortedSet s)

public TreeSet(SortedSet<E> s)
Constructs a new tree set containing the same elements and using the same ordering as the specified sorted set.

import java.util.*;

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

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

Elements in mySet2 are
[10, 20, 30, 40]


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment