试图测试Java8拉姆达,但类型是混乱:Java8功能类型`(A,B) - >一个+ B`
import java.util.function.ToIntBiFunction;
import java.util.stream.IntStream;
public class Test {
public static void main(String... args) {
int sum1 = 0;
for (int n = 0; n < 10; n++) {
sum1 += n;
}
ToIntBiFunction<Integer, Integer> add = (a, b) -> a + b;
int sum2 = IntStream.range(0, 10)
.reduce(0, add); //error here
System.out.println(""+sum1);
System.out.println(""+sum2);
}
}
Test.java:15:错误:不兼容的类型:ToIntBiFunction无法转换为IntBinaryOperator .reduce(0,add);
什么来定义函数
(a,b) -> a+b
由于最通用的方法。
我喜欢内置的'Integer :: sum',非常感谢Peter! – 2014-10-09 08:10:24