Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java forEach方法接受返回值的lambda表达式。为什么我没有得到下面代码的编译问题?_Java_Lambda_Java 8_Functional Interface - Fatal编程技术网

Java forEach方法接受返回值的lambda表达式。为什么我没有得到下面代码的编译问题?

Java forEach方法接受返回值的lambda表达式。为什么我没有得到下面代码的编译问题?,java,lambda,java-8,functional-interface,Java,Lambda,Java 8,Functional Interface,在上面的代码中,forEach接受一个lambda表达式,该表达式返回一个值。但forEach on stream只接受不能从其accept方法返回任何值的使用者。为什么我没有得到这个编译错误 为什么我没有得到这个编译错误 因为方法返回的值在使用时被忽略 您还可以将其视为IntConsumer的accept方法,该方法现在如下所示: AtomicInteger value1 = new AtomicInteger(0); IntStream.iterate(1, x -> 1).limit

在上面的代码中,forEach接受一个lambda表达式,该表达式返回一个值。但forEach on stream只接受不能从其accept方法返回任何值的使用者。为什么我没有得到这个编译错误

为什么我没有得到这个编译错误

因为方法返回的值在使用时被忽略

您还可以将其视为
IntConsumer
的accept方法,该方法现在如下所示:

AtomicInteger value1 = new AtomicInteger(0);
IntStream.iterate(1, x -> 1).limit(100).parallel().forEach(y -> value1.incrementAndGet());
new IntConsumer() {
    @Override
    public void accept(int y) {
        value1.incrementAndGet();
    }
});