Why is this java Stream operated upon twice?(为什么这个 java Stream 被操作了两次?)
问题描述
Java 8 API 说:
直到管道的终端操作执行完毕,管道源的遍历才开始.
Traversal of the pipeline source does not begin until the terminal operation of the pipeline is executed.
那么为什么会抛出以下代码:
So why the following code throws :
java.lang.IllegalStateException: 流已经被操作或关闭
java.lang.IllegalStateException: stream has already been operated upon or closed
Stream<Integer> stream = Stream.of(1,2,3);
stream.filter( x-> x>1 );
stream.filter( x-> x>2 ).forEach(System.out::print);
API 的第一个过滤操作不应该对 Stream 进行操作.
The first filtering operation according to the API is not supposed to operate on the Stream.
推荐答案
这是因为你忽略了 filter
的返回值.
This happens because you are ignoring the return value of filter
.
Stream<Integer> stream = Stream.of(1,2,3);
stream.filter( x-> x>1 ); // <-- ignoring the return value here
stream.filter( x-> x>2 ).forEach(System.out::print);
Stream.filter
返回一个 new Stream
,其中包含与给定谓词匹配的该流的元素.但重要的是要注意它是一个新的 Stream.旧的过滤器添加到它时已对其进行了操作.但新的不是.
Stream.filter
returns a new Stream
consisting of the elements of this stream that match the given predicate. But it's important to note that it's a new Stream. The old one has been operated upon when the filter was added to it. But the new one wasn't.
引用自 Stream代码> Javadoc:
Quoting from Stream
Javadoc:
一个流只能被操作(调用一个中间或终端流操作)一次.
A stream should be operated on (invoking an intermediate or terminal stream operation) only once.
在这种情况下,filter
是对旧 Stream 实例进行操作的中间操作.
In this case, filter
is the intermediate operation that operated on the old Stream instance.
所以这段代码可以正常工作:
So this code will work fine:
Stream<Integer> stream = Stream.of(1,2,3);
stream = stream.filter( x-> x>1 ); // <-- NOT ignoring the return value here
stream.filter( x-> x>2 ).forEach(System.out::print);
<小时>
正如 Brian Goetz 所说,您通常会将这些调用链接在一起:
As noted by Brian Goetz, you would commonly chain those calls together:
Stream.of(1,2,3).filter( x-> x>1 )
.filter( x-> x>2 )
.forEach(System.out::print);
这篇关于为什么这个 java Stream 被操作了两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么这个 java Stream 被操作了两次?
基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01