gpt4 book ai didi

java - peek()和allMatch()如何在Java 8 Stream API中一起工作

转载 作者:行者123 更新时间:2023-12-01 09:13:00 26 4
gpt4 key购买 nike

我发现了一个关于peek方法的Java 8 Stream API的测验,如下所示

Arrays.asList("Fred", "Jim", "Sheila")
.stream()
.peek(System.out::println)
.allMatch(s -> s.startsWith("F"));

输出是
Fred
Jim

我对此流的工作方式感到困惑吗?我的预期结果应该是
Fred
Jim
Sheila

peek()方法是一个中间操作,它处理Stream中的每个元素。谁能向我解释一下。

最佳答案

这是流优化,称为短路。本质上,发生的事情是allMatch阻止了对流执行不必要的中间操作,因为在知道最终结果时执行这些操作没有意义。

好像发生了这种情况:

take"Fred"
peek("Fred")
evaluate("Fred".startsWith("F"))
decide whether the result of allMatch() is known for sure: Not yet

take"Jim"
peek("Jim")
evaluate("Jim".startsWith("F"))
decide whether the result of allMatch() is known for sure: Yes

评估 "Jim".startsWith("F")时,可以肯定知道 allMatch(s -> s.startsWith("F"))的结果。 "Jim"之后的管道中会出现什么值都没有关系,我们知道所有以“F”开头的值都是 false

这不是特定于 peek/ allMatch组合,存在多个中间和端子短路操作。 java.util.stream package's docs状态:

Further, some operations are deemed short-circuiting operations. An intermediate operation is short-circuiting if, when presented with infinite input, it may produce a finite stream as a result. A terminal operation is short-circuiting if, when presented with infinite input, it may terminate in finite time. Having a short-circuiting operation in the pipeline is a necessary, but not sufficient, condition for the processing of an infinite stream to terminate normally in finite time.



将其扩展到有限的流,并且短路操作避免了不必要的流水线步骤的执行,就像您的示例一样。

关于java - peek()和allMatch()如何在Java 8 Stream API中一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60498730/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com