重复消费Java Stream的三种方法。你选择哪种?
Java中的Stream一旦被消费就会关闭,不能再次使用了。如果的确有需要该怎么办呢?
这里介绍三种重复消费Stream的方法。
1. 从集合再次创建
这里你都不用往下继续看就知道该怎么办,不过我还是放上示例代码:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> stream1 = numbers.stream();
Stream<Integer> stream2 = numbers.stream();
只需要分别消费每个stream就可以。
2. 通过Supplier多次创建
这种方法是大多数网文推荐的办法(他们只提供了这一种方法),比如
- https://www.jb51.net/article/183920.htm
- https://blog.csdn.net/SUDDEV/article/details/79380818
我也贴一下示例代码:
Supplier<Stream<Integer>> supplier = () -> Stream.of(1, 2, 3, 4, 5);
Stream<Integer> stream1 = supplier.get();
Stream<Integer> stream2 = supplier.get();
同样地,你能拿到多个相同的stream。
3. 通过Stream Builder多次创建
这种方法用的最少,因为我们很少创建Builder:
IntStream.Builder builder = IntStream.builder();
builder.add(1);
builder.add(2);
builder.add(3);
builder.add(4);
builder.add(5);
IntStream stream1 = builder.build();
IntStream stream2 = builder.build();
建议
那么应该用哪种方法呢?第二种吗?NO!多数情况下应该是第一种。
Both approaches have their own advantages and disadvantages, and the choice between them depends on the specific use case.
Approach 1 (using a collection to create a new stream) is generally simpler and more efficient, especially if the original stream was created from a collection. Since the new stream is created from the same collection, there is no additional memory overhead or processing time required to create it.
Approach 2 (using a supplier to create a new stream) is more flexible, as it allows you to create a new stream from any source, not just a collection. However, it may be less efficient than approach 1, as creating a new stream from a non-collection source may require additional memory or processing time.
In general, if the original stream was created from a collection, approach 1 is recommended. If the original stream was created from a non-collection source, or if you need more flexibility in creating new streams, approach 2 may be more appropriate.
重要更新
以上方案来自GPT3.5,但是经过验证,第三种方案是错误的,Builder并不能重复build()。

