将集合转换为数组的最简单方法?
假设我们有一个Collection
。将其转换为 Foo[]
的最佳方式(在当前上下文中 LoC 中最短)是什么?任何知名库都是允许的。
UPD:(本节还有一个案例;如果您认为值得为其创建另一个线程,请留下评论):将 Collection
转换为 Bar[]
怎么样?其中 Bar
的构造函数有 1 个 Foo
类型的参数,即 public Bar(Foo foo){ ... }
?
Suppose we have a Collection<Foo>
. What is the best (shortest in LoC in current context) way to transform it to Foo[]
? Any well-known libraries are allowed.
UPD: (one more case in this section; leave comments if you think it's worth to create another thread for it): What about transforming Collection<Foo>
to Bar[]
where Bar
has constructor with 1 parameter of type Foo
i.e. public Bar(Foo foo){ ... }
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
其中
x
是集合:Where
x
is the collection:使用 Java 8 更新问题的替代解决方案:
Alternative solution to the updated question using Java 8:
如果您多次使用它或在循环中使用它,您可以定义一个常量
并进行转换,就像
toArray 方法将采用空数组来确定目标数组的正确类型并创建一个为您准备的新数组。
这是我的更新建议:
If you use it more than once or in a loop, you could define a constant
and do the conversion it like
The
toArray
method will take the empty array to determine the correct type of the target array and create a new array for you.Here's my proposal for the update:
在 JDK/11 中,将
Collection
转换为Foo[]
的另一种方法是使用Collection.toArray(IntFunction生成器)
如:正如 @Stuart 所解释的邮件列表(强调我的),其性能本质上应该与现有的
Collection.toArray(new T[0])
相同 --JDK 中 API 的实现如下:
注意:- 只是当用于带有
null
的代码时,API 使用应遵循向后不兼容性值,例如toArray(null)
,因为这些调用现在由于现有的toArray(T[] a)
而变得不明确,并且无法编译。With JDK/11, an alternate way of converting a
Collection<Foo>
to anFoo[]
could be to make use ofCollection.toArray(IntFunction<T[]> generator)
as:As explained by @Stuart on the mailing list(emphasis mine), the performance of this should essentially be the same as that of the existing
Collection.toArray(new T[0])
--The implementation of the API within the JDK reads:
Note:- Just that the API use shall be guided along with a backward incompatibility when used for code with
null
values e.g.toArray(null)
since these calls would now be ambiguous because of existingtoArray(T[] a)
and would fail to compile.如果您在项目中使用 Guava,则可以使用
Iterables::toArray
。If you use Guava in your project you can use
Iterables::toArray
.这是更新部分中案例的最终解决方案(在 Google Collections 的帮助下):
但是,这里的关键方法在 doublep 的答案(我忘记了
toArray
方法)。Here's the final solution for the case in update section (with the help of Google Collections):
But, the key approach here was mentioned in the doublep's answer (I forgot for
toArray
method).原文请参阅doublep答案:
作为对于更新:
For the original see doublep answer:
As for the update:
实际上在现代Java中,没有设置显式大小的版本更快。请参阅此答案:
.toArray(new MyClass[0]) 或 .toArray(new MyClass [myList.size()])?
这是由独立研究和 IntelliJ 团队支持的。
也就是说,这是当今最快的方法:
或者更好的是,具有一点安全性:
Actually in modern Java, the version without setting the explicit size is faster. See this SO answer:
.toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])?
This is backed up by independent research and the team at IntelliJ.
That is, this is the fastest approach today:
Or, even better, with a bit of safety:
Foo[] foos = x.toArray(new Foo[0]);
Foo[] foos = x.toArray(new Foo[0]);
例如,您有包含 Student 类元素的集合 ArrayList:
For example, you have collection ArrayList with elements Student class: