Grails Cookbook - A collection of tutorials and examples

Convert String Array to List in Java

When we wish to work with multiple values of the same type, the most tempting data structure to use is Array. This provide a simple and fast way to create and manage multiple instances of a Class. It is easy and intuitive to use. However, one frustrating limitation of Arrays is that the number of items it holds is not flexible. Meaning we are stuck with a fixed number of items throughout the lifetime of the data structure. It is not possible to add more items later. That is why if we need flexibility, Lists are much better to work with. To cater the transition from one data structure to another, below are some examples on how to convert a String Array to List in Java.

Convert String Array to List in Java Using Custom Code

To solve the given problem, we can always create our own List object via ArrayList, and add all the elements of the array there one by one. Here is a short code to illustrate:
import java.util.ArrayList;
import java.util.List;
/**
  * A simple example that converts a String Array to List.
  */
public class SimpleCodeToCovertStringArrayToList
{
    public static void main( String[] args )
    {
        String[] myStringArray = { "A", "B", "C", "D", "E" };
        List<String> myStringList = new ArrayList<String>(myStringArray.length);
        for (String s:myStringArray) {
            myStringList.add( s );
        }
        for (String s : myStringList)
        {
            System.out.print( s + " " );
        }
    }
}

Note that we passed the length of the array when constructing the instance of an ArrayList. This is for efficiency, so that the ArrayList object will have enough room for all the elements of the Array. Otherwise, the code will be inefficient if the ArrayList will resize at some point. The output of this code will be:

A B C D E

We can actually avoid the for loop and just use Collections.addAll to transfer the values from the String Array to the List of String.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
  * A revised example that converts a String Array to List.
  */
public class RevisedCodeToCovertStringArrayToList
{
    public static void main( String[] args )
    {
        String[] myStringArray = { "A", "B", "C", "D", "E" };
        List<String> myStringList = new ArrayList<String>(myStringArray.length);
        Collections.addAll( myStringList, myStringArray );
        for (String s : myStringList)
        {
            System.out.print( s + " " );
        }
    }
}

Convert String Array to List in Java Using Arrays.asList

The util class Arrays has a built-in feature to convert a String Array to List. Here is a revised sample on how to use it:
import java.util.Arrays;
import java.util.List;
/**
  * An example that converts a String Array to List. using Arrays.asList()
  */
public class CovertStringArrayToListUsingArraysAsList
{
    public static void main( String[] args )
    {
        String[] myStringArray = { "A", "B", "C", "D", "E" };
        List<String> myStringList = Arrays.asList( myStringArray );
        for (String s : myStringList)
        {
            System.out.print( s + " " );
        }
    }
}
Note that there is a pitfall. The returned instance by Arrays.AsList is fixed size. It means we can not add any more value to it. For example, if we run this code:
String[] myStringArray = { "A", "B", "C", "D", "E" };
List<String> myStringList = Arrays.asList( myStringArray );
myStringList.add( "F" );

An exception will be raised. This is because adding elements is not supported in the returned object.

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(AbstractList.java:148)
	at java.util.AbstractList.add(AbstractList.java:108)

But we can revise the code to make it not throw exception. We can create another object and transfer the values to it. For example:
String[] myStringArray = { "A", "B", "C", "D", "E" };
List<String> myStringList = new ArrayList<String>(myStringArray.length);
myStringList.addAll(Arrays.asList(myStringArray));
myStringList.add( "F" );
The code now will not throw any exception.