为什么 Java 可变参数不支持集合?
在我的 Java 代码中,我经常使用非常方便的 method(Class... args)
varargs。据我所知,它们允许您传递任意数量的 Class 对象或 Class[] 数组。由于我也经常使用 Java 集合类,因此我对两者之间缺乏兼容性感到沮丧。结果,我最终做了collection.toArray()
,但这存在一些类型安全问题。
现在的问题是:为什么 Java 不允许 IterableT...
类型可变参数?不是每个人都一直使用列表、集合等吗?是否有一种简单、类型安全的方法来提供从集合到可变参数的转换?
In my Java code I often use the very handy method(Class... args)
varargs. As far as I know, they allow you to pass any amount of Class
objects or an array of Class[]
. Since I also often use the Java collection classes, I am frustrated by the lack of compatibility between both. As a result, I end up doing collection.toArray()
, but that has some type safety issues.
So now for the question: why doesn't Java allow instances of Iterable<T>
as vararg arguments, as long as the generic type fits the T...
type of the vararg? Doesn't everyone use lists, sets, etc. all the time? Is there an easy, type-safe way to provide the conversion from collection to vararg?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因很简单:变量 arity 参数只是一个老式数组参数,带有一些附加元数据,告诉编译器提供一些语法糖(即,它允许隐式数组创建)。
因此,从 JVM 的角度来看,
Object...
与Object[]
几乎相同。允许集合也需要对 JVM 进行更具侵入性的更改(迄今为止,JVM 还没有明确支持集合)。请注意,如果您想支持这两种方式,那么使用基于集合的方法可能是更好的方法:
这样做的原因是使用
Arrays.asList()
通常 比 Collection.toArray() 更便宜的操作(因为它创建了一个简单的包装器)。The reason is simple: a variable arity parameter is simply an old-school array paramater with some additional metadata that tells the compiler to provide some syntactic sugar (namely, it allows implicit array creation).
So from the perspective of the JVM
Object...
is pretty much the same asObject[]
. Allowing collections as well would require a more invasive change to the JVM (which has no explicit support for collections to date).Note that if you want to support both ways, then making the collection-based method is probably the better approach:
The reason for this is that using
Arrays.asList()
is usually a cheaper operation thanCollection.toArray()
(because it creates a simple wrapper).