A List is an interface that is part of the Java Collection framework. It can be used to store a list of objects. Arrays can also be used to store a group of values. There are often programming situations when you would need to convert a list to an array and vice versa. In this article, I will be explaining how you can achieve this.

  1. Converting Array to List
  2. Converting List to Array

Converting Array to List

Let us first learn about the different ways in which you can convert an array to a list

Manually Via a For Loop

The simplest way of converting an array to a list is to use a for a loop. The for loop can iterate over the input array and populate the list with each element from the array.

Sample Code

int[] myArray = {76, 23, 91, 45, 21, 82};
List<Integer> myList = new ArrayList<>();
for (int num:myArray) {
  myList.add(num);
}
System.out.println("List:"+myList);

 

  • Line 1 declares an int array called myArray and initializes it with some values.
  • Line 2 declares a List called myList.
  • Line 3 specifies a for loop that iterates over the array
  • Line 4 retrieves each element from myArray and adds it to myList.

Output

List:[76, 23, 91, 45, 21, 82]

Via Arrays.asList

The Arrays class has a method called asList. It accepts a variable number of values and returns a list containing those values. So, this method can be used to convert an array to a list.

Sample Code

Integer[] myArray = { 76, 23, 91, 45, 21, 82 };
List<Integer> myList = Arrays.asList(myArray);
System.out.println("List:" + myList);
  • As before, Line 1 declares an Integer array called myArray and initializes it with some values.
  • Line 2 invokes the Arrays.asList method passing in myArray. This returns an Integer list which is assigned to myList

Output

List:[76, 23, 91, 45, 21, 82]

Via Collections.addAll

The Collections class has a method called addAll. This accepts as a parameter a Collection and a variable number of values and adds the values specified to the Collection passed in. So, this method can be used to convert an array to a list.

Sample Code

Integer[] myArray = { 76, 23, 91, 45, 21, 82 };
List<Integer> myList = new ArrayList<>();
Collections.addAll(myList, myArray);
System.out.println("List:" + myList);
  • As before, Line 1 declares an Integer array called myArray and initializes it with some values.
  • Line 2 creates a new List called myList
  • Line 3 invokes the Collections.addAll method. It passes in myList and myArray as parameters. This adds all the elements in myArray to myList.

Output

List:[76, 23, 91, 45, 21, 82]

Via a Stream

The Arrays class has a method called stream. It can be used to obtain a stream from an array. This stream can then be converted to a list via the collect method.

Sample Code

Integer[] myArray = { 76, 23, 91, 45, 21, 82 };
List<Integer> myList = Arrays.stream(myArray).collect(Collectors.toList());
System.out.println("List:" + myList);
  • As before, Line 1 declares an Integer array called myArray and initializes it with some values.
  • Line 2 invokes Arrays.stream. This returns an IntStream corresponding to the input array. It then invokes collect which converts the stream to a list that is assigned to the myList variable.

Output

List:[76, 23, 91, 45, 21, 82]

Converting List to Array

Let us now learn about the different ways in which you can convert a list to an array.

Manually via a For loop

Just like we converted an array to a list, we can also convert a list to an array via a for loop.

Sample Code

List<Integer> myList = Arrays.asList(76,23,91,45,21,82);
int[] myArray = new int[myList.size()];

for(int i=0; i<myList.size();i++) {
  myArray[i] = myList.get(i);
}

System.out.println("Array:");
for (int num:myArray){
  System.out.print(num+" ");
}
  • Line 1 creates an Integer list called myList and initializes it with some values.
  • Line 2 declares an array called myArray. It uses the size of the list to determine the size of the array.
  • Line 4 specifies a for loop that iterates over the list.
  • Line 5 retrieves each element from the list and adds it to the array.
  • Lines 9-11 specify a for loop that prints the elements in the array.

Output

Array:
76 23 91 45 21 82

Via toArray

The List interface has a method called toArray. This can be used to convert a list to an array.

Sample Code

List<Integer> myList = Arrays.asList(76, 23, 91, 45, 21, 82);
Integer[] myArray = (Integer[])myList.toArray();
System.out.println("Array:");
for (Object num : myArray) {
  System.out.print(num + " ");
}
  • As before, Line 1 creates an Integer List called myList and initializes it with some values.
  • Line 2 invokes the toArray method. This converts the list to an array. It however creates an object array. This is then explicitly typecast to an Integer array.

Output

Array:
76 23 91 45 21 82

There is an overloaded version of the toArray method. It accepts as a parameter an array and returns an array of the same type as the array passed in.

Sample Code

List<Integer> myList = Arrays.asList(76, 23, 91, 45, 21, 82);
Integer[] myArray = myList.toArray(new Integer[myList.size()]);

System.out.println("Array:");
for (Object num : myArray) {
  System.out.print(num + " ");
}
  • In this case, when toArray is invoked, a new Integer array is passed in. So, toArray returns an Integer array and there is no need to explicitly typecast the array.
  • This code produces the same output as before.

Via a Stream

The Stream interface has a method called toArray. So, we can obtain a stream on the input list and then use the toArray method to convert it to an array.

Sample Code

List<Integer> myList = Arrays.asList(76, 23, 91, 45, 21, 82);
Integer[] myArray = (Integer[])myList.stream().toArray();
System.out.println("Array:");
for (Object num : myArray) {
  System.out.print(num + " ");
}
  • As before, Line 1 creates an Integer list called myList and initializes it with some values.
  • Line 2 invokes the stream method on myList. This returns a stream. It then invokes the toArray. Like the list.toArray, this method also returns an object array which is then explicitly typecast to an Integer array.

Output

Array:
76 23 91 45 21 82

There is an overloaded version of the toArray method. It accepts as a parameter an IntFunction and generates an array using it. IntFunction is an in-built functional interface that accepts as a parameter an int value and returns a result of any data type. In this case, this is used to generate an Integer array.

Sample Code

List<Integer> myList = Arrays.asList(76, 23, 91, 45, 21, 82);

Integer[] myArray = myList.stream().toArray(num -> new Integer[myList.size()]);
System.out.println("Array:");
for (Object num : myArray) {
System.out.print(num + " ");
}
  • As before, Line 1 creates an Integer List called myList and initializes it with some values.
  • Line 2 invokes the stream method on myList. This returns a stream. It then invokes the toArray with a lambda expression. The lambda expression accepts an int value corresponding to the size of the array and returns a new array of the specified size.

Output

Array:
76 23 91 45 21 82

Conclusion

So, in this article, we first saw how an array can be converted to a List via a for loop, via Collections.addAll, and via a Stream. We then saw how a List can be converted to an array via a for loop,  via the List.toArray method, and via a Stream.