作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我发现了一个关于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
最佳答案
这是流优化,称为短路。本质上,发生的事情是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/
我是一名优秀的程序员,十分优秀!