Java 9,Set.of()和Map.of()varargs重载 [英] Java 9, Set.of() and Map.of() varargs overloads
问题描述
我正在研究不可变
集合的工厂方法。
我看到 Set.of()
方法有10个varargs重载(同样适用于 Map.of()
)。我真的不明白为什么会这么多。最后,无论如何都会调用函数 ImmutableCollections.SetN<>(元素)
。
I'm studying the factory methods for Immutable
collections.
I see the Set.of()
method has 10 varargs overloading (same for Map.of()
). I really can't understand why there are so many. In the end the function ImmutableCollections.SetN<>(elements)
gets called anyway.
在文档中我发现这个:
虽然这会在API中引入一些混乱,但它避免了varargs引起的数组分配,初始化和垃圾收集开销调用。
While this introduces some clutter in the API, it avoids array allocation, initialization, and garbage collection overhead that is incurred by varargs calls.
杂乱的确是否值得获得性能提升?如果是,理想情况下是否会为任何 N
元素创建单独的方法?
Is the clutter indeed worth the performance gain? If yes, would that ideally do create a separate method for any N
elements?
推荐答案
目前无论如何调用该方法 - 这可能会改变。例如,它可能会创建一个 Set
,只有三个元素,4等等。
At the moment that method is called anyway - this could change. For example it could be that it creates a Set
with only three elements, 4 and so on.
此外并非所有委托给 SetN
- 那些零,一个和两个元素的实际类别为 ImmutableCollections.Set0
, ImmutableCollections.Set1
和 ImmutableCollections。 Set2
Also not all of them delegate to SetN
- the ones that have zero, one and two elements have actual classes of ImmutableCollections.Set0
, ImmutableCollections.Set1
and ImmutableCollections.Set2
或您可以阅读有关此问题的实际问题... where 阅读 Stuart Marks
在那个问题中 - 他是创建这些收藏的人。
Or you can read the actual question regarding this ... here Read the comments from Stuart Marks
in that question -as he is the person that created these Collections.
这篇关于Java 9,Set.of()和Map.of()varargs重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!