Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Streams from collections: stream(), parallelStream(), and List::stream

Create streams from collections, distinguish execution mode from element order, and use `List::stream` to flatten nested lists.

Version: Java SE 21

Syntax

// Default instance methods declared by java.util.Collection<E>
default Stream<E> stream()
default Stream<E> parallelStream()

// Calls on a Collection<E> instance
collection.stream()
collection.parallelStream()

// Unbound instance method reference for a Stream<List<E>> named lists
lists.flatMap(List::stream)
// Equivalent lambda: lists.flatMap(list -> list.stream())

Collection.stream names the method, and Collection.stream() identifies its zero-parameter form. It is a default instance method of java.util.Collection<E>, available since Java 8. Calling collection.stream() returns a sequential java.util.stream.Stream<E> with the collection as its source. The stream is not a copy of the collection.

Collection.parallelStream is another zero-parameter default instance method, also available since Java 8. Calling collection.parallelStream() returns a possibly parallel stream: the default implementation returns a parallel stream, but another implementation may return a sequential one. Do not assume a speedup. A list has an encounter order, regardless of execution mode. toList() preserves the stream's encounter order when defined and returns an unmodifiable list. In parallel mode, forEach() does not guarantee that order; use forEachOrdered() if you need it.

List inherits stream() from Collection. List::stream is an unbound reference to that instance method, not a static call. In flatMap(List::stream), each inner list becomes the receiver of stream(), just as in flatMap(list -> list.stream()). flatMap combines the elements of the inner streams into one flat stream.

Intermediate operations such as flatMap and filter are lazy: a terminal operation such as toList() starts processing. Do not reuse a stream after a terminal operation; obtain a fresh stream from the collection for a new pipeline. Do not modify a non-concurrent source while the pipeline runs, and avoid mutating shared state in lambdas.

Examples

import java.util.Collection;
import java.util.List;

public class CollectionStreamsDemo {
    public static void main(String[] args) {
        Collection<List<Integer>> groups = List.of(List.of(3, 4), List.of(2, 1));

        List<Integer> sequential = groups.stream()
                .flatMap(List::stream)
                .filter(n -> n % 2 == 0)
                .toList();

        List<Integer> possiblyParallel = groups.parallelStream()
                .flatMap(List::stream)
                .filter(n -> n % 2 == 0)
                .toList();

        System.out.println(sequential);
        System.out.println(possiblyParallel);
    }
}

Result

[4, 2]
[4, 2]

Both pipelines flatten the lists, keep the even numbers, and preserve their order. Both lines are expected to be `[4, 2]`; the output does not show how many threads were used.

Related lessons

  • Java · Lesson 63 · Basic · Version: 2
  • Java · Lesson 64 · Basic · Version: 2
  • Java · Lesson 65 · Basic · Version: 2
  • Java · Lesson 66 · Basic · Version: 2
  • Java · Lesson 67 · Basic · Version: 2
  • Java · Lesson 41 · Intermediate · Version: 2
  • Java · Lesson 42 · Intermediate · Version: 2
  • Java · Lesson 43 · Intermediate · Version: 2
  • Java · Lesson 44 · Intermediate · Version: 2
  • Java · Lesson 45 · Intermediate · Version: 2
  • Java · Lesson 57 · Intermediate · Version: 2

Sources