Java 8中的操作员的便捷功能?

Pat*_*ins 7 java functional-programming java-8

在Python中,如果我想对xor操作进行折叠,我可以写:

reduce(operator.xor, my_things, 0)
Run Code Online (Sandbox Code Playgroud)

而不是更麻烦

reduce(lambda x, y: x^y, my_things, 0)
Run Code Online (Sandbox Code Playgroud)

在新的Java 8功能特性中是否有类似的东西?例如写这样的东西

myThings.reduce(0, Integer::xor)
Run Code Online (Sandbox Code Playgroud)

而不是

myThings.reduce(0, (x, y) -> x ^ y)
Run Code Online (Sandbox Code Playgroud)

Sot*_*lis 3

Integer#sum(int, int)按照您在包 private 中的建议使用它,IntPipeline但其他数字运算符没有类似的方法。

@Override
public final int sum() {
    return reduce(0, Integer::sum);
}
Run Code Online (Sandbox Code Playgroud)

您可以自己定义它们。

  • 还有`Integer.max`和`Integer.min`,所以它提供了你不需要传递给`reduce`的三个方法,因为`IntStream`已经有`sum()`,`min() `, 和 `max()` (2认同)