Convert List to Array using toArray(...)

Considering you have a List of String, the following code bit transforms it into an array.

List<String> list = new ArrayList<>();
// ...
String[] array = list.toArray(new String[0]);

Convert List to Array using Stream (Java 8+)

String[] array = list.stream().toArray(String[]::new);

Convert List to Array toArray(...) with method reference (Java 11+)

String[] array = list.toArray(String[]::new);

Explanation

There are cases where converting a List to an array might be necessary, such as dealing with an Application Programming Interface (API) that accepts only arrays as parameters.

The most common way of doing this is by using the toArray(...) method, which requires the array type to be created. Beginning with Java 8, another option is to use the Streams API. In Java 11, this was further enhanced, allowing the toArray(...) method to be used without informing the array size.

This brief article outlined how to convert a List to an Array using Java. Check other entries for more information.