Converting between an Array and List in Java
Arrays are linear data structures used to store collections of data. Arrays are allocated contiguous blocks of memory, and this allows us to randomly access any element of the array using its index. The List is an interface of java.util package and helps us to maintain an ordered collection of data. The List interface is implemented by the ArrayList, LinkedList, and a few other classes. In this tutorial, we will learn how to convert an array to a list and a list back to an array.
Array to List Conversion
Using Arrays.asList() Method
The asList() method of the Arrays class provides a simple method to convert an array to a list. We need to pass the array as a parameter to this method, and it will return a list of the elements of the array.
The following code demonstrates the working of the asList() method.
public static void main(String args[])
{
//Converting a string array
String[] strArr = {"this", "is", "a", "string", "array"};
List<String> strList = Arrays.asList(strArr);
System.out.println(strList);
//Converting an integer array
Integer[] intArr = {2, 3, 5, 7, 11, 13, 17};
List<Integer> intList = Arrays.asList(intArr);
System.out.println(intList);
}
[this, is, a, string, array]
[2, 3, 5, 7, 11, 13, 17]
Using Collections.addAll() Method
The Collections class of java.util package has an addAll() method which will add all the elements of an array to a list. We first need to create an empty List and then pass the existing array and the newly created List as parameters to this method. The method signature is shown below.
Collections.addAll(target, source);
The following code demonstrates the working of the addAll() method.
public static void main(String args[])
{
//Converting a string array
String[] strArr = {"this", "is", "a", "string", "array"};
List<String> strList = new ArrayList<String>();
Collections.addAll(strList, strArr);
System.out.println(strList);
//Converting an integer array
Integer[] intArr = {2, 3, 5, 7, 11, 13, 17};
List<Integer> intList = new ArrayList<Integer>();
Collections.addAll(intList, intArr);
System.out.println(intList);
}
[this, is, a, string, array]
[2, 3, 5, 7, 11, 13, 17]
Using Guava Library
Guava is a set of open-source libraries developed by Google. It has a Lists.newArrayList() method which can be used to convert an array to an ArrayList. The following code demonstrates its working.
public static void main(String args[])
{
//Converting a string array
String[] strArr = {"this", "is", "a", "string", "array"};
List<String> strList = Lists.newArrayList(strArr);
System.out.println(strList);
//Converting an integer array
Integer[] intArr = {2, 3, 5, 7, 11, 13, 17};
List<Integer> intList = Lists.newArrayList(intArr);
System.out.println(intList);
}
[this, is, a, string, array]
[2, 3, 5, 7, 11, 13, 17]
Writing our own method
We can write our method and create a List from an array. The List interface provides us with the add() method which we can use to add elements from the array to the list. The following code converts an array of Strings to a List. We can create a different method if we wish to convert an array of some other data type, or we can just use Generic methods.
public static List<String> arrayToList(String[] strArr)
{
List<String> strList = new ArrayList<String>();
for(int i = 0; i <= strArr.length - 1; i++)
strList.add(strArr[i]);
return strList;
}
public static void main(String args[])
{
//Converting a string array
String[] strArr = {"this", "is", "a", "string", "array"};
List<String> strList = arrayToList(strArr);
System.out.print(strList);
}
[this, is, a, string, array]
List to Array Conversion
Using toArray() method
The toArray() method provides a simple way of converting a List to an array. The following code demonstrates its working.
public static void main(String args[])
{
//Creating an ArrayList
List<String> strList = new ArrayList<String>();
strList.add("this");
strList.add("is");
strList.add("a");
strList.add("string");
strList.add("array");
//converting to array
String[] strArr = strList.toArray(new String[0]);
for(int i = 0; i <= strArr.length - 1; i++)
System.out.print(strArr[i] + " ");
}
this is a string array
Using Guava Library
In the previous section, we saw how the Guava library can be used to convert an array to a List. It also provides the toArray() method that can be used to convert a List back to an array.
public static void main(String args[])
{
//Creating an ArrayList
List<String> strList = new ArrayList<String>();
strList.add("this");
strList.add("is");
strList.add("a");
strList.add("string");
strList.add("array");
//converting to array
String[] strArr = Iterables.toArray(strList, String.class);
for(int i = 0; i <= strArr.length - 1; i++)
System.out.print(strArr[i] + " ");
}
this is a string array
Writing our own method
We can also define our own method to convert a List to an array. We will loop through the List and fetch each element using the get() method and add it to the array using the array indices. We can define different methods for different data types or we can simply use Generic methods.
public static String[] listToArray(List<String> strList)
{
String[] strArr = new String[strList.size()];
for(int i = 0; i <= strList.size() - 1; i++)
strArr[i] = strList.get(i);
return strArr;
}
public static void main(String args[])
{
//Creating an ArrayList
List<String> strList = new ArrayList<String>();
strList.add("this");
strList.add("is");
strList.add("a");
strList.add("string");
strList.add("array");
//converting to array
String[] strArr = listToArray(strList);
for(int i = 0; i <= strArr.length - 1; i++)
System.out.print(strArr[i] + " ");
}
this is a string array
Summary
An array is a simple yet useful data structure used to store similar types of data. The List is an interface and classes like ArrayList and LinkedList implement this interface. ArrayList is a dynamic array and overcomes the problem of the limited size of the array. One must know how to convert an array to a list and vice versa to utilize the full potential of both of them. The asList() and toArray() methods are the most frequently used ones. We also implemented our own methods to convert array to list by using the add() method of the list, and to convert a list to an array by using the get() method of the list. We can also define generic methods to work with other data types.