What you are doing may be the simplest way, provided your stream stays sequential—otherwise you will have to put a call to sequential() before forEach.

The reason the call to sequential() is necessary is that the code as it stands (forEach(targetLongList::add)) would be racy if the stream was parallel. Even then, it will not achieve the effect intended, as forEach is explicitly nondeterministic—even in a sequential stream the order of element processing is not guaranteed. You would have to use forEachOrdered to ensure correct ordering. The intention of the Stream API designers is that you will use collector in this situation, as below:

targetLongList = sourceLongList.stream()
    .filter(l -> l > 100)
    .collect(Collectors.toList());
Answer from Maurice Naftalin on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-get-arraylist-from-stream-in-java-8
How to Get ArrayList from Stream in Java 8? - GeeksforGeeks
July 17, 2024 - This Java program converts a Stream to an ArrayList using the Collectors.toCollection method.
🌐
Java67
java67.com › 2016 › 03 › how-to-get-arraylist-from-stream-in-java8-example.html
How to Convert Stream to ArrayList in Java 8 - Collectors.toCollection() Example | Java67
But when you use toList() method there are no guarantees on the type, mutability, serializability, or thread-safety of the List returned, that's why if you need an ArrayList, you should use toCollection() method.
🌐
Baeldung
baeldung.com › home › java › java collections › converting a collection to arraylist in java
Converting a Collection to ArrayList in Java | Baeldung
June 9, 2025 - In our previous guide to ArrayList, we learned that the ArrayList constructor can accept a collection parameter: ArrayList<Foo> newList = new ArrayList<>(srcCollection); The new ArrayList contains a shallow copy of the Foo elements in the source collection. The order is the same as one in the source collection. The simplicity of the constructor makes it a great option in most scenarios. Now, let’s take advantage of the Streams API to create an ArrayList from an existing Collection:
🌐
Medium
neesri.medium.com › how-to-convert-a-stream-to-list-set-map-and-different-types-of-collections-1902c849e34a
How to Convert a Stream to List, Set, Map and different types of collections. | by A cup of JAVA coffee with NeeSri | Medium
July 25, 2025 - For instance, if your stream was [1, 2, 2, 3, 4, 4], the resulting list would be [1, 2, 2, 3, 4, 4], which contains duplicates. Type Inference: Java uses type inference in the lambda expressions to determine what type of collection (like ArrayList or LinkedList) should be returned.
🌐
Java67
java67.com › 2017 › 04 › how-to-convert-java-8-stream-to-array-and-list-in-java.html
How to convert Java 8 Stream to Array and ArrayList in Java? Example Tutorial | Java67
All you need to do is provide the ArrayList::new as supplier and toCollection() method will wrap all elements of a stream in an ArrayList and return its reference to you. You can read Java SE 8 for the Really Impatient bookto learn more about ...
🌐
Baeldung
baeldung.com › home › java › java streams › collecting stream elements into a list in java
Collecting Stream Elements into a List in Java | Baeldung
December 3, 2025 - However, there have been change requests for a method to get a List directly from a Stream instance. With the Java 16 release, we can now invoke toList(), a new method directly on the Stream, to get the List.
Find elsewhere
🌐
Benchresources
benchresources.net › home › java › java 8 – convert stream to arraylist
Java 8 - Convert Stream to ArrayList - BenchResources.Net
June 3, 2022 - Stream of String tokens Stream<String> nameStream = Stream.of( "Rajiv", "Anbu", "Santosh", "Abdul", "Lingaraj" ); // 2. convert Stream<String> to List<String> ArrayList<String> names = nameStream .collect(Collectors.toCollection(ArrayList::new)); // 3. print to console System.out.println("Stream ...
🌐
Tabnine
tabnine.com › home page › code › java › java.util.arraylist
java.util.ArrayList.stream java code examples | Tabnine
public String usage() { StringBuilder sb = new StringBuilder(); if ( !namedArgs.isEmpty() ) { sb.append( namedArgs.values().stream().map( NamedArgument::usage ).collect( Collectors.joining( " " ) ) ); } if ( !positionalArgs.isEmpty() ) { sb.append( " " ); positionalArgs.sort( Comparator.comparingInt( PositionalArgument::position ) ); sb.append( positionalArgs.stream().map( PositionalArgument::usage ).collect( Collectors.joining( " " ) ) ); } return sb.toString().trim(); } origin: spotbugs/spotbugs · public List<AncestorNodeReference> getAncestors() { List<AncestorNodeReference> ancestorNodes; ancestorNodes = new ArrayList<AncesterNodeReferenceDTO>() .stream() .map(this::createAncestorNodeReference) .collect(java.util.stream.Collectors.toList()); return ancestorNodes; } origin: jooby-project/jooby ·
🌐
Blogger
javarevisited.blogspot.com › 2015 › 03 › 5-ways-to-convert-java-8-stream-to-list.html
5 ways to Convert Java 8 Stream to List - Example, Tutorial
October 6, 2021 - In this example, we are collecting Stream elements into ArrayList. The toColection() method returns a Collector that accumulates the input elements into a new Collection, in encounter order.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › stream › package-summary.html
java.util.stream (Java Platform SE 8 )
2 weeks ago - As an example of how to transform a stream pipeline that inappropriately uses side-effects to one that does not, the following code searches a stream of strings for those matching a given regular expression, and puts the matches in a list. ArrayList<String> results = new ArrayList<>(); stream.filter(s -> pattern.matcher(s).matches()) .forEach(s -> results.add(s)); // Unnecessary use of side-effects!
🌐
Stack Abuse
stackabuse.com › java-8-streams-convert-a-stream-to-list
Guide to Java 8 Collectors: Definitive Guide to toList()
November 19, 2024 - List list = Stream.of("David", "Scott", "Hiram").collect(Collectors.toList()); System.out.println(String.format("Class: %s\nList: %s", list.getClass(), list)); This example is rather simple and just deals with Strings: Class: class java.util.ArrayList List: [David, Scott, Hiram]
Top answer
1 of 10
1653

