Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Streams intermediate operations produce new streams?

When I invoke intermediate operations on streams, do they create new stream for each intermediate operation?

List<Integer> list = List.of(1,2,3,4,5);

list.stream().filter(i -> i > 4).map(o -> o*2).collect(Collectors.toList());

How many streams did it create? 1 or 3?

like image 567
Faraz Avatar asked Sep 12 '25 02:09

Faraz


1 Answers

Basically, one of the concepts of functional programming is pure functions that deterministic and have no-side effect that called immutability. All of the intermediate operations in the Stream api are immutable and returns new Stream.

As mentioned in the Oracle document, all of the intermediate operations like filter, map and etc returns new stream. For example document of map method is as follow:

Stream<R> map(Function<? super T,? extends R> mapper)

Returns a stream consisting of the results of applying the given function to the elements of this stream. This is an intermediate operation.

Type Parameters: R - The element type of the new stream Parameters:

mapper - a non-interfering, stateless function to apply to each element

Returns: the new stream

For more information, you can take a look at the Stream implementation in Java (For example map method that returns a new StatelessOp)

public final <R> Stream<R> map(Function<? super P_OUT, ? extends R> mapper) {
    Objects.requireNonNull(mapper);
    return new StatelessOp<P_OUT, R>(this, StreamShape.REFERENCE,
                                 StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
        @Override
        Sink<P_OUT> opWrapSink(int flags, Sink<R> sink) {
            return new Sink.ChainedReference<P_OUT, R>(sink) {
                @Override
                public void accept(P_OUT u) {
                    downstream.accept(mapper.apply(u));
                }
            };
        }
    };
}
like image 144
Hossein Mobasher Avatar answered Sep 13 '25 15:09

Hossein Mobasher