Java Collection to Array

Java Collection to Array

Java Collection to Array conversion is used to convert any other Collection to Array and it is possible in Java using the method provided by Collection interface.

 Method to convert Java Collection to Array :

java.util.Collection interface provides toArray() method. It has declaration in the Collection interface as follows :

Object[] toArray()
Arguments / parameters : None

Returns:  Array containing all the elements of invoking collection

 

  • This method returns an array , which will contain all the elements of invoking collection.
  • This method can be invoked by any collection utility and it will result in an array containing the elements of same collection.
  • The ordering of the elements in array will be same as the order in the collection.
  • The array can be further modified like any basic array can be.
  • We can say that, this method acts as the bridge between collection-based and array-based APIs.

Example : How to convert Java Collection to Array : ArrayList to Array

This example will illustrate the use of toArray() method of the Collection interface to convert Java Collection ArrayList to Array.

//Example to convert Java Collection to Array

import java.util.*;
public class ExampleCollToArray
{
public static void main(String args[])
{ 

// create an ArrayList object 
ArrayList<Object> al = new ArrayList<>(); 

// Add some elements into this ArrayList
al.add("Joey"); 
al.add("Phoebe"); 
al.add(6); 
al.add(new Date());
al.add(3.10);
al.add("F.R.I.E.N.D.S");

System.out.println("Before Conversion , ArrayList elements : " + al);

Object arr[] = al.toArray();

System.out.println("After Conversion , ArrayList elements : " + Arrays.toString(arr));

}
}

Output :

Before Conversion , ArrayList elements : [Joey, Phoebe, 6, Thu May 23 10:06:00 UTC 2019, 3.1, F.R.I.E.N.D.S]
After Conversion , ArrayList elements : [Joey, Phoebe, 6, Thu May 23 10:06:00 UTC 2019, 3.1, F.R.I.E.N.D.S]

 

In the above example of Java Collection to Array, we have created an ArrayList of Objects al and we have added some elements to it. then, we have converted this ArrayList to array arr using toArray() method. We have printed the converted array  arr by using ArrayList.toString() method.

Example : How to convert Java Collection to Array : LinkedList to Array

This example will illustrate the use of toArray() method of the Collection interface to convert Java Collection LinkedList to Array.

//Example to convert Java Collection to Array

import java.util.*;
public class ExampleCollToArray
{
public static void main(String args[])
{ 

// create an LinkedList object 
LinkedList <Object> list = new LinkedList <>(); 

// Add some elements into this LinkedList 
list.add("Rachel"); 
list.add(1.2); 
list.add(6); 
list.add(new Date());


System.out.println("Before Conversion , LinkedList elements : " + list);

Object arr[] = list.toArray();

System.out.println("After Conversion , LinkedList elements : " + Arrays.toString(arr));

}
}

Output :

Before Conversion , LinkedList elements : [Rachel, 1.2, 6, Thu May 23 10:11:15 UTC 2019]
After Conversion , LinkedList elements : [Rachel, 1.2, 6, Thu May 23 10:11:15 UTC 2019]

 

In the above example of Java Collection to Array, we have created an LinkedList of Objects list and we have added some elements to it. then, we have converted this LinkedList to array arr using toArray() method. We have printed the converted array  arr by using ArrayList.toString() method.

Interview questions and answers related to converting Java Collection to Array

A) Is it possible to convert ArrayList to an Array and How?

Yes, it is possible. We can convert a java collection to an array using toArray() method of the Collection interface.

ArrayList al = new ArrayList();

al.add("Example");

Object arr[] = al.toArray();

B) In which Collection utility, toArray() method exists?

There are two overloaded methods of toArray() present in the Collection interface which is the parent in the Collection hierarchy but other Collection utilities also have this method but that is the overridden method of the Collection interface. e.g ArrayList also have this method.

Conclusion

The toArray() method of java.util.concurrent can be used to concert any collection to array. This can be useful in many contexts, where you need to put your collection elements into an array.

For more interview questions and answers for Java : Go here .

For complete understanding of ArrayList in Java : Go here .

For other Java concepts : Go here .

Thanks a lot for reading this article.

Please share your queries / suggestions / questions in below comment box. You can also directly connect with me through social media accounts.

Leave a Reply

Your email address will not be published. Required fields are marked *