How To Convert An Array To An ArrayList In Java! 👍
In this article, we’ll take a look at how to convert an array to an ArrayList in Java. We’ll investigate three examples where we turn an array into an ArrayList using pure Java and then we’ll review several approaches to convert an array into an ArrayList using one or more open-source libraries.
Array To ArrayList Java TOC
Software developers often find the need to convert arrays into List objects (ArrayList probably being one of the more popular classes) when dealing with objects in order to leverage the flexibility and utility of the List implementation for easier and more effective data manipulation.
Transforming arrays in Java into instances of java.util.List enables developers to access a more robust set of methods for managing data, including adding, removing, and traversing elements, methods which are unavailable when working with simple Java arrays.
Java arrays and ArrayLists, while serving similar purposes as containers for objects, differ significantly in their flexibility and functionality.
An array is a fixed-size data structure that does not permit dynamic resizing, while the ArrayList implementation, which is part of Java’s Collection Framework, provides dynamic resizing functionality and comes with a multitude of built-in methods to facilitate the manipulation of the ArrayList’s contents.
Java arrays can contain both primitive values as well as objects while implementations of the Java List can only hold references to objects.
In this article, we’ll cover several approaches to converting a Java array to an instance of Java ArrayList.
We’ll also look at the performance of each approach as well, which is important as not all approaches are equal when it comes to speed.
Benefits of converting an array to an ArrayList in Java
Converting an array to an instance of java.util.ArrayList in Java can offer several advantages from a software engineering perspective:
Enhanced methods and operations are available when you turn a Java array into an ArrayList
The java.util.ArrayList class is a part of the Java Collections Framework and provides a long list of methods for data access, traversal, manipulation, and other operations that allow software developers to handle data more efficiently.
For example, the Java developer can access a specific array element by using its index, such as someArray[0], which retrieves the first array element; when working with an instance of java.util.ArrayList the developer will use the get method, which is defined in the java.util.List interface and is available to all classes that implement that specification.
As software engineers, working with an instance of an ArrayList gives us many options that are not available out of the box when using a simple array.
Dynamic sizing comes for free when using a Java ArrayList
Unlike arrays, which are of a fixed size once they’ve been created, the java.util.ArrayList implementation can automatically adjust its capacity.
The ability to dynamically adjust the size makes the ArrayList class an attractive option when it’s unclear how many elements your data structure requires and also avoids wasted memory as well as the need to manually resize the container.
Automatic Element Shifting is another reason to use an ArrayList rather than an array
When you remove an element from an instance of java.util.ArrayList, the instance automatically shifts the subsequent elements to cover the gap thereby making deletions simpler — with Java arrays, however, you’re forced to handle these shifts manually.
Support for Java Generics ensures compile-time type safety
With Java’s Generics, you can ensure type safety by defining an ArrayList of a specific type — for example:
List exampleList = new ArrayList ();
Only instances of Java String can be placed in this List and we’ll get a compile time exception if we try to add an instance of some other type, as is demonstrated in the image below.

