Saturday 19 April 2014

TreeSet(Collection c)

public TreeSet(Collection<? extends E> c)
Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements.

import java.util.*;

class TreeSetConstructor2{
 public static void main(String args[]){
  TreeSet<Number> mySet;
  ArrayList <Integer> myList = new ArrayList<> ();
  
  /* Add Data to  myList */
  for(int i=0; i < 10; i++)
   myList.add(i);
   
  System.out.println("Elements in myList is");
  System.out.println(myList);
  
  System.out.println("\nConstruct TreeSet with elements of myList");
  mySet = new TreeSet(myList);
  
  System.out.println("\nElements in mySet is");
  System.out.println(mySet);
 }
}

Output
Elements in myList is
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Construct TreeSet with elements of myList

Elements in mySet is
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment