Java SE API

Stream.toList()

Produces an unmodifiable list in encounter order if present; does not freeze elements. Since Java 16. Java SE API; example uses Java 21-compatible syntax.

Formal Syntax & Structure

java syntaxjava
default List<T> toList()

Return Value

List<T>

Produces an unmodifiable list in encounter order if present; does not freeze elements. Since Java 16.

Working Example

Example Snippetjava
import java.util.*;import java.util.stream.*;public class Main {    public static void main(String[] args) {        List<Integer> stock = List.of(0, 4, 2, 0, 7);        var available = stock.stream().filter(n -> n > 0);        System.out.println(available.toList());        System.out.println(stock);    }}
Java Reference