This post will discuss how to convert a list of strings to a string array in Java.

1. Using List.toArray() method

We can use the toArray(T[]) method to copy the list into a newly allocated string array. We can either pass a string array as an argument to the toArray() method or pass an empty string type array. If an empty array is passed, JVM will allocate memory for the string array. Please note that if no argument is passed to toArray(), it will return an Object array.

Download  Run Code

Output:

[NYC, New Delhi]

2. Using Java 8

Java 8 provides another simple approach to convert a list of strings to a string array. Following are the steps:

  1. Convert the specified list of string to a sequential stream.
  2. Use the toArray() method to accumulate the stream elements into a new string array.

The following program demonstrates it:

Download  Run Code

Output:

[NYC, New Delhi]

3. Using Arrays.copyOf() method

We can also use Arrays.copyOf() to copy the specified array to an array of the specified type.

Download  Run Code

Output:

[NYC, New Delhi]

4. Using System.arraycopy() method

We can use System.arraycopy() that copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

Download  Run Code

Output:

[NYC, New Delhi]

5. Naive solution

This post is incomplete without re-inventing the wheels. A naive approach uses regular for-loop to iterate over the list of strings and simply copy elements from a list to string array.

Download  Run Code

Output:

[NYC, New Delhi]

That’s all about converting List of String to an array of Java String.