Friday 18 April 2014

HashSet : toArray : Convert HashSet data to Array of Specific Type

public <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this set; the run time type of the returned array is that of the specified array.

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

Output
Elements in the Array are
55.6
10.01
-100.123
5.01

1. If the set fits in the specified array, it is returned, Otherwise, a new array is allocated with the run time type of the specified array and the size of this set.

import java.util.*;
class HashSetToSpecificArray1{
 public static void main(String args[]){
  Set<Double> mySet = new HashSet<> ();
  Double myArrayParam[] = new Double[2];
  Double myArrayReturn[];
  int size;
  
  /* 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 of type Double*/
  myArrayReturn = mySet.toArray(myArrayParam);
  
  /* Display the elements of the Array */
  System.out.println("Elements in the myArrayParam are");
  for(Double obj : myArrayParam)
   System.out.println(obj);
   
  System.out.println("\nElements in the myArrayReturn are");
  for(Double obj : myArrayReturn)
   System.out.println(obj);
 }
}

Output
Elements in the myArrayParam are
null
null

Elements in the myArrayReturn are
55.6
10.01
-100.123
5.01

As you observe the output, the size of the Array myArrayParam is 2, which is less that the set size 4. So the elements in the set are not copied to the Array myArrayParam.

If the myArrayParam has the size 4, exactly as the set size, then the elements are copied to myArrayParam and at the same time to the Array myArrayReturn also.

import java.util.*;
class HashSetToSpecificArray2{
 public static void main(String args[]){
  Set<Double> mySet = new HashSet<> ();
  Double myArrayParam[] = new Double[4];
  Double myArrayReturn[];
  int size;
  
  /* 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 of type Double*/
  myArrayReturn = mySet.toArray(myArrayParam);
  
  /* Display the elements of the Array */
  System.out.println("Elements in the myArrayParam are");
  for(Double obj : myArrayParam)
   System.out.println(obj);
   
  System.out.println("\nElements in the myArrayReturn are");
  for(Double obj : myArrayReturn)
   System.out.println(obj);
 }
}

Output
Elements in the myArrayParam are
55.6
10.01
-100.123
5.01

Elements in the myArrayReturn are
55.6
10.01
-100.123
5.01

2. If the Array size is greater than the set size, then the element in the array immediately following the end of the set is set to null.

import java.util.*;
class HashSetToSpecificArray3{
 public static void main(String args[]){
  Set<Double> mySet = new HashSet<> ();
  Double myArrayParam[] = new Double[10];
  Double myArrayReturn[] = new Double[10];
  int size;
  
  /* 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 of type Double*/
  myArrayReturn = mySet.toArray(myArrayParam);
  
  /* Display the elements of the Array */
  System.out.println("Elements in the myArrayParam are");
  for(Double obj : myArrayParam)
   System.out.println(obj);
   
  System.out.println("\nElements in the myArrayReturn are");
  for(Double obj : myArrayReturn)
   System.out.println(obj);
 }
}

Output
Elements in the myArrayParam are
55.6
10.01
-100.123
5.01
null
null
null
null
null
null

Elements in the myArrayReturn are
55.6
10.01
-100.123
5.01
null
null
null
null
null
null

3. toArray method throws NullPointerException if the specified Array is null

import java.util.*;
class HashSetToSpecificArray4{
 public static void main(String args[]){
  Set<Double> mySet = new HashSet<> ();
  Double myArrayParam[];
  myArrayParam = null;
  
  /* 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 of type Double*/
  mySet.toArray(myArrayParam);
  
  /* Display the elements of the Array */
  System.out.println("Elements in the myArrayParam are");
  for(Double obj : myArrayParam)
   System.out.println(obj);
 }
}

Output
Exception in thread "main" java.lang.NullPointerException
        at java.util.AbstractCollection.toArray(AbstractCollection.java:176)
        at HashSetToSpecificArray4.main(HashSetToSpecificArray4.java:15)

4. toArray throws ArrayStoreException if the runtime type of the specified array is not a supertype of the runtime type of every element in this set.

import java.util.*;
class HashSetToSpecificArray5{
 public static void main(String args[]){
  Set<Double> mySet = new HashSet<> ();
  Integer myArrayParam[] = new Integer[10];
  
  /* Add Elements to the Set */
  mySet.add(55.6);
  mySet.add(10.01);
  
  /* Copy Elements to the myArray of type Double*/
  mySet.toArray(myArrayParam);
 }
}

Output
Exception in thread "main" java.lang.ArrayStoreException: java.lang.Double
        at java.util.AbstractCollection.toArray(AbstractCollection.java:188)
        at HashSetToSpecificArray5.main(HashSetToSpecificArray5.java:12)

Set is of type Double, but the Array which is passes to the toArray method is of type Integer, which is not a sub class of Double, So ArrayStoreException thrown.




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment