Convert List of String to a String array in Java
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; import java.util.List; class Main { // convert a list of strings to a string array in Java public static void main(String[] args) { List<String> list = Arrays.asList("NYC", "New Delhi"); String[] array = list.toArray(new String[0]); System.out.println(Arrays.toString(array)); } } |
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:
- Convert the specified list of string to a sequential stream.
- Use the
toArray()method to accumulate the stream elements into a new string array.
The following program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.List; class Main { // convert a list of strings to a string array in Java 8 and above public static void main(String[] args) { List<String> list = Arrays.asList("NYC", "New Delhi"); String[] array = list.stream() .toArray(String[]::new); System.out.println(Arrays.toString(array)); } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.List; class Main { // convert a list of strings to a string array in Java public static void main(String[] args) { List<String> list = Arrays.asList("NYC", "New Delhi"); String[] array = Arrays.copyOf(list.toArray(), list.size(), String[].class); System.out.println(Arrays.toString(array)); } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; import java.util.List; class Main { // convert a list of strings to a string array in Java public static void main(String[] args) { List<String> list = Arrays.asList("NYC", "New Delhi"); String[] array = new String[list.size()]; System.arraycopy(list.toArray(), 0, array, 0, list.size()); System.out.println(Arrays.toString(array)); } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; import java.util.List; class Main { // convert a list of strings to a string array in Java public static void main(String[] args) { List<String> list = Arrays.asList("NYC", "New Delhi"); // allocate memory for string array String[] array = new String[list.size()]; // copy elements from a list to a string array for (int i = 0; i < array.length; i++) { array[i] = list.get(i); } System.out.println(Arrays.toString(array)); } } |
Output:
[NYC, New Delhi]
That’s all about converting List of String to an array of Java String.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)