Tutorial: Learn how to convert an array to an ArrayList with the Java Stream API! 💎

In this introductory Java guide we’ll review how to convert a Java array to an instance of java.util.ArrayList using the Java Stream API.

See the parent instructional regarding how to convert an array into an ArrayList in Java for other options.

This example is slightly more interesting when compared with other options and 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.

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

In the next section we’ll step through this example and explain what each step does.

This post was updated on June 10, 2025.

Need Strategic CTO Consulting?

If you’re looking to hire an Interim CTO or find a Fractional CTO, I can help -- schedule an appointment today to discuss your requirements.

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 the Java Stream API.

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: Create some sample data (optional)

We need some sample data to work with and so we’ll create an array of primitive integers here.

Note that we’re using a primitive array of integers which means that we’ll need to box the values in the array such that the primitive integer values are converted into their object wrapper equivalent.

				
					int[] numbers = {1, 2, 3, 4, 5};
				
			

Step Three: Perform the Java array to ArrayList conversion using the Java Stream API.

In this step we’ll transform the Java primitive array of integers to an ArrayList using the Java Stream Fluent API.

We could write this code on a single line however for clarity we place individual method calls on a new line in order to make it easier to understand.

				
					List<YourObject> exampleList =
  Arrays
    .stream(yourObjectArray)
    .boxed()
    .collect(
      Collectors.toList()
    );
				
			

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 Four: Test your code! (optional)

Perform whatever tests are required in order to ensure that the Java array to ArrayList transformation via the Java Stream API has been completed successfully.

In the example below we simply print both the result values as well as the result’s type so we can see the content as well as the List implementation, in order to confirm that everything is as we expect.

				
					System.out.println ("exampleList.getClass (): " + exampleList.getClass ());

    exampleList.forEach (item -> System.out.println ("item: " + item));
				
			

Step Five: Review the output (optional)

Visually inspect the result and determine that an instance of ArrayList was returned from the call to the Java Stream API and contains the values we expect to see (that being integers one through five).

Script and output from tio.run which demonstrates how to convert a primitive integer array in Java to an ArrayList using a stream. Note that the exampleList is an instance of ArrayList and contains the values 1 - 5.
The Java Stream API: boxed collectors toList example output.

Run this example 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: How To Convert An Array To An ArrayList In Java
Try It Online: How to transform an array to an instance of java.util.ArrayList using Java Streams

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.

The tio.run (Try It Online) script and output with pointers to the button to click in order to run the script along with a pointer to the output.
Transform an Array to an ArrayList using Java Streams methods.

We can verify in this image that the exampleList is of type java.util.ArrayList and that it also contains the Integers one through five.

Are you struggling with Autoboxing performance in Java?

Avoid common performance pitfalls caused by autoboxing in Java. Discover when it’s helpful, when it hurts, and how to optimize your code for speed and clarity -- perfect for Java developers ready to level up.

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 (Groovy) to test the performance when performing the conversion.

See also the Apache Groovy page on Wikipedia and the Groovy Script Examples category for other articles that contain Groovy examples.

Example Two: performance test script for the java.util.stream approach to converting an array to an ArrayList in Java with a pointer to the section of code where the conversion takes place and the average time to run this script five times at around 10.5 seconds.
How to transform a Java array to an ArrayList using the java.util.stream API.

This example seems to be, by far, the best-performing of the group in this tutorial to date given the simple tests I’ve included below, and takes ~11 seconds, on average, to run.

Note that line #16 means we need to call the boxed method on line 31 yielding performance results that are not limited to the Java Streams API alone.

If we change line #16 to use a java.lang.Integer and we remove the call to boxed on line 31 the average time to run this script five times is 8.7 seconds.

The gist for the complete example is available on GitHub.

Conclusion

Working with the Java ArrayList and Java Streams typically yields more concise and expressive code when it comes to manipulating collections.

As shown in this article, using streams can drastically reduce the need for boilerplate code while improving readability.

This guide was inspired by a popular question on StackOverflow entitled “How to convert int[] into List<Integer> in Java?” and expands on it with practical examples and deeper explanations.

See Also

  1. Tutorial: Learn how to convert array into ArrayList in Java.
  2. Use the Java ArrayList constructor to convert an array of integers to an instance of List.
  3. Tutorial: Learn how to use the Guava Lists.newArrayList method now!
  4. Tutorial: Learn how to use the Arrays.asList method in Java now!
author avatar
ThosPFuller
I am a software engineer based in Northern Virginia (USA) and this website focuses on content engineering, web development, Technical SEO, Search Engine Optimization (SEO).

Leave a Reply