Java Collectors toList: Usage and Behavior
Learn how Java's Collectors.toList() works, what it returns, and how it compares with Stream.toList(), toCollection(), and toUnmodifiableList().

When you need to turn a Java Stream into a List, Collectors.toList() is often the first method that comes to mind. It is a simple, readable way to collect stream elements into a List instance, but its behavior has subtleties that affect mutability, type guarantees, and performance. This article explains how Collectors.toList() works, what it returns, and when you should choose a different collector.
What Collectors.toList() Actually Returns
Collectors.toList() is a static factory method that returns a Collector<T, ?, List<T>>. When you pass it to Stream.collect(), it accumulates the stream's elements into a List. The contract does not guarantee the concrete type of the returned List. In the OpenJDK implementation, it returns an ArrayList, but code that depends on that type, such as casting to ArrayList, is fragile and may break if the implementation changes.
The collector is a mutable-reduction collector. For parallel streams, the stream framework gives each worker its own accumulator and combines the partial lists with the collector's combiner, so you do not need to synchronize calls to the collector itself. The returned List is not thread-safe, so it should not be mutated concurrently after collection. The parallel reduction merges partial results by combining lists; it does not use a ConcurrentHashMap.
Basic Usage and Syntax
A minimal example shows how to collect a stream of strings into a list:
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
List<String> names = Stream.of("Alice", "Bob", "Carol")
.collect(Collectors.toList());The collect method is the terminal operation that triggers the stream pipeline. The collector receives each element and adds it to an internal accumulator, which is an ArrayList in the OpenJDK implementation. After the stream is exhausted, the accumulator is returned as the result list.
This approach works for any stream source: collections, arrays, generators, or I/O. The stream's element type becomes the list's generic type, so type safety is preserved.
When to Use toList() vs. toCollection()
Collectors.toList() is convenient, but it gives you no control over the list implementation. If you need a specific list implementation, such as a LinkedList, use Collectors.toCollection() with a supplier. For example:
List<String> linkedList = stream.collect(Collectors.toCollection(LinkedList::new));This is also the way to collect into a list with a specific initial capacity, though that is rarely necessary. The choice matters when you rely on list characteristics like insertion order, random access, or memory footprint. ArrayList provides O(1) random access but O(n) insertion in the middle. LinkedList offers O(1) insertion at the ends but poor random access. If your downstream code only iterates, the implementation rarely matters.
Collectors.toList() returns a mutable list. If you need an unmodifiable list, use Collectors.toUnmodifiableList() or, from Java 16 onward, Stream.toList(). Do not use toCollection() for that purpose: its supplier must return a mutable accumulator that the collector can add elements to.
Stream.toList() vs. Collectors.toList()
Since Java 16, Stream.toList() provides a direct way to get a list from a stream. It returns an unmodifiable list, meaning you cannot add, remove, or replace elements. This is a significant difference from Collectors.toList(), which returns a mutable list. The two methods also differ in null handling: Stream.toList() does not allow null elements and throws NullPointerException if the stream contains a null. Collectors.toList() allows nulls.
Here is a comparison:
| Method | Mutability | Null elements | Introduced |
|---|---|---|---|
Collectors.toList() | Mutable | Allowed | Java 8 |
Stream.toList() | Immutable | Not allowed | Java 16 |
Choose Stream.toList() when you want to guarantee that the list cannot be modified, and when you are certain the stream contains no nulls. Choose Collectors.toList() when you need a mutable list or when nulls are possible.
Performance and Memory Behavior
The performance of Collectors.toList() is generally good. The accumulator starts as an empty ArrayList and grows as elements are added. The growth strategy follows ArrayList's standard doubling behavior, so the total cost of adding n elements is amortized O(n). For very large streams, the repeated resizing may cause memory churn. If you know the approximate size, you can use Collectors.toCollection() with a supplier that creates an ArrayList with an initial capacity:
List<String> list = stream.collect(Collectors.toCollection(() -> new ArrayList<>(expectedSize)));This avoids resizing overhead but requires knowing the size in advance. For most applications, the default behavior is sufficient. In parallel streams, the stream framework merges partial lists, and the final list is not thread-safe. If you need a thread-safe list, collect into a CopyOnWriteArrayList using toCollection().
Common Pitfalls and Edge Cases
One common mistake is assuming Collectors.toList() returns an ArrayList. As mentioned, the contract does not guarantee that. Another pitfall is using toList() on an infinite stream; the collector will never terminate because it waits for all elements. This is inherent to all collectors.
Another edge case is null elements. Collectors.toList() allows nulls, but some downstream operations may not. If you use Stream.toList(), nulls cause an immediate exception. Be aware of the difference when migrating code.
For an ordered stream, collect(Collectors.toList()) preserves encounter order in the resulting list, including in parallel streams. If the stream source is unordered, the order is not meaningful. In a parallel stream, forEach does not guarantee encounter order; use forEachOrdered when you need ordered processing.
Choosing the Right Collector for Your Use Case
The decision among Collectors.toList(), Collectors.toCollection(), Stream.toList(), and Collectors.toUnmodifiableList() depends on your requirements:
- Use
Collectors.toList()when you need a mutable list and are okay with the default implementation. - Use
Collectors.toCollection()when you need a specific list type or initial capacity. - Use
Stream.toList()when you are on Java 16+ and want an immutable list with no null elements. - Use
Collectors.toUnmodifiableList()when you are on Java 10+ and want an immutable list through theCollectorsAPI; it also rejects null elements.
Each collector has a clear role. The choice affects mutability, null tolerance, and performance characteristics. For most simple cases, Collectors.toList() is the right starting point, but understanding the alternatives helps you write more robust and intention-revealing code.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.