龙空技术网

Stream.toList()和Collectors.toList()的性能比较

海椰人 542

前言:

如今你们对“java collectorstolist”大概比较注意,同学们都需要剖析一些“java collectorstolist”的相关文章。那么小编同时在网上收集了一些关于“java collectorstolist””的相关文章,希望我们能喜欢,我们一起来学习一下吧!

天给大家介绍了Java 16中的Stream增强,可以直接通过toList()来转换成List。

主要涉及下面这几种转换方式:

list.stream().toList();list.stream().collect(Collectors.toList());list.stream().collect(Collectors.toUnmodifiableList());

然后,看到有网友评论问:Stream.toList()Collectors.toList()的区别是什么?哪个性能好?

处理结果的区别,其实上一篇文章和视频里都有说:

Stream.toList()返回的List是不可变List,不能增删改Collectors.toList()返回的是个普通的List,可以增删改Collectors.toUnmodifiableList()返回的List是不可变List,不能增删改

而至于性能的话,今天我们就来测试一下,看看哪个性能更好。

@BenchmarkMode(Mode.All)@Fork(1)@State(Scope.Thread)@Warmup(iterations = 20, time = 1, batchSize = 10000)@Measurement(iterations = 20, time = 1, batchSize = 10000)public class BenchmarkStreamToList {    @Benchmark    public List<Integer> streamToList() {        return IntStream.range(1, 1000).boxed().toList();    }    @Benchmark    public List<Integer> collectorsToList() {        return IntStream.range(1, 1000).boxed().collect(Collectors.toList());    }    @Benchmark    public List<Integer> streamToList() {        return IntStream.range(1, 1000).boxed().toList();    }}

结果报告:

Benchmark                                                                                  Mode  Cnt   Score    Error  UnitsBenchmarkStreamToList.collectorsToList                                                    thrpt   20  24.422 ±  0.268  ops/sBenchmarkStreamToList.collectorsToUnmodifiableList                                        thrpt   20  22.784 ±  0.599  ops/sBenchmarkStreamToList.streamToList                                                        thrpt   20  31.779 ±  1.732  ops/sBenchmarkStreamToList.collectorsToList                                                     avgt   20   0.045 ±  0.006   s/opBenchmarkStreamToList.collectorsToUnmodifiableList                                         avgt   20   0.062 ±  0.035   s/opBenchmarkStreamToList.streamToList                                                         avgt   20   0.040 ±  0.028   s/opBenchmarkStreamToList.collectorsToList                                                   sample  445   0.046 ±  0.002   s/opBenchmarkStreamToList.collectorsToList:collectorsToList·p0.00                            sample        0.039            s/opBenchmarkStreamToList.collectorsToList:collectorsToList·p0.50                            sample        0.041            s/opBenchmarkStreamToList.collectorsToList:collectorsToList·p0.90                            sample        0.057            s/opBenchmarkStreamToList.collectorsToList:collectorsToList·p0.95                            sample        0.073            s/opBenchmarkStreamToList.collectorsToList:collectorsToList·p0.99                            sample        0.102            s/opBenchmarkStreamToList.collectorsToList:collectorsToList·p0.999                           sample        0.150            s/opBenchmarkStreamToList.collectorsToList:collectorsToList·p0.9999                          sample        0.150            s/opBenchmarkStreamToList.collectorsToList:collectorsToList·p1.00                            sample        0.150            s/opBenchmarkStreamToList.collectorsToUnmodifiableList                                       sample  460   0.044 ±  0.001   s/opBenchmarkStreamToList.collectorsToUnmodifiableList:collectorsToUnmodifiableList·p0.00    sample        0.042            s/opBenchmarkStreamToList.collectorsToUnmodifiableList:collectorsToUnmodifiableList·p0.50    sample        0.044            s/opBenchmarkStreamToList.collectorsToUnmodifiableList:collectorsToUnmodifiableList·p0.90    sample        0.046            s/opBenchmarkStreamToList.collectorsToUnmodifiableList:collectorsToUnmodifiableList·p0.95    sample        0.047            s/opBenchmarkStreamToList.collectorsToUnmodifiableList:collectorsToUnmodifiableList·p0.99    sample        0.051            s/opBenchmarkStreamToList.collectorsToUnmodifiableList:collectorsToUnmodifiableList·p0.999   sample        0.057            s/opBenchmarkStreamToList.collectorsToUnmodifiableList:collectorsToUnmodifiableList·p0.9999  sample        0.057            s/opBenchmarkStreamToList.collectorsToUnmodifiableList:collectorsToUnmodifiableList·p1.00    sample        0.057            s/opBenchmarkStreamToList.streamToList                                                       sample  655   0.031 ±  0.001   s/opBenchmarkStreamToList.streamToList:streamToList·p0.00                                    sample        0.030            s/opBenchmarkStreamToList.streamToList:streamToList·p0.50                                    sample        0.031            s/opBenchmarkStreamToList.streamToList:streamToList·p0.90                                    sample        0.032            s/opBenchmarkStreamToList.streamToList:streamToList·p0.95                                    sample        0.033            s/opBenchmarkStreamToList.streamToList:streamToList·p0.99                                    sample        0.035            s/opBenchmarkStreamToList.streamToList:streamToList·p0.999                                   sample        0.037            s/opBenchmarkStreamToList.streamToList:streamToList·p0.9999                                  sample        0.037            s/opBenchmarkStreamToList.streamToList:streamToList·p1.00                                    sample        0.037            s/opBenchmarkStreamToList.collectorsToList                                                       ss   20   0.043 ±  0.001   s/opBenchmarkStreamToList.collectorsToUnmodifiableList                                           ss   20   0.045 ±  0.004   s/opBenchmarkStreamToList.streamToList                                                           ss   20   0.031 ±  0.001   s/op

