Scrivio Interview library
CodingFoundations5 min read · updated 12 November 2026

What is the difference between Stream.toList() (Java 16) and collect(Collectors.toList())? Which would you use and when?

Stream.toList(), added in Java 16, always returns an unmodifiable list and allows null elements. collect(Collectors.toList()) makes no promise about the list type or mutability, but in practice returns an ArrayList, so code that adds to the result breaks if it is switched to toList(). Collectors.toUnmodifiableList() is also unmodifiable but rejects nulls. For new code I prefer toList(), and when I need a mutable list I say so with Collectors.toCollection(ArrayList::new).

Why this question is asked

These two calls look interchangeable, and IDEs even suggest replacing one with the other. That suggestion is safe in most code and wrong in some, and the difference only shows up at runtime with an UnsupportedOperationException far from the line that changed. Interviewers ask this to see whether we know the contracts rather than the habits. In this answer, we will recap the options for collecting a stream into a list, compare their mutability and null handling with code, see how a migration can break existing code, and finish with a clear rule for which to use.

1A Quick Recap of the Options

Before Java 10, the standard way to get a list from a stream was collect(Collectors.toList()). Java 10 added Collectors.toUnmodifiableList(), and Java 16 added toList() directly on Stream, as a shorter and more efficient way to get a list. There is also Collectors.toCollection(supplier), which lets us choose the exact collection class.

An unmodifiable list is one whose add, remove and set methods throw UnsupportedOperationException. It is not the same as an immutable list of immutable objects: the elements themselves can still be changed if they are mutable. It only guarantees that the list's structure cannot be changed through that reference.

2Mutability: The Difference That Matters Most

The documentation of Collectors.toList() says there are no guarantees on the type, mutability, serializability or thread safety of the returned list. In every JDK so far it returns an ArrayList, and a lot of code silently depends on that. toList() guarantees the opposite: the list is unmodifiable.

Java
List<String> a = Stream.of("x", "y").collect(Collectors.toList());
a.add("z"); // works today: an ArrayList
System.out.println(a); // [x, y, z]
List<String> b = Stream.of("x", "y").toList();
b.add("z"); // java.lang.UnsupportedOperationException

Notice that the failure is not at compile time. Both calls return List<String>, so the compiler cannot tell the difference, and the exception appears wherever the list is later modified, which may be in another class.

3Nulls: toList() Allows Them, the Others Do Not

toList() was designed to be a drop-in replacement for the common case, so it accepts null elements just as Collectors.toList() does. Collectors.toUnmodifiableList() and List.copyOf follow the rules of List.of and reject nulls with a NullPointerException.

Java
List<String> withNull = Arrays.asList("a", null);
withNull.stream().toList(); // [a, null]
withNull.stream().collect(Collectors.toList()); // [a, null]
withNull.stream().collect(Collectors.toUnmodifiableList()); // java.lang.NullPointerException
List.copyOf(withNull); // java.lang.NullPointerException
Ways to collect a stream into a list
CallModifiable?Nulls?Since
stream.toList()no, guaranteedallowedJava 16
collect(Collectors.toList())not specified, ArrayList in practiceallowedJava 8
collect(Collectors.toUnmodifiableList())no, guaranteedNullPointerExceptionJava 10
collect(toCollection(ArrayList::new))yes, guaranteedallowedJava 8

So toList() and toUnmodifiableList() are both unmodifiable but are not interchangeable either: switching from toList() to toUnmodifiableList() can introduce NullPointerExceptions.

4How a Migration Breaks Existing Code

Here is the typical breakage. A method collects a list and a caller, often in another module, adds to it. After a clean-up that replaces collect(Collectors.toList()) with toList(), the method still compiles and its own tests pass.

Java
// OrderQueries
List<Order> openOrders() {
return repo.findAll().stream().filter(Order::isOpen).toList(); // was collect(Collectors.toList())
}
// ReportService, in another module
List<Order> rows = queries.openOrders();
rows.add(Order.placeholder()); // UnsupportedOperationException after the change
rows.sort(byDate); // also throws: sort modifies the list

Notice that sort counts as a modification too, as do removeIf and replaceAll. When reviewing such a migration, we search for every caller of the changed method that modifies the result, or we change only methods whose results never leave a small, known scope. The behaviour change is arguably good, since returning a list that callers can modify is often a design smell, but it has to be done deliberately.

5Which One to Use and When

For new code I use toList(). It is shorter, it states clearly that the result should not be modified, it allows nulls like the old collector, and it is slightly more efficient because it can build the list directly from the stream's internal array. When I really need a mutable list, for example to add to it or sort it in place, I write collect(Collectors.toCollection(ArrayList::new)), which guarantees an ArrayList and documents the intent. I avoid relying on Collectors.toList() being mutable, because that is behaviour the documentation does not promise. I use toUnmodifiableList() only when I also want nulls rejected.

6Follow-up questions

Is the list from toList() thread safe?

Because it cannot be modified, it can be shared between threads safely once it has been published, for example through a final field or a concurrent collection. The elements themselves are a different matter: if they are mutable objects, they need their own thread safety.

Does toList() copy the data again compared with the collector?

No, it usually does less work. The default implementation collects into an array and wraps it in an unmodifiable list, while the collector grows an ArrayList as elements arrive. For sized streams toList() can allocate the exact array size up front.

Why did Java not just make Collectors.toList() unmodifiable?

Because a great deal of existing code depends on its result being an ArrayList, even though that was never promised. Changing it would have broken that code silently, so the JDK added a new method with a clear contract instead.

7Conclusion

In this answer, we compared Stream.toList() with collect(Collectors.toList()). toList() guarantees an unmodifiable list and allows nulls, while Collectors.toList() promises nothing and returns an ArrayList in practice, so switching between them breaks callers that add to, remove from or sort the result. Collectors.toUnmodifiableList() is also unmodifiable but rejects nulls. We prefer toList() for new code and Collectors.toCollection(ArrayList::new) when a mutable list is genuinely needed.

What to say in the interviewAbout 80 seconds aloud

Open with

The main difference is mutability. Stream toList, added in Java 16, guarantees an unmodifiable list, so add, remove, set or even sort throw UnsupportedOperationException. collect of Collectors toList makes no promise at all about the type or mutability, but in every JDK it returns an ArrayList, and a lot of code quietly depends on that.

Then, if they want more

Both allow null elements, which is different from Collectors toUnmodifiableList and List copyOf, which throw a NullPointerException on nulls. So they're not drop-in replacements. If I switch a method from collect toList to toList, it still compiles, but a caller somewhere that adds to or sorts the result now fails at runtime.

When I review that kind of migration, I check every caller that modifies the returned list. For new code I use toList: it's shorter, it states that the result shouldn't be modified, it handles nulls like the old collector, and it's slightly more efficient.

If I genuinely need a mutable list, I write Collectors toCollection with ArrayList colon colon new, because that guarantees an ArrayList and makes the intent explicit, instead of relying on unspecified behaviour of Collectors toList. And I'd use toUnmodifiableList only when I also want nulls rejected.

About this answer

Last updated
12 November 2026
Versions it names
Java 10, Java 16
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between Stream.toList() (Java 16) and...": https://scrivio-interview-library.netlify.app/java/streams/10251933b9/