Java 将集合转换为数组的最简单方法?

Java 将集合转换为数组的最简单方法?,java,arrays,collections,transformation,Java,Arrays,Collections,Transformation,假设我们有一个集合。将其转换为Foo[]的最佳方式(当前上下文中LoC最短)是什么?任何著名的图书馆都是允许的 UPD:(本节中还有一个例子;如果您认为值得为其创建另一个线程,请留下注释):如何将集合转换为Bar[],其中Bar具有一个类型为Foo的参数的构造函数,即public Bar(Foo-Foo){…}?其中x是集合: Foo[] foos = x.toArray(new Foo[x.size()]); 如果您多次使用它或在循环中使用它,您可以定义一个常量 public static

假设我们有一个
集合
。将其转换为
Foo[]
的最佳方式(当前上下文中LoC最短)是什么?任何著名的图书馆都是允许的


UPD:(本节中还有一个例子;如果您认为值得为其创建另一个线程,请留下注释):如何将
集合
转换为
Bar[]
,其中
Bar
具有一个类型为
Foo
的参数的构造函数,即
public Bar(Foo-Foo){…}

其中
x
是集合:

Foo[] foos = x.toArray(new Foo[x.size()]);

如果您多次使用它或在循环中使用它,您可以定义一个常量

public static final Foo[] FOO = new Foo[]{};
然后做它喜欢的转换

Foo[] foos = fooCollection.toArray(FOO);
toArray
方法将使用空数组来确定目标数组的正确类型,并为您创建一个新数组


以下是我的更新建议:

Collection<Foo> foos = new ArrayList<Foo>();
Collection<Bar> temp = new ArrayList<Bar>();
for (Foo foo:foos) 
    temp.add(new Bar(foo));
Bar[] bars = temp.toArray(new Bar[]{});
Collection foos=new ArrayList();
集合温度=新的ArrayList();
for(Foo-Foo:foos)
临时添加(新条(foo));
棒材[]棒材=阵列温度(新棒材[]{});
有关原始答案,请参见答案:

至于最新情况:

int i = 0;
Bar[] bars = new Bar[fooCollection.size()];
for( Foo foo : fooCollection ) { // where fooCollection is Collection<Foo>
    bars[i++] = new Bar(foo);
}    
inti=0;
Bar[]bars=新条[fooCollection.size()];
for(Foo-Foo:fooCollection){//其中fooCollection是Collection
条[i++]=新条(foo);
}    

以下是更新部分案例的最终解决方案(借助Google Collections):

Collections2.transform(fooCollection,新函数(){
公共酒吧申请(富富){
返回新条(foo);
}
}).toArray(新条[fooCollection.size()]);

但是,在中提到了这里的关键方法(我忘记了使用
toArray
方法)。

使用Java 8更新问题的替代解决方案:

Bar[] result = foos.stream()
    .map(x -> new Bar(x))
    .toArray(size -> new Bar[size]);

使用JDK/11,将
集合
转换为
Foo[]
的另一种方法是使用作为:

Foo[] foos = fooCollection.toArray(new Foo[0]); // before JDK 11
Foo[] updatedFoos = fooCollection.toArray(Foo[]::new); // after JDK 11

正如(emphasis mine)所解释的,它的性能应该与现有的
集合.toArray(新的T[0])
--

结果是使用
数组的实现。copyOf(
)是 最快,可能是因为它是内在的

它可以避免零填充新分配的数组,因为它知道 整个数组内容将被覆盖。不管发生什么,这都是事实 公共API看起来像

JDK中API的实现如下:

default <T> T[] toArray(IntFunction<T[]> generator) {
    return toArray(generator.apply(0));
}
默认T[]到阵列(IntFunction generator){
返回阵列(生成器。应用(0));
}
默认实现调用
generator.apply(0)
以获取零长度数组 然后简单地调用
toArray(T[])
。这将通过
Arrays.copyOf()
快速路径,因此基本上与toArray(新T[0])的速度相同



注意:-只是当用于带有
null
值的代码时,API的使用应遵循向后不兼容的原则,例如
toArray(null)
,因为这些调用现在会因为现有的
toArray(T[]a)
而变得模棱两可,并且无法编译。

例如,您拥有集合ArrayList和elements学生类:

List stuList = new ArrayList();
Student s1 = new Student("Raju");
Student s2 = new Student("Harish");
stuList.add(s1);
stuList.add(s2);
//now you can convert this collection stuList to Array like this
Object[] stuArr = stuList.toArray();           // <----- toArray() function will convert collection to array
List stuList=newarraylist();
学生s1=新学生(“Raju”);
学生s2=新学生(“哈里什”);
添加(s1);
添加(s2);
//现在可以将此集合列表转换为如下数组

对象[]stuArr=stuList.toArray();// 如果你在项目中使用番石榴,你可以使用


更简单(更容易)但不是最好的(内存):
x.toArray(新Foo[0])
——文档:没有时间阅读…@Carlos实际上,它使用的内存比
(新Foo[0])
更好。根据Collection.toArray文档,`@param a是存储此集合元素的数组,如果它足够大,`这意味着它将直接将它们存储在新数组中。如果给它一个大小为0的数组,它将生成一个新的数组,这意味着在不需要的情况下会生成一个小数组和一个大数组。@glowcoder-但如果将空数组定义为静态常量,则可以将其最小化。这只是提示
toArray
创建正确类型的目标数组。在这种情况下,老实说,我不会关心我的应用程序中额外的一个空数组。这远远低于噪音水平。@glowcoder-这正是我(试图)写下使用
newfoo[0]
更简单“但不是最好的(内存)”。。。也就是说,我的意思是我的解决方案更简单,但不是最好的(这就是我使用
)的原因。请注意,此代码不是线程安全的,即使您使用的是同步集合。它在大多数情况下都会自动运行,但如果在调用x.size()和x.toArray()之间删除了一个项,则生成的数组将在末尾包含一个额外的null元素。卡洛斯提出的
新Foo[0]
变体没有遇到这个问题。ups,
const
不是Java<代码>公共静态final Foo[]Foo={}
为什么它是公共的?@zoltan-为什么不是?它是不可变的,因此不存在安全问题。它有点杂乱,但在其他代码中可能有用,因此通常是无害的。甚至可以创建一个包含所有这些常量的中心类,然后使用
import static
获取它们。@Jules对数组的引用是不可变的,但数组的内容不是。类外的任何人都可以自由替换数组的元素。另一方面,根据经验,我认为所有字段在类外需要之前都应该是私有的。由于长度为零,因此没有可以更改的内容。请参阅我对doublep关于线程安全的回答的评论,这也适用于此解决方案
newbar[0]
避免了该问题。可以将其缩短为
Bar[]result=foos.stream().map(Bar::new).toArray(Bar[]::new)语法智能I
default <T> T[] toArray(IntFunction<T[]> generator) {
    return toArray(generator.apply(0));
}
List stuList = new ArrayList();
Student s1 = new Student("Raju");
Student s2 = new Student("Harish");
stuList.add(s1);
stuList.add(s2);
//now you can convert this collection stuList to Array like this
Object[] stuArr = stuList.toArray();           // <----- toArray() function will convert collection to array
Foo[] foos = Iterables.toArray(x, Foo.class);