
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: April 4, 2025
In this quick tutorial, we’re going to learn how to convert between an Array and a List using core Java libraries, Guava and Apache Commons Collections.
This article is part of the “Java – Back to Basic” series here on Baeldung.
Let’s start with the conversion from List to Array using plain Java:
@Test
public void givenUsingCoreJava_whenListConvertedToArray_thenCorrect() {
List<Integer> sourceList = Arrays.asList(0, 1, 2, 3, 4, 5);
Integer[] targetArray = sourceList.toArray(new Integer[0]);
}
Note that the preferred way for us to use the method is toArray(new T[0]) versus toArray(new T[size]). As Aleksey Shipilëv proves in his blog post, it seems faster, safer, and cleaner.
Now let’s use the Guava API for the same conversion:
@Test
public void givenUsingGuava_whenListConvertedToArray_thenCorrect() {
List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
int[] targetArray = Ints.toArray(sourceList);
}
Let’s start with the plain Java solution for converting the array to a List:
@Test
public void givenUsingCoreJava_whenArrayConvertedToList_thenCorrect() {
Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
List<Integer> targetList = Arrays.asList(sourceArray);
}
Note that this is a fixed-sized list that will still be backed by the array. If we want a standard ArrayList, we can simply instantiate one:
List<Integer> targetList = new ArrayList<Integer>(Arrays.asList(sourceArray));
Now let’s use the Guava API for the same conversion:
@Test
public void givenUsingGuava_whenArrayConvertedToList_thenCorrect() {
Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
List<Integer> targetList = Lists.newArrayList(sourceArray);
}
Finally, let’s use the Apache Commons Collections CollectionUtils.addAll API to fill in the elements of the array in an empty List:
@Test
public void givenUsingCommonsCollections_whenArrayConvertedToList_thenCorrect() {
Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
List<Integer> targetList = new ArrayList<>(6);
CollectionUtils.addAll(targetList, sourceArray);
}