GPT4:
Note that the Stream.Builder itself cannot be reused once the build method has been called, just like any other stream.
Stream为何不设计成能重复消费
GPT4:
Java中的Stream不能被重复使用,主要是因为它们被设计为一次性的、即时执行的操作序列。以下是一些关于Stream不可重用性的详细解释:
设计哲学:Stream被设计为"流"的概念,即数据从源流向目的地。流被消费后就完成了它的使命。这与传统的集合不同,集合可以被多次访问和修改。
内部迭代:Stream API使用内部迭代,意味着迭代是由Stream自身控制的,而不是由外部代码控制的。一旦迭代完成,Stream的内部状态就表示该流已经被消费。
性能优化:不允许重用Stream有助于性能优化。因为Stream只能被消费一次,所以可以在内部进行一些优化,例如延迟执行和中间操作融合。
避免错误:限制Stream只能被消费一次有助于减少编程错误。如果允许重用Stream,可能会引入复杂的状态管理和同步问题。
惰性执行与短路操作:许多Stream操作是惰性的,并且可以进行短路操作。一旦终端操作被触发,中间操作就会被执行,并且可能会被短路。短路操作允许Stream尽早终止处理。如果Stream被重用,这些惰性和短路特性可能会变得复杂且难以管理。
重复消费Java Stream的三种方法。你选择哪种?的更多相关文章
- spring mvc 防止重复提交表单的两种方法,推荐第二种
第一种方法:判断session中保存的token 比较麻烦,每次在提交表单时都必须传入上次的token.而且当一个页面使用ajax时,多个表单提交就会有问题. 注解Token代码: package c ...
- 像画笔一样慢慢画出Path的三种方法(补充第四种)
今天大家在群里大家非常热闹的讨论像画笔一样慢慢画出Path的这种效果该如何实现. 北京-LGL 博客号@ligl007发起了这个话题.然后各路高手踊跃发表意见.最后雷叔 上海-雷蒙 博客号@雷蒙之星 ...
- js如何判断一个对象是不是Array? 三种方法总有一种可以帮上忙
转载:http://www.nowamagic.net/librarys/veda/detail/1250 在开发中,我们经常需要判断某个对象是否为数组类型,在Js中检测对象类型的常见方法都有哪些呢? ...
- MD5 几种方法的选择
转:http://zoroeye.iteye.com/blog/2026984?utm_source=tuicool&utm_medium=referral md5加密实现方法有很多种,也导致 ...
- JavaScript调用后台的三种方法实例(包含两种Ajax)
方法一:直接使用<%=%>调用(ASPX页面) 前台JS,代码如下: <script type="text/javascript"> var methodS ...
- Java反射【三、方法的反射】
获取一个类下的所有方法 可以获取类类型后,获取到所有方法及相关信息 Method[] ms = c.getMethods(); 获取方法列表(public) Method[] ms = c.getDe ...
- Ionic 设置全局变量,三种方法设置图片一种是直接增加,一种是replace,第三种是管道和第二种类似
- SQL Server 查询表的记录数(3种方法,推荐第一种)
http://blog.csdn.net/smahorse/article/details/8156483 --SQL Server 查询表的记录数 --one: 使用系统表. SELECT obje ...
- 【转】SQL Server 查询表的记录数(3种方法,推荐第一种)
--SQL Server 查询表的记录数 --one: 使用系统表. SELECT object_name (i.id) TableName, rows as RowCnt FROM sysindex ...
- Eclipse远程调试Java代码的三种方法
Eclipse远程调试Java代码的三种方法, 第1种方法是用来调试已经启动的Java程序,Eclipse可以随时连接到远程Java程序进行调试, 第2种方法可以调试Java程序启动过程,但是Ecli ...
随机推荐
- 使用可视化工具redis-desktop-manager管理查询缓存。
AnotherRedisDesktopManager https://gitee.com/qishibo/AnotherRedisDesktopManager/releases 下载windows版本 ...
- nim 2. 变量与函数
对于系统的学习,官网上的相应的资料, 这里通过一个简单的例程,演示一下. 这个例程里有一个根据半径计算圆面积的方法,以及调用的示例: 演示代码 1 #计算圆的面积 2 proc calcArea(r: ...
- Vben-admin---ApiSelect Invalid prop: type check failed for prop "onUpdate:value". Expected Function, got Array
在basicFrom组件里添加一个ApiSelect, <template #localSearch="{ model, field }"> <ApiSelect ...
- mongodb单个服务部署
mongodb3.2.8安装步骤: 1.系统准备 (1)redhat或cnetos6.2以上系统 (2)系统开发包完整 (3)ip地址和hosts文件解析正常 (4)iptables防火墙&S ...
- WPF,Frame控件的一个BUG
我使用WPF默认的frame <Frame Style="{DynamicResource FrameStyle1}" x:Name="frame" He ...
- 自定义Naive UI的数据表格Data Table中按钮Button图标
在Naive UI官网中详细介绍了[数据表格 Data Table](数据表格 Data Table - Naive UI)的使用方式 { title: "Action", key ...
- mysql 命令行安装方式
一:下载 先到 mysql 官方网站下载:https://dev.mysql.com/downloads/mysql/ 点击直接下载: 解压到目录:D:\mysql-8.0.19-winx64 如图 ...
- Vue cli路由
上面是将Forecast组件作为了Home的子组件使用,现在我们将其作为一个路由组件使用. 在router/index.js路由系统注册路由: { path: '/forecast', name: ' ...
- 日常Bug排查-MVCC和for update混用导致读数据不一致
日常Bug排查-MVCC和for update混用导致读数据不一致 前言 日常Bug排查系列都是一些简单Bug的排查.笔者将在这里介绍一些排查Bug的简单技巧,同时顺便积累素材. Bug现场 又是喜闻 ...
- c语言不可不说的一件事——进制之间的转化
进制之间的转换 二进制转换 二进制转换为十进制 10111010.101 转换方法: 个位数是乘以2的0次方 从小数点开始分割,(小数点前从右往左,小数点后从左往右)乘以2的对应次方(从0开始取),最 ...