Why overload the varargs method of() in Java Stream interface?(为什么要在 Java Stream 接口中重载 varargs 方法 of()?)
问题描述
流 接口对方法 of()
有两个重载.其中一个是可变参数方法,而另一个采用单个参数.
The Stream interface has two overloads for the method of()
. One of these is a variable-arity method while the other takes a single argument.
与将一个参数传递给可变参数方法相比,单参数方法是一种性能优化吗?如果是这样,它如何提高性能?可以对 empty()
方法提出相同的问题,这似乎是可变参数 of()
周围的语法糖.
Is the single-argument method a performance optimization versus passing one argument to the variable-arity method? If so, how does it improve performance? The same questions could be asked of the empty()
method, which would seem to be syntax sugar around the variable-arity of()
.
我发现这些方法的实现方式不同,区别显然在于 Spliterator
的实例化方式;但这对 Stream
API 有什么好处?
I see that the implementation differs between these methods, with the difference apparently being how the Spliterator
is instantiated; but what advantage does this offer to the Stream
API?
推荐答案
是的,这是一种优化,可以避免创建一个只包含单个元素的数组的开销,如果你使用 varargs 版本,你会得到这样的结果.
Yes, it's an optimization to avoid the overhead of creating an array to hold only a single element, which is what you'd get if you used the varargs version.
empty() 方法也可以问同样的问题,这似乎是围绕可变参数 of() 的语法糖
The same questions could be asked of the empty() method, which would seem to be syntax sugar around the variable-arity of()
您在看什么实施版本?当我查看实现时,我看不到这一点.
What implementation version are you looking at? When I look at the implementation I don't see this.
这篇关于为什么要在 Java Stream 接口中重载 varargs 方法 of()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么要在 Java Stream 接口中重载 varargs 方法 of()?
基础教程推荐
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01