Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Overview

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.

Further reading:

Convert an Array of Primitives to a List

Learn how to convert an array of primitives to a List of objects of the corresponding type.

Converting a Collection to ArrayList in Java

A brief tutorial to building ArrayLists given a collection in Java.

How to Convert List to Map in Java

Learn about different ways of converting a List to a Map in Java, using core functionalities and some popular libraries

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_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.

2.2. Using Guava

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);
}

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_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));

3.2. Using Guava

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);
}

3.3. Using Commons Collections

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); 
}

4. Conclusion

The implementation of all of 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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!