It is often required to convert a java List to an Array or wise versa. In this article we discuss few methods of doing the List to Array conversion.
Java List interface provides methods to interact with collection of objects which duplicates are allowed. java.util.list is a child interface of the java.util.collection interface where java.util.list is implemented in ArrayList, LinkedList, Vector and Stack classes. List interface allows adding, removing, getting and setting values in List objects. Our main focus here is how to convert list to array in java. Below are few example methods of converting a list to array.
1. List interface's built in method toArray() for converting List to Array of String
2. Stream API (Since Java 8)
With the introduction of streams in java 8, it is more than easier to convert List to array in java. First it has to stream the list with List.stream() method then Stream.toArray() can be used to return an Array object containing items from the original list.
3. Using Guava library
Guava library provides methods for converting a List to Array using Iterables class.