The script for the example above can be reviewed and executed on tio.run.
Taking advantage of Java Generics prevents inadvertent insertions of incorrect data types and also enhances code clarity.
Concurrency Control
If you’re working in a multi-threaded environment, you can use the Collections synchronizedList static method to wrap your instance of ArrayList in a synchronized (thread-safe) List as follows:
List syncronizedExamplesList = Collections.synchronizedList(examplesList);
Synchronized lists are appropriate for scenarios where moderate levels of concurrency are required.
If an application requires higher levels of concurrent access, other concurrent data structures or strategies including CopyOnWriteArrayList or fine-grained locking may be more appropriate.
Integration with the Java Stream API
The Java Collections framework, of which the ArrayList is a member of, integrates seamlessly with the Java Stream API (see example two), which was introduced in Java 8.
The Java Stream API allows for more readable, efficient data manipulation using functional programming constructs.
The Java Stream API also delivers significant performance advantages for certain operations.
From a software engineering perspective, using an ArrayList can lead to more readable, maintainable, and flexible code, especially when dealing with an unknown or variable number of elements.
While arrays have their own merits and use cases, ArrayList s offer higher-level functionality that can simplify many common tasks.
Let’s take a look at the initial example regarding how to transform an array of java.lang.Integer objects into an instance of java.util.ArrayList In Java.
Example Zero: Turn an array into an ArrayList in Java via the ArrayList constructor
This is the easiest example and probably the most frequent way of approaching this issue since we can write this on one line if we so desire.
I’ve broken this down into a script that better demonstrates what’s going on below — lines #7 and #11 are where we need to focus our attention.
Note that this script can be run directly via your browser on tio.run.
The gist for the complete example is available on GitHub and the following code is compatible with Java 8 and above.
public class Main {
public static void main(String[] args) {
Integer[] numbers = {1, 2, 3, 4, 5};
java.util.List exampleList = java.util.Arrays.asList (numbers);
System.out.println ("exampleList.class.name: " + exampleList.getClass ().getName ());
java.util.List exampleArrayList = new java.util.ArrayList<> (exampleList);
System.out.println ("exampleArrayList.class.name: " + exampleArrayList.getClass ().getName ());
exampleArrayList.forEach (item -> System.out.println ("item: " + item));
System.exit (0);
}
}
We can condense lines #7 and #11 into a single line as follows:
java.util.List exampleArrayList = new ArrayList<> (Arrays.asList (numbers));
As per the documentation for the Arrays class, the Arrays.asList array method returns a fixed-size list backed by the specified array.
This newly converted List instance is backed by the original array and does not support the add method or other list modification methods, as we can see below.
The example in the picture can be executed in your browser using the tio.run online interpreter.
If we want to add a new element to this List instance, we’ll need to turn it into an instance of some other implementation of the List specification, one which is resizable, and we use a java.util.ArrayList as it fits this description (see line #11).
Turn An Array Into An ArrayList In Java from Your Browser!
We can convert an array to an ArrayList by running this Java program directly in our browser using Try It Online (tio.run or TIO).
Try It Online is described as a family of online interpreters for practical and recreational programming languages and, as of 02.Oct.2023, supports a total of 681 languages.
TIO is very helpful for demonstrating ideas like the one we’re reviewing here because we can run this Java program without needing to download any code or software and directly via our browsers.
Try It Online Example Script and Output
We can see this example as it should appear in the Try It Online Java 8 interpreter and we can run this script by clicking on the play icon which the red pointer points to.
Performance considerations when converting an array to an ArrayList via the java.util.ArrayList constructor
Much to my surprise this example seems to be the worst performing of the group in this article to date given the simple performance test I’ve included below.
The Java ArrayList constructor either assigns a copy of the array (#182) to the internal elementData property when it’s an instance of ArrayList or if it’s not an instance of ArrayList then the elementData property is set to a copy of the array (#187).
Making a copy of an array in Java is a relatively efficient operation as well.
This concludes the initial example regarding how to turn an array into an instance of an ArrayList in Java, in this case by calling the ArrayList constructor and passing it the tempResult collection returned by the call to the asList method.
In the next example we’ll call the ArrayList constructor and pass it the initial capacity and use the addAll method to populate the instance.
Example One: Convert an Array to an ArrayList using Arrays and ArrayList methods.
In this example we rely on java.util.Arrays and java.util.ArrayList methods to convert an array of java.lang.Integer objects into an java.util.Arrays of type java.lang.Integer.
If we are content with using the read-only instance of java.util.Arrays$ArrayList then this example is complete on line #10, however if we require an instance of java.util.ArrayList then this conversion takes place on lines 16 and 18.
Note that the previous example suffered from an expensive resizing operation when the length of the numbers array is greater than ten however this example does not have this problem because we set the initial capacity on line #18 when calling the ArrayList constructor.
The following code is compatible with Java 8 and above.
Step-by-step instructions regarding how to copy an array to an ArrayList in Java using the addAll method!
This is a minimal example that provides only what’s required to convert an array of Integer objects to an ArrayList of type Integer in Java.
Step One: Create an array of type java.lang.Integer.
The first step in this example involves creating an array of Integer objects — in this case, we just pick numbers one through five.
Note that we could take advantage of Java’s support for varargs and autoboxing however for clarity I’ve declared the array on its own line.
Integer[] numbers = {1, 2, 3, 4, 5};
Step Two: Convert the numbers array to an instance of java.util.List.
In step two we’ll use the asList static method in the java.util.Arrays class to help convert the numbers array into a List of type Integer. The exampleList is of type java.util.Arrays$ArrayList — if we want an instance of java.util.ArrayList then we need to perform an extra step.
java.util.List exampleList = java.util.Arrays.asList (numbers);
Step Three (OPTIONAL): Create an instance of java.util.ArrayList from the exampleList.
In step three we explicitly create an instance of java.util.ArrayList and add the elements from the exampleList created in step two to this object as follows:
int initialCapacity = exampleList.size ();
java.util.List exampleArrayList = new java.util.ArrayList<> (initialCapacity);
exampleArrayList.addAll (exampleList);
Passing the size of the exampleList as the ArrayList initialCapacity can prevent resizing when the the addAll method is invoked and hence may offer a slight efficiency advantage when many objects are being added to the exampleArrayList.
This slight efficiency advantage may not amount to much if this happens only a few times every now and then however if this happens frequently then the application performance will incur an aggregate time complexity and memory allocation performance hit that can be easily addressed as we’ve discussed here.
See also the ArrayList.java implementation in Java 8 — in particular the constructor (155 and 169), grow method implementation (232), and addAll method implementation (752).
The complete example appears below and this script can be run directly via your browser via tio.run.
The gist for the complete example below is also available on GitHub.
public class Main {
public static void main(String[] args) {
Integer[] numbers = {1, 2, 3, 4, 5};
/* This returns an instance of java.util.Arrays$ArrayList -- see lines 21 and 23
* which converts this into an instance of java.util.ArrayList.
*/
java.util.List exampleList = java.util.Arrays.asList (numbers);
System.out.println ("exampleList.class.name: " + exampleList.getClass ().getName ());
exampleList.forEach (item -> System.out.println ("item: " + item));
int initialCapacity = exampleList.size ();
java.util.List exampleArrayList = new java.util.ArrayList<> (initialCapacity);
exampleArrayList.addAll (exampleList);
System.out.println ("exampleArrayList.class.name: " + exampleArrayList.getClass ().getName ());
exampleArrayList.forEach (item -> System.out.println ("item: " + item));
System.exit (0);
}
}
Turn An Array Into An ArrayList In Java using the addAll method from Your Browser!
We can transform an array to an ArrayList directly in our browser using the Try It Online (tio.run) interpreter.
Try It Online Example Script with Output
We can see the code above as it should appear in the Try It Online Java 8 interpreter and we can run this script by clicking on the play icon which the red pointer points to.
In an effort to be precise the picture below shows us that the ArrayList returned from the call to asList is an inner class implementation in the Arrays class and not a reference to java.util.ArrayList.
This is important because the Arrays$ArrayList implementation does not support all of the optional methods available in the java.util.List specification.
We can see the output when the script is run below:
Performance considerations when turning an array into an ArrayList using the addAll method
This example seems to be the second best-performing of the group in this article to date given the simple tests I’ve included below.
The addAll method converts the collection c into an array and since we set the initial capacity the call the grow the elementData array on line #761 should be unnecessary and instead the arrays are merged.

I’ve included a basic example script below which is written in Groovy and which demonstrates the total time in milliseconds to create an instance of java.util.ArrayList by invoking the constructor and passing the initialCapacity and then using the ArrayList addAll method to populate the target ArrayList with data.
In this example, we can see that it took an average of ~165 seconds for this script to run to completion five times.

This concludes this example regarding how to turn an Array into an ArrayList using the addAll method in Java.
In the next example we’ll use the Java Stream API to transform the Java array to an instance of java.util.ArrayList.
Example Two: Transform an Array to an ArrayList using the Java Stream API.
This example is a bit more interesting when compared with the previous example however we are able to do everything on one line if we choose to and and the result is an instance of java.util.ArrayList, which is what we need.
Using Java Stream API is also the best-performing solution for converting a Java array into an ArrayList that we cover in this article.
The following source code relies on classes in the java.util.stream package, which is a fluent API used for processing sequences of elements, such as collections, in a functional and declarative manner.
The Java Stream API is available since Java 8.
The gist for the complete example below is available on GitHub.
import java.util.stream.Collectors;
import java.util.List;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
List exampleList =
Arrays
.stream(numbers)
.boxed()
.collect(
Collectors.toList()
);
System.out.println ("exampleList.getClass (): " + exampleList.getClass ());
exampleList.forEach (item -> System.out.println ("item: " + item));
System.exit (0);
}
}
What’s going on here?
In simple terms this code transforms an array of primitive integers into a list of type java.util.ArrayList containing the java.lang.Integer object equivalents.
Let’s take a look at each method call individually:
The Arrays stream method call
The Arrays stream method creates and returns a sequential IntStream with the specified numbers array as its source.
The IntStream specification supports sequential and parallel aggregate operations on a sequence of primitive integer elements.
The support for parallel operations delivers improved performance for certain tasks by leveraging multi-core architectures.
The IntStream stream is specialized for dealing with sequences of primitive integer values without the boxing overhead which can be more efficient than working with streams of Integer objects.
The IntStream boxed method
The IntStream boxed method converts (boxes) the primitive integer values from the stream into their java.lang.Integer object equivalent.
The Stream collect method
The Stream collect method adds the boxed Integer objects into the java.util.List instance returned from the call to the Collectors toList method.
The Collectors toList method
The Collectors toList method provides a java.util.stream.Collector that, when used with the Stream’s collect method, accumulates the Stream’s elements into an instance of java.util.List.
The reference to the instance of java.util.List containing the java.lang.Integer values is then returned, and this reference is assigned to the exampleList variable.
In this example the java.util.List is an instance of java.util.ArrayList however we need to be careful since the toList method makes no guarantees regarding the type, mutability, serializability, or thread-safety of the java.util.List instance returned.
As per the Collectors toList method documentation, when control over the specific type of java.ultil.List is required, we should use the Collectors toCollection method.
Step-by-step instructions regarding how to convert an array to an ArrayList using the Java Stream API!
In this example, we’ll detail what’s required to turn an array into an ArrayList using only the required code to transform a Java array into an instance of an ArrayList. Classes required are available from Java 8 onward.Step One: Import required classes
We need to import several classes, including Java List, Arrays, and Collectors — Collectors, specifically, are part of the Java Stream API.
import java.util.stream.Collectors;
import java.util.List;
import java.util.Arrays;
Step Two: Perform the Java array to ArrayList conversion
In step two we’ll transform the Java array to ArrayList conversion using the Java Stream Fluent API.
We could write this code on a single line however for clarity purposes we place individual method calls on a new line in order to make it easier to understand.
List exampleList =
Arrays
.stream(yourObjectArray)
.boxed()
.collect(
Collectors.toList()
);
Step Three: Test your code!
Perform whatever tests are required in order to ensure that the Java array to ArrayList transformation has been completed successfully.
In the example below we simply print both the result as well as the result’s type so we can see the content as well as the List implementation, in order to confirm that the class is the one we expect to see..
Turn An Array Into An ArrayList using the Java Streams API from Your Browser!
We can transform an array to an ArrayList using the Java Streams API directly in our browser using the Try It Online (tio.run) interpreter.
Try It Online Example Script and Output
We can see the code above as it should appear in the Try It Online Java 8 interpreter and we can run this script by clicking on the play icon which the red pointer points to.
We can see in this image that the exampleList is of type java.util.ArrayList and that it also contains the Integers one through five.
Performance considerations when turning an array into an ArrayList using the java.util.stream API
The code to turn an array into an ArrayList in Java using the java.util.stream API is available as a Gist on GitHub and can be reviewed below along with output.
In this example, we’re using the Groovy Scripting Language to test the performance when performing the conversion.

