[英]Java stream unexpected result
I have following code: 我有以下代码:
Stream.of("Java", "Stream", "Test")
.peek(s -> System.out.print(s + " "))
.allMatch(s -> s.startsWith("J"));
Why does it print me Java Stream? 为什么它会打印出Java Stream?
allMatch
is short-circuiting operation as most of the streams operations are. allMatch
是大多数流操作的短路操作。 since allMatch
returned early that's why peek
is only printing the first two elements. 因为
allMatch
早期返回,这就是为什么peek
只打印前两个元素。
Because allMatch()
checks if everyone element in the stream is true. 因为
allMatch()
检查流中的每个元素是否为true。 And since the second was false, it doesn't have to check further. 由于第二个是假的,因此无需进一步检查。
So peek()
won't print the 3rd element. 所以
peek()
不会打印第3个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.