I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE COURSE

1. Overview

This quick article is going to show how to convert between an Array and a List using core Java libraries, Guava or Apache Commons Collections.

This article is part of the “Java – Back to Basic” series here on Baeldung.

2. Convert List to Array

2.1. Using plain Java

Let’s start with the conversion from List to Array using plain java:

@Test
public void givenUsingCoreJava_whenListConvertedToArray_thenOK() {
    List<Integer> sourceList = Arrays.asList(0, 1, 2, 3, 4, 5);
    Integer[] targetArray = sourceList.toArray(new Integer[sourceList.size()]);
}

2.2. Using Guava

Let’s now use the Guava API for the same conversion:

@Test
public void givenUsingGuava_whenListConvertedToArray_thenOK() {
    List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
    int[] targetArray = Ints.toArray(sourceList);
}

3. Convert Array to List

3.1. Using plain Java

Let’s start with the plain Java solution for converting the array to a List:

@Test
public void givenUsingCoreJava_whenArrayConvertedToList_thenOK() {
    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 you want a standard ArrayList you can simply instantiate one as so:

List<Integer> targetList = new ArrayList<Integer>(Arrays.asList(sourceArray));

3.2. Using Guava

Let’s now use the Guava API for the same conversion:

@Test
public void givenUsingGuava_whenArrayConvertedToList_thenOK() {
    Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
    List<Integer> targetList = Lists.newArrayList(sourceArray);
}

3.3. Using Commons Collections

And finally, let’s use the Apache Commons CollectionsCollectionUtils.addAll API to fill in the elements of the array into an empty List:

@Test 
public void givenUsingCommonsCollections_whenArrayConvertedToList_thenOK() { 
    Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; 
    List<Integer> targetList = new ArrayList<>(6); 
    CollectionUtils.addAll(targetList, sourceArray); 
}

4. Conclusion

The implementation of all these examples and code snippets can be found over on GitHub – this is a Maven-based project, so it should be easy to import and run as it is.

I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE LESSONS

Sort by:   newest | oldest | most voted
Antal
Guest

Please note, that in 2. Convert with List to Array to two solutions are not the same, since the plain Java version returns a fixed size List, which you cannot add any more elements.
The equivalent solution would be:
List targetList = new ArrayList(Arrays.asList(sourceArray));

Eugen Paraschiv
Guest

Thanks – added the clarification into the article. Cheers, Eugen.

Anish K George
Guest

In the plain java example you have used “Lists”. I suppose this is from Asserj library. Is it worth to mention that?

Eugen Paraschiv
Guest

Actually, it’s Guava, but that’s a good point – since the example is focused on plain Java, I changed it to remove the Guava logic entirely. Cheers,
Eugen.