Java 16 introduced java.util.Stream.toList method that is simplified version of stream.collect(Collectors.toList())

Let’s see an example using Stream.collect(Collectors.toList()) method

The below example checks for even and odd numbers for a given list of numbers.

package java16;

import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.List;

public class StreamToListTest {
    public static void main(String[] args) {
        List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        List<Integer> evenNumbers = numbers.stream()
                .filter(n -> n % 2 == 0)
                .map(n -> n)
                .collect(Collectors.toList());
        List<Integer> oddNumbers = numbers.stream()
                .filter(n -> n % 2 != 0)
                .map(n -> n)
                .collect(Collectors.toList());
        System.out.println(oddNumbers);
        System.out.println(evenNumbers);

    }

}

Output:

[1, 3, 5, 7, 9]
[2, 4, 6, 8, 10]

Stream toList method

This method is used to return the list from a given stream in an easier way.

    default List<T> toList() {

Returned is Unmodified List. if you tried to add or update to list, It throws UnsupportedOperationException

The same example is rewritten using Stream.toList() method.

package java16;

import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.List;

public class StreamToListTest {
    public static void main(String[] args) {
        List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        List<Integer> evenNumbers = numbers.stream()
                .filter(n -> n % 2 == 0)
                .map(n -> n)
                .toList();
        List<Integer> oddNumbers = numbers.stream()
                .filter(n -> n % 2 != 0)
                .map(n -> n)
                .toList();
        System.out.println(oddNumbers);
        System.out.println(evenNumbers);


}

Another comparision with Stream.collect(Collector.toList) method

package java16;

import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.List;

public class StreamToListTest {
    public static void main(String[] args) {
        List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        List<Integer> list = numbers.stream().toList();
        list.add(12);
        System.out.println(list);

    }

}

Output:

It throws java.lang.UnsupportedOperationException and returned list is unmodifiable and throws an error due to mutators method

Exception in thread "main" java.lang.UnsupportedOperationException
 at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:142)
 at java.base/java.util.ImmutableCollections$AbstractImmutableCollection.add(ImmutableCollections.java:147)
 at java16.StreamToListTest.main(StreamToListTest.java:23)

Difference between Stream.List and and Stream.collect(Collectors.toList())?

  • Mutable vs immutable: Stream.List returns immutable list that can not added,updated or sorted Stream.collect(Collectors.toList()) returns list that modifieable.

You can check the same above for example.

  • Null vs NullPointerException

Returns null for list of null values using Stream toList method

package java16;

import java.util.stream.Stream;
import java.util.List;

public class StreamToListTest {
    public static void main(String[] args) {
        List<Object> nullList = Stream.of(null, null).toList();
        System.out.println(nullList);

    }

}

Output:

[null,null]

Let’s see an example for collect(toList()) method with null values

package java16;

import java.util.List;

public class StreamToListTest {
    public static void main(String[] args) {
        List<Object> nullList = List.of(null, null);
        System.out.println(nullList);

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException
 at java.base/java.util.Objects.requireNonNull(Objects.java:208)
 at java.base/java.util.ImmutableCollections$List12.<init>(ImmutableCollections.java:563)
 at java.base/java.util.List.of(List.java:829)
 at java16.StreamToListTest.main(StreamToListTest.java:9)

Difference between Stream, collect toList and collect(toUnmodifiableList())

|Stream ToList | collect(toList()) | collect(toUnmodifiableList())| |:——–| ————— | |Immutable List| Mutable List|Immutable List |Not updated or sortable| Can update or sortable|Not updated or sortable| |Allow nulls| Throws NullPointerException|Throws NullPointerException|