Java Convert String List To Array

Java Convert String List To Array | In Java, the List interface provides a way to store the ordered collection where duplicate values can also be held i.e. the values in the list can be repeated. We can have a string list that contains strings and an integer list that contains integers and many more. In this section, our problem is to convert list of string to array of string Java.

To understand more see the below example:-
list = [“Java”, “Programming”, “Language”]
String array = [“Java”, “Programming”, “Language”]

We can convert list of string to array of string Java using the below three ways:-
1. Using toArray() method
2. Using Stream In Java 8
3. Using get() method

Java List Of String To Array using toArray()

Here we use the toArray() method to convert list of string to array of string Java. The list interface contains the toArray() method which is used to convert lists to arrays. When we apply toArray() to a List of strings then it gives an array of strings. Let us demonstrate it through an example:-

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
   public static void main(String[] args) {
      List<String> list = new ArrayList<>();
      list.add("Java");
      list.add("Programming");
      list.add("Language");
      System.out.println("List = " + list);
      System.out.println("list type: " + list.getClass().getName());
      
      // convert list of strings to array of strings
      String[] array = list.toArray(new String[] {});
      
      System.out.println("Array = " + Arrays.toString(array));
      System.out.println("array type: " + array.getClass().getName());
   }
}

Output:-

List = [Java, Programming, Language]
list type: java.util.ArrayList
Array = [Java, Programming, Language]
array type: [Ljava.lang.String;

In the toArray() method we have passed “new String[ ] { }” therefore it converts list of string to array of strings. To show the type of the variable we have called getClass() and getName() method. To display the array we have called the toString() method of the Arrays class which is used to convert an array to a string.

Java Convert String List To Array using Stream

Here to convert list to string array Java we use Stream. It is very similar to the previous approach.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
   public static void main(String[] args) {
      List<String> list = new ArrayList<>();
      list.add("Java");
      list.add("Programming");
      list.add("Language");
      System.out.println("List = " + list);
      System.out.println("list type: " + list.getClass().getName());
      
      // convert list of string to array of strings
      String[] array = list.stream().toArray(String[]::new);
      
      System.out.println("Array = " + Arrays.toString(array));
      System.out.println("array type: " + array.getClass().getName());
   }
}

Output:-

List = [Java, Programming, Language]
list type: java.util.ArrayList
Array = [Java, Programming, Language]
array type: [Ljava.lang.String;

How To Convert List To String Array In Java using get()

Now we use the get() method to convert the list to a string array. This is one of the simple ways where we will create an array of size of the list, then we will iterate the list, fetch the elements and insert them into the array.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
   public static void main(String[] args) {
      List<String> list = new ArrayList<>();
      list.add("Java");
      list.add("Programming");
      list.add("Language");
      System.out.println("List = " + list);
      System.out.println("list type: " + list.getClass().getName());

      // convert list of string to array of strings
      String[] array = new String[list.size()];
      for (int i = 0; i < list.size(); i++) {
         array[i] = list.get(i);
      }

      System.out.println("Array = " + Arrays.toString(array));
      System.out.println("array type: " + array.getClass().getName());
   }
}

Output:-

List = [Java, Programming, Language]
list type: java.util.ArrayList
Array = [Java, Programming, Language]
array type: [Ljava.lang.String;

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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