Friday, 18 April 2014

HashSet : toArray : Convert HashSet data to Object Array

Object[] toArray()
Returns an array containing all of the elements in this set. The order how the Elements from the set copied is totally depends on the iterator implementation.

import java.util.*;
class HashSetToArray{
 public static void main(String args[]){
  Set<Double> mySet = new HashSet<> ();
  Object myArray[];
  
  /* Add Elements to the Set */
  mySet.add(55.6);
  mySet.add(10.01);
  mySet.add(5.01);
  mySet.add(-100.123);
  
  /* Copy Elements to the myArray */
  myArray = mySet.toArray();
  
  /* Display the elements of the Array */
  System.out.println("Elements in the Array are");
  for(Object obj : myArray)
   System.out.println(obj);
 }
}

Output
Elements in the Array are
55.6
10.01
-100.123
5.01



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment