Java 25 (LTS) reference · Stream API

Java Stream toList() Method

Collect a stream's elements into an unmodifiable List. Shorter than collect(Collectors.toList()).

Java 25 (LTS) Since Java 16

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

stream.toList()

Return value

An unmodifiable List<T>.

Examples

Collect results
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> squares = Stream.of(1, 2, 3).map(n -> n * n).toList();
        System.out.println(squares);
    }
}

Output

[1, 4, 9]

Run this on your computer — see “Get Started” for setup.

It's unmodifiable
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> fixed = Stream.of(3, 1).toList();
        try {
            fixed.add(2);
        } catch (UnsupportedOperationException e) {
            System.out.println("toList() is unmodifiable; use collect(Collectors.toCollection(ArrayList::new)) to edit");
        }
    }
}

Output

toList() is unmodifiable; use collect(Collectors.toCollection(ArrayList::new)) to edit

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
Java Java 16Added

Was this page helpful?