在Java 9中重载的方便工厂方法的重点是什么? [英] What is the point of overloaded Convenience Factory Methods for Collections in Java 9

查看:132
本文介绍了在Java 9中重载的方便工厂方法的重点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 9带有便利工厂方法,用于创建不可变列表。最后创建列表非常简单:

Java 9 comes with convenience factory methods for creating immutable lists. Finally a list creation is as simple as:

List<String> list = List.of("foo", "bar");

但此方法有12个重载版本,11个包含0到10个元素,另一个包含var args。

But there are 12 overloaded versions of this method, 11 with 0 to 10 elements, and one with var args.

static <E> List<E>  of(E... elements)

Set <的情况相同/ code>和 Map

由于存在var args方法,有什么意义有额外的11种方法?

Since there is a var args method, what is the point of having extra 11 methods?

我认为var-args创建一个数组,所以其他11种方法可以跳过创建一个额外的对象,在大多数情况下0 - 10个元素会做。还有其他原因吗?

What I think is that var-args create an array, so the other 11 methods can skip creation of an extra object and in most cases 0 - 10 elements will do. Is there any other reason for this?

推荐答案

来自 JEP docs 本身 -

描述 -


这些将包括varargs重载,因此集合大小没有固定限制
。但是,如此创建的集合实例
可能会针对较小的尺寸进行调整。将提供最多十个元素的特殊情况API(固定参数
重载)。 虽然这个
在API中引入了一些混乱,但它避免了
varargs调用引起的数组分配,
初始化和垃圾收集开销。
重要的是,源代码无论是否调用fixed-arg或varargs重载,调用站点的代码都是相同的。

These will include varargs overloads, so that there is no fixed limit on the collection size. However, the collection instances so created may be tuned for smaller sizes. Special-case APIs (fixed-argument overloads) for up to ten of elements will be provided. While this introduces some clutter in the API, it avoids array allocation, initialization, and garbage collection overhead that is incurred by varargs calls. Significantly, the source code of the call site is the same regardless of whether a fixed-arg or varargs overload is called.






编辑 - 为了增加动力,正如@CKing在评论中已经提到的那样:


Edit - To add motivation and as already mentioned in the comments by @CKing too :

非目标 -


支持具有任意数量元素的高性能,可扩展集合
并非目标。 重点关注小品牌

动机 -

创建一个小的,不可修改的集合(比如一个集合)包括构造它,将它存储在局部变量中,并在其上多次调用add(),然后包装它。 / p>

Creating a small, unmodifiable collection (say, a set) involves constructing it, storing it in a local variable, and invoking add() on it several times, and then wrapping it.

Set<String> set = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("a", "b", "c")));

通过组合流工厂方法和收集器,Java 8 Stream API可用于构建小型集合。

The Java 8 Stream API can be used to construct small collections, by combining stream factory methods and collectors.

// Java 8
Set<String> set1 = Collections.unmodifiableSet(Stream.of("a", "b", "c").collect(Collectors.toSet()));

通过提供用于创建小型集合实例的库API,可以获得集合文字的大部分好处,与更改语言相比,显着降低了成本和风险。例如,创建小型Set实例的代码可能如下所示:

Much of the benefit of collection literals can be gained by providing library APIs for creating small collection instances, at significantly reduced cost and risk compared to changing the language. For example, the code to create a small Set instance might look like this:

// Java 9 
Set set2 = Set.of("a", "b", "c");

这篇关于在Java 9中重载的方便工厂方法的重点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