java 可以使用函数式操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25299749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can use functional operations
提问by rink.attendant.6
I'm using the for-each construct in Java as follows:
我在 Java 中使用 for-each 构造如下:
public int getNumRStations() {
int numRoutes = 0;
for (ArrayList<Route> route : routes) {
numRoutes += route.size();
}
return numRoutes;
}
NetBeans shows a warning/notice that says "Can use functional operations". Upon automatically resolving it, the newly generated code shows this:
NetBeans 显示一条警告/通知,内容为“可以使用功能性操作”。自动解析后,新生成的代码显示如下:
public int getNumRStations() {
int numRoutes = 0;
numRoutes = routes.stream().map((route) -> route.size()).reduce(numRoutes, Integer::sum);
return numRoutes;
}
- Why is NetBeans warning me of this? I know I'm not supposed to blindly trust IDEs, so that's why I'm asking.
- What is that new line supposed to do? I haven't seen anything like it, in real life or in class.
- 为什么 NetBeans 会警告我这一点?我知道我不应该盲目地相信 IDE,所以这就是我问的原因。
- 那条新线应该做什么?无论是在现实生活中还是在课堂上,我都没有见过这样的事情。
采纳答案by paisanco
That looks like NetBeans suggesting refactoring your sum operation as a Java 8 "lambda" or functional programming operation using the map and reduce functions from the Stream interface. You must be using a Java 8 JDK with NetBeans.
这看起来像 NetBeans 建议将您的 sum 操作重构为 Java 8 “lambda”或使用 Stream 接口中的 map 和 reduce 函数的函数式编程操作。您必须使用带有 NetBeans 的 Java 8 JDK。
Breaking down what it's doing:
分解它在做什么:
the "map()" function is performing a count of all of your
route
sizes in yourroutes
array list,the "reduce()" function is then performing a sum of those individual sizes to get the final result for the total number of routes.
“map()”函数正在对数组列表中的所有
route
大小进行计数routes
,然后,“reduce()”函数对这些单独的大小进行求和,以获得路线总数的最终结果。
The map() and reduce() functions are documented in the Java 8 documentation for the Stream interface available at this link
map() 和 reduce() 函数在 Java 8 文档中记录在此链接中可用的 Stream 接口
This answer addresses "what it is" but doesn't address "why it's better". I will admit to still learning about these constructs myself.
这个答案解决了“它是什么”,但没有解决“为什么它更好”。我承认我自己仍在学习这些结构。
回答by Naetmul
So @paisanco already explained about what each function does.
所以@paisanco 已经解释了每个函数的作用。
I agree that the modification the IDE suggested is more complex than the original.
If I were asked to select between the original one and the IDE's recommendation, then I will choose the original one.
我同意 IDE 建议的修改比原始修改更复杂。
如果让我在原始版本和 IDE 的推荐之间进行选择,那么我会选择原始版本。
However, here is a simpler (and more preferred) way for your example.
但是,对于您的示例,这是一种更简单(且更受欢迎)的方法。
public int getNumRStations() {
return routes.stream().mapToInt(x -> x.size()).sum();
}
Explanation is simpler in this case, too.
For each routes' element x
, change it into x.size()
and sum them up.
在这种情况下,说明也更简单。
对于每个路由的元素x
,将其更改为x.size()
并汇总。
x -> x.size()
is called a lambda expression, or anonymous function.
It's like
x -> x.size()
称为 lambda 表达式或匿名函数。就像是
int function(x) {
return x.size();
}
(I omitted the parameter type. The type is implicitly selected by the Java compiler.)
This function is applied to each of the collection's element. This is what mapToInt(lambda exp)
method does.
(我省略了参数类型。该类型是由 Java 编译器隐式选择的。)
此函数应用于集合的每个元素。这就是mapToInt(lambda exp)
方法的作用。
sum()
method doesn't seem to need explanation.
sum()
方法似乎不需要解释。
Simple, isn't it?
很简单,不是吗?