This example seems to be, by far, the best-performing of the group in this article to date given the simple tests I’ve included below, and takes ~11 seconds, on average, to run.
The gist for the complete example is available on GitHub.
Example Three: Copy an array to an ArrayList in Java Using Google Guava
If your project is using the Google Guava core libraries for Java then it’s possible to convert an array to a List in java in a single line, and we demonstrate how this is done in this example.
Line #4 below contains a static import for the newArrayList method and line #8 demonstrates how this is used.
@Grapes(
@Grab(group='com.google.guava', module='guava', version='32.1.3-jre')
)
import static com.google.common.collect.Lists.newArrayList
def numbers = [1, 2, 3, 4, 5] as int[]
def result = newArrayList (numbers)
println "numbers.class: ${numbers.class.name}, result.class: ${result.class.name}, result: $result"
return;
Due to security constraints we are unable to run this example using tio.run so as an alternative I’ve executed this code locally using the GroovyConsole.
We can see the result when we run this example using the GroovyConsole below.
On line #8 we can see the Guava List newArrayList method and the pointers towards the bottom of the image below indicate that we have an ArrayList correctly containing the contents of the numbers array.

The gist regarding how to convert array to list java example is available on GitHub.
Performance considerations when transforming an array into an ArrayList in Java using Google Guava
Google Guava, in this example, came in last place insofar as performance goes.
Converting an array to an ArrayList in Java took ~166 seconds, as we can see in the image below.

The gist for transforming a Java array to ArrayList is available on GitHub.
I’ve not had the opportunity to review what’s going on in the Guava code yet and may do this in a future update to this article.
Article Conclusion
I’ve added a chart below so that we can see the performance comparison side-by-side when creating an ArrayList from an array in Java.
As stated previously, and assuming that there’s nothing inherently wrong with my simple performance tests, it appears that the Java Stream API example for copying a Java array into an ArrayList performed the best (example two) whereas the obvious example (example zero) seems to have performed the worst.
If the time to perform these operations was similar then one could argue that it doesn’t matter much which solution a developer chooses, however, the performance of the Java Stream API in Example Two is significant enough such that if we need to turn a Java array into an ArrayList, we should consider this as the best performing solution.
In closing, a developer who is working on a process where time is not a factor may somewhat appropriately argue that a slower solution doesn’t matter much — that is only if we ignore the bill, which I’ll get to in a moment.
The more speed matters the more we should be looking for opportunities to optimize, and this is one of them, especially if it’s happening in multiple places and all the time.
Cloud platforms such as Amazon AWS have a billing model where the cost increases the more you use it.
The charging model for AWS means that slower processes will result in higher bills, so it behooves your business to look for performance improvement opportunities, where practical.
To obtain better insight regarding your use of AWS resources, see my articles regarding the purpose of cost allocation tags in AWS and how you can use AWS Cost Allocation Tags to lower your AWS bills.
About This Article
This article is a content experiment based on research from the StackOverflow post entitled How to convert int[] into List<Integer> in Java? along with some observations discovered while doing keyword research using SEMrush.