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?
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));
}
};
}
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With