how to convert a TreeSet to an Array using collection in java
Previous | Home | Next |
In this example we shall see how to convert a TreeSet to a linear array.
For creating a TreeSet we have to import the java.util package in which we have the class java.util.TreeSet which contains the definition and various that can be performed on a TreeSet. In this example we have created a class named CollectionExample in which we have created a TreeSet and added some elements to it.
In this example for converting a TreeSet to a linear we have used toArray(Anytypearray[] array) method
on the object of the TreeSet. The syntax for using the same method can be shown as,
toArray(Anytypearray[] array)
The above method returns an array containing all of the elements of the TreeSet in the correct order and the runtime type of the returned array is similar as that of the specified array.
In the given example we have created a TreeSet and added some elements to it by invoking the add() method on the object of the TreeSet i.e ts. We have also created an array of same size as that of the TreeSet by using the size() method on the TreeSet object ts. The array object is used for storing the elements returned by invoking toArray(Anytypearray[] array) method on the TreeSet object ts. Finally we have displayed the elements of the array using the for loop.
The following statements in the given example converts the TreeSet elements to the array elements,
Integer[] array=new Integer[ts.size()];
This statment creates a new integer array of same size as that of the TreeSet.
array=ts.toArray(array);
This statement converts the TreeSet to the array.
package r4r.co.in;
import java.util.*;
public class CollectionExample {
/**
* @param args
*/
public static void main(String[] args) throws ClassCastException, NullPointerException, ArrayStoreException
{
// TODO Auto-generated method stub
try
{
TreeSet<Integer> ts=new TreeSet<Integer>(); // creating a TreeSet object ts
ts.add(new Integer(2)); // adding object to the TreeSet
ts.add(4);
ts.add(7);
ts.add(8);
ts.add(1);
ts.add(9);
System.out.println("The objects of the tree set are as follows: ");
System.out.println(ts); // displaying the contents of the TreeSet
Integer[] array=new Integer[ts.size()]; // creating a new Integer Array named array of same size as of TreeSet
array=ts.toArray(array); // converting the TreeSet to the Array using toArray() method
System.out.println("The contents of the created array is as follows: ");
for(int i=0; i<array.length; i++)
{
System.out.println(array[i]); // displaying the contents of the array
}
}
catch(ClassCastException e)
{
System.out.println(e.getMessage());
}
catch(NullPointerException e)
{
System.out.println(e.getLocalizedMessage());
}
catch(ArrayStoreException e) // handles the exception if elements stored in array are not compatible
{
System.out.println(e.getMessage());
}
}
}
The output of the above program is as follows:
The objects of the tree set are as follows:
[1, 2, 4, 7, 8, 9]
The contents of the created array is as follows:
1
2
4
7
8
9
Previous | Home | Next |