Java String Array to String List

Java String Array To String List | The strings are the collection of characters or a sequence of characters. Here, the array of strings is in the sense of the array which contains strings. Now, in this section, our goal is to see how to convert string array to list in Java.

For understanding see the below example:-
Array = [“Hi”, “Welcome”]
List = [“Hi”, “Welcome”]

There are many methods to convert an array of strings to a list they are:-
1. Brute force method
2. Using Arrays.asList() method
3. Using Collections.addAll() method

Convert Array of Strings to List Java

In the brute force method,

  1. We obtain the array to be converted
  2. Then we take an empty list
  3. Iterate the array and fetch the elements of the array
  4. Then for each element, add it to the list.
  5. Return the list. 
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

public class Main {

   public static <T> List<T> convertArrayToList(T array[]) {
      List<T> list = new ArrayList<>();
      for (T t : array) {
         list.add(t);
      }
      return list;
   }

   public static void main(String args[]) {
      String array[] = { "Java", "Python", "JavaScript" };
      System.out.println("Array: " + Arrays.toString(array));
      System.out.println("array type: " + array.getClass().getName());

      // convert array of strings to list 
      List<String> list = convertArrayToList(array);

      System.out.println("List: " + list);
      System.out.println("list type: " + list.getClass().getName());
   }
}

Output:

Array: [Java, Python, JavaScript]
array type: [Ljava.lang.String;
List: [Java, Python, JavaScript]
list type: java.util.ArrayList

Java String Array to String List using Arrays.asList()

Now, we will use Arrays.asList() method to convert string array to list in Java. The asList() method of the Arrays class is used to convert the array to a list.

  1. We first need to initialize the array
  2. Then create an empty list and pass the array as the parameter to the constructor of the list by using the Arrays.asList() method
  3. Return the list.
import java.util.Arrays;
import java.util.List;

public class Main {

   public static <T> List<T> convertArrayToList(T array[]) {
      List<T> list = Arrays.asList(array);
      return list;
   }

   public static void main(String args[]) {
      String array[] = { "Java", "Python", "JavaScript" };
      System.out.println("Array: " + Arrays.toString(array));
      System.out.println("array type: " + array.getClass().getName());

      List<String> list = convertArrayToList(array);
      System.out.println("List: " + list);
      System.out.println("list type: " + list.getClass().getName());
   }
}

Output:-

Array: [Java, Python, JavaScript]
array type: [Ljava.lang.String;
List: [Java, Python, JavaScript]
list type: java.util.Arrays$ArrayList

Using the convertArrayToList() you can convert any type of list to an array. When we pass array of String to convertArrayToList() then it returns an array of strings.

In the above example, we have used a separate method to convert list of string to array of strings, but we do it in the same method also.

Java String Array to String List using Arrays.asList()

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

public class Main {
   public static void main(String args[]) {
      String array[] = { "Java", "Python", "JavaScript" };
      System.out.println("Array: " + Arrays.toString(array));
      System.out.println("array type: " + array.getClass().getName());

      List<String> list = Arrays.asList(array);
      System.out.println("List: " + list);
      System.out.println("list type: " + list.getClass().getName());
   }
}

Output:-

Array: [Java, Python, JavaScript]
array type: [Ljava.lang.String;
List: [Java, Python, JavaScript]
list type: java.util.Arrays$ArrayList

Limitation of using Arrays.asList():- When we notice, while using brute force approach the type of list is “java.util.ArrayList” but while using Arrays.asList() it is “java.util.Arrays$ArrayList“. When we call Arrays.asList() then instead of creating and returning a separate list it returns a list representation of the array. And there are many limitations of this approach as follows:-

  1. Arrays are fixed in size, we can’t increase and decrease their size once it is declared. Therefore, in the list, we can’t add or remove elements because it will try to modify the array size.
import java.util.Arrays;
import java.util.List;

public class Main {
   public static void main(String args[]) {
      String array[] = { "Java", "Python", "JavaScript" };
      List<String> list = Arrays.asList(array);
      System.out.println("List: " + list);
      list.add("HTML");
   }
}

Output:-

List: [Java, Python, JavaScript]
Exception in thread “main” java.lang.UnsupportedOperationException
at java.base/java.util.AbstractList.add(AbstractList.java:153)
at java.base/java.util.AbstractList.add(AbstractList.java:111)
at Main.main(Main.java:9)

  1. Modification done in the array will also reflect in the list.
import java.util.Arrays;
import java.util.List;

public class Main {
   public static void main(String args[]) {
      String array[] = { "Java", "Python", "JavaScript" };
      List<String> list = Arrays.asList(array);
      System.out.println("List: " + list);
      array[1] = "HTML"; // modifying array
      System.out.println("List: " + list);
   }
}

Output:-

List: [Java, Python, JavaScript]
List: [Java, HTML, JavaScript]

Due to these limitations, it is not recommended to use Arrays.asList() if you are going to modify the list or array after the conversion. But if you want only read operation then you can use it happily.

Java String Array to String List using Collections.addAll()

Here, we use collections.addAll() method to convert string array to list in Java. Let us see it through an example.

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

public class Main {

   public static <T> List<T> convertArrayToList(T array[]) {
      List<T> list = new ArrayList<>();
      Collections.addAll(list, array);
      return list;
   }

   public static void main(String args[]) {
      String array[] = { "Java", "Python", "JavaScript" };
      System.out.println("Array: " + Arrays.toString(array));
      System.out.println("array type: " + array.getClass().getName());

      List<String> list = convertArrayToList(array);
      System.out.println("List: " + list);
      System.out.println("list type: " + list.getClass().getName());
   }
}

Output:-

Array: [Java, Python, JavaScript]
array type: [Ljava.lang.String;
List: [Java, Python, JavaScript]
list type: java.util.ArrayList

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.