简体   繁体   English

Java 8和流上的聚合操作

[英]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. 当我们使用像filtermapToIntsum等方法并传递它们的lambda表达式时,我不明白这些操作本身是方法还是我们传递的lambda。

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. 我认为lambda是函数,因此我们传递给使用它来产生结果的方法的操作。

Why is also said that filter , sum , etc. are operations that use function as their arguments? 为什么还说过filtersum等是使用函数作为参数的操作?

Are both correct terminology? 这两个都是正确的术语吗?

Both the Stream methods and the lambda arguments that they accept are, broadly speaking, operations. 从广义上讲,Stream方法和它们接受的lambda参数都是操作。 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). Stream方法将其赋予的函数应用于其流中的值,以生成新流(中间方法)或生成一些聚合结果(终端方法)。

For a more detailed explanation, see http://www.lambdafaq.org/why-are-lambda-expressions-being-added-to-java/ 有关更详细的说明,请参阅http://www.lambdafaq.org/why-are-lambda-expressions-being-added-to-java/

Not sure if this is commonly accepted, but I think it is thus: 不确定这是否被普遍接受,但我认为这是:

A function is something that receives arguments and produces a value, ideally without side effects (though, that is not enforcable in Java). 函数是接收参数并生成值的函数,理想情况下没有副作用(但是,这在Java中是不可执行的)。 Use this if you want to emphasize the mathematical/functional aspect. 如果您想强调数学/功能方面,请使用此选项。

A subroutine/procedure is a named piece of code that is reused for it's side effect. 子程序/过程是一段经过命名的代码,可以重复使用它的副作用。

A method is how functions and subroutines are implemented/written in Java. 方法是如何用Java实现/编写函数和子例程。 There is no such thing as a function or procedure that does not belong to some class. 不存在不属于某个类的函数或过程。

A lambda expression in Java is a way to write methods (of some anonymous class that happens to implement a functional interface) on the fly and at the same time obtain a reference to an instance of said interface. Java中的lambda表达式是一种在运行中编写方法(一些恰好实现功能接口的匿名类)的方法,同时获得对所述接口的实例的引用。

An operation is a function or procedure. 操作是功能或过程。

So, depending on how you want to look at it: Since it is about Java, you could just call everything "method". 所以,取决于你想看它的方式:因为它是关于Java的,你可以把所有东西称为“方法”。 But sometimes you want to emphasize different aspects. 但有时你想强调不同的方面。 Like in your example: 就像你的例子:

filter, sum, etc. are operations that use function as their arguments filter,sum等是使用函数作为参数的操作

Here, we could say: " filter is a method that takes a reference to a functional interface as argument", but this somehow changes the intention of the sentence. 在这里,我们可以说:“ filter是一种将功能接口作为参数引用的方法”,但这会以某种方式改变句子的意图。

Lambda is a function/callback which is usually passed as an argument. Lambda是一个函数/回调函数,通常作为参数传递。 An approach to understand this, consider you need to search for a value in a list: 理解这一点的方法,考虑您需要在列表中搜索值:

Java 7: Java 7:

int i = Arrays.asList(1,2,3,4,5,6).indexOf(3);

This will give a single element. 这将给出一个元素。 Now what happens, when one needs a more dynamic request - he could collect these items with a for-loop. 现在发生了什么,当需要更动态的请求时 - 他可以用for循环收集这些项目。 But if he wants to do it in a similar fashion, he could pass a lambda as an argument: 但如果他想以类似的方式做到这一点,他可以传递一个lambda作为一个论点:

Java 8 (how it could be): Java 8(它是如何):

List<Integer> collect = asList(1, 2, 3, 4, 5, 6).filter(x -> x >= 2 && x < 4);

the real Java 8 API example is a bit more verbose: 真正的Java 8 API示例有点冗长:

List<Integer> collect = Arrays.asList(1, 2, 3, 4, 5, 6)
    .stream()
    .filter(x -> x >= 2 && x < 4)          
    .collect(Collectors.toList());

Remember, what we pass to the filter is as a function is just a filtering logic. 请记住,我们传递给过滤器的功能只是一个过滤逻辑。 While the filter function already expect a function that has the filtering logic. 虽然过滤器功能已经期望具有过滤逻辑的功能。 Filter function specifically accepts a function that returns boolean and decides whether to pass on the currently element in the stream or simply discard it. Filter函数专门接受一个返回boolean的函数,并决定是传递流中的当前元素还是简单地丢弃它。

I hope I have understood your question correctly. 我希望我能正确理解你的问题。 For more on streams here is a ongoing series of article on Java 8 Stream API amitph.com > Java 8 Streams API Tutorials 有关流的更多信息,请参阅 Java 8 Stream API amitph.com> Java 8 Streams API教程的一系列文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM