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()?
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01