从报告中我们可以看到:

吞吐量:streamToList > collectorsToList > collectorsToUnmodifiableList平均耗时:streamToList > collectorsToList > collectorsToUnmodifiableListp0.9999耗时:streamToList > collectorsToUnmodifiableList > collectorsToList冷启动耗时:streamToList > collectorsToList > collectorsToUnmodifiableList

所以,Stream.toList()的性能要各方面都要好于Collectors.toList()Collectors.toUnmodifiableList()

如果您学习过程中如遇困难?可以加入我们超高质量的技术交流群,参与交流与讨论,更好的学习与进步!

本文收录在了我正在连载的《Java新特性专栏》,该系列该用电子书的方式编写,如果想要沉浸式阅读学习的话,可以访问Web版本:

再放大一些数据量,试试:

@Benchmarkpublic List<Integer> streamToList() {  return IntStream.range(1, 10000).boxed().toList();}@Benchmarkpublic List<Integer> collectorsToList() {  return IntStream.range(1, 10000).boxed().collect(Collectors.toList());}@Benchmarkpublic List<Integer> streamToList() {  return IntStream.range(1, 10000).boxed().toList();}

结果报告:

Benchmark                                                                                  Mode  Cnt  Score   Error  UnitsBenchmarkStreamToList.collectorsToList                                                    thrpt   20  2.186 ± 0.162  ops/sBenchmarkStreamToList.collectorsToUnmodifiableList                                        thrpt   20  2.184 ± 0.042  ops/sBenchmarkStreamToList.streamToList                                                        thrpt   20  3.538 ± 0.058  ops/sBenchmarkStreamToList.collectorsToList                                                     avgt   20  0.426 ± 0.004   s/opBenchmarkStreamToList.collectorsToUnmodifiableList                                         avgt   20  0.469 ± 0.016   s/opBenchmarkStreamToList.streamToList                                                         avgt   20  0.293 ± 0.008   s/opBenchmarkStreamToList.collectorsToList                                                   sample   58  0.448 ± 0.049   s/opBenchmarkStreamToList.collectorsToList:collectorsToList·p0.00                            sample       0.414           s/opBenchmarkStreamToList.collectorsToList:collectorsToList·p0.50                            sample       0.422           s/opBenchmarkStreamToList.collectorsToList:collectorsToList·p0.90                            sample       0.458           s/opBenchmarkStreamToList.collectorsToList:collectorsToList·p0.95                            sample       0.560           s/opBenchmarkStreamToList.collectorsToList:collectorsToList·p0.99                            sample       1.160           s/opBenchmarkStreamToList.collectorsToList:collectorsToList·p0.999                           sample       1.160           s/opBenchmarkStreamToList.collectorsToList:collectorsToList·p0.9999                          sample       1.160           s/opBenchmarkStreamToList.collectorsToList:collectorsToList·p1.00                            sample       1.160           s/opBenchmarkStreamToList.collectorsToUnmodifiableList                                       sample   60  0.458 ± 0.004   s/opBenchmarkStreamToList.collectorsToUnmodifiableList:collectorsToUnmodifiableList·p0.00    sample       0.447           s/opBenchmarkStreamToList.collectorsToUnmodifiableList:collectorsToUnmodifiableList·p0.50    sample       0.455           s/opBenchmarkStreamToList.collectorsToUnmodifiableList:collectorsToUnmodifiableList·p0.90    sample       0.471           s/opBenchmarkStreamToList.collectorsToUnmodifiableList:collectorsToUnmodifiableList·p0.95    sample       0.482           s/opBenchmarkStreamToList.collectorsToUnmodifiableList:collectorsToUnmodifiableList·p0.99    sample       0.492           s/opBenchmarkStreamToList.collectorsToUnmodifiableList:collectorsToUnmodifiableList·p0.999   sample       0.492           s/opBenchmarkStreamToList.collectorsToUnmodifiableList:collectorsToUnmodifiableList·p0.9999  sample       0.492           s/opBenchmarkStreamToList.collectorsToUnmodifiableList:collectorsToUnmodifiableList·p1.00    sample       0.492           s/opBenchmarkStreamToList.streamToList                                                       sample   78  0.293 ± 0.012   s/opBenchmarkStreamToList.streamToList:streamToList·p0.00                                    sample       0.277           s/opBenchmarkStreamToList.streamToList:streamToList·p0.50                                    sample       0.284           s/opBenchmarkStreamToList.streamToList:streamToList·p0.90                                    sample       0.309           s/opBenchmarkStreamToList.streamToList:streamToList·p0.95                                    sample       0.377           s/opBenchmarkStreamToList.streamToList:streamToList·p0.99                                    sample       0.459           s/opBenchmarkStreamToList.streamToList:streamToList·p0.999                                   sample       0.459           s/opBenchmarkStreamToList.streamToList:streamToList·p0.9999                                  sample       0.459           s/opBenchmarkStreamToList.streamToList:streamToList·p1.00                                    sample       0.459           s/opBenchmarkStreamToList.collectorsToList                                                       ss   20  0.474 ± 0.133   s/opBenchmarkStreamToList.collectorsToUnmodifiableList                                           ss   20  0.493 ± 0.099   s/opBenchmarkStreamToList.streamToList                                                           ss   20  0.325 ± 0.056   s/op

从报告中我们可以看到

吞吐量:streamToList > collectorsToList > collectorsToUnmodifiableList平均耗时:streamToList > collectorsToList > collectorsToUnmodifiableListp0.9999耗时:streamToList > collectorsToUnmodifiableList > collectorsToList冷启动耗时:streamToList > collectorsToList > collectorsToUnmodifiableList

所以,即使集合内的元素增大,Stream.toList()的性能在各方面依然都要好于Collectors下的方法。

文章来自

标签: #java collectorstolist