问题描述
接口对于方法 of()
有两个重载。其中一个是变量arity方法,而另一个是一个参数。
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.
单参数方法是一个性能优化而不是将一个参数传递给变量arity方法?如果是这样,它如何提高性能?可以询问 empty()
方法的相同问题,这似乎是围绕变量arity 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.
你在看什么实现版本?当我看到实现时,我没有看到这一点。
What implementation version are you looking at? When I look at the implementation I don't see this.
这篇关于为什么在Java Stream接口中重载()的varargs方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!