The easiest method is to use the toArray(IntFunction<A[]> generator) method with an array constructor reference. This is suggested in the API documentation for the method.

String[] stringArray = stringStream.toArray(String[]::new);

It finds a method that takes in an integer (the size) as argument, and returns a String[], which is exactly what (one of the overloads of) new String[] does.

You could also write your own IntFunction:

Stream<String> stringStream = ...;
String[] stringArray = stringStream.toArray(size -> new String[size]);

The purpose of the IntFunction<A[]> generator is to convert an integer, the size of the array, to a new array.

Example code:

Stream<String> stringStream = Stream.of("a", "b", "c");
String[] stringArray = stringStream.toArray(size -> new String[size]);
Arrays.stream(stringArray).forEach(System.out::println);

Prints:

a
b
c
2 of 10
69

If you want to get an array of ints, with values from 1 to 10, from a Stream<Integer>, there is IntStream at your disposal.

Here we create a Stream with a Stream.of method and convert a Stream<Integer> to an IntStream using a mapToInt. Then we can call IntStream's toArray method.

Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9,10);
//or use this to create our stream 
//Stream<Integer> stream = IntStream.rangeClosed(1, 10).boxed();
int[] array =  stream.mapToInt(x -> x).toArray();

Here is the same thing, without the Stream<Integer>, using only the IntStream:

int[]array2 =  IntStream.rangeClosed(1, 10).toArray();
🌐
Dot Net Perls
dotnetperls.com › stream-java
Java - Stream Examples - Dot Net Perls
So We must use the stream() directly when we get it from an ArrayList. We "cannot cast from a Stream int to an IntStream." import java.util.ArrayList; import java.util.stream.Stream; public class Program { public static void main(String[] args) { // Create an Integer ArrayList and add three ...
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java collect stream to list (with examples)
Java Collect Stream to List (with Examples)
April 29, 2024 - The toList() method has been added in Java 16. It is a default method that collects the stream items into an unmodifiable List. The returned list is an implementation of Collections.unmodifiableList(new ArrayList<>(Arrays.asList(stream.toArray()))) ...
🌐
Blogger
javarevisited.blogspot.com › 2017 › 01 › 3-ways-to-convert-java-8-stream-to-array.html
3 Ways to Convert Java 8 Stream to an Array - Lambda Expression and Constructor Reference Example
September 7, 2021 - This is quite straightforward, no trick about this one. You can simply call the toArray() method on Stream and it will give you an object array which contains all elements of the corresponding stream, as shown in the following example:
🌐
GeeksforGeeks
geeksforgeeks.org › java › program-to-convert-list-to-stream-in-java
Program to Convert List to Stream in Java - GeeksforGeeks
July 11, 2025 - Convert Stream into List using List.stream() method. ... // Java Program to convert // List to Stream in Java 8 import java.util.*; import java.util.stream.*; import java.util.function.Function; class GFG { // Generic function to convert a list ...