Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 and aggregate operations on stream

When we use methods like filter, mapToInt, sum, etc. and pass them lambda expressions I don't understand if the operations are method themselves or are the lambda that we pass.

I' d like to know the correct terminology.

I think that the lambda is the function and thus the operation that we pass to methods that use that to produce a results.

Why is also said that filter, sum, etc. are operations that use function as their arguments?

Are both correct terminology?

like image 220
xdevel2000 Avatar asked Mar 15 '26 17:03

xdevel2000


1 Answers

Both the Stream methods and the lambda arguments that they accept are, broadly speaking, operations. This isn't confusing once we get used to the idea that the arguments to method calls can be functions. A Stream method applies the function that it's been given to the values in its stream, either to produce a new stream (intermediate methods) or to produce some aggregated result (terminal methods).

For a more detailed explanation, see http://www.lambdafaq.org/why-are-lambda-expressions-being-added-to-java/

like image 146
Maurice Naftalin Avatar answered Mar 18 '26 07:03

Maurice Naftalin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!