Converting between an Array and a List in Java – 在Java中的数组和列表之间的转换

最后修改: 2014年 5月 30日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’re going to learn how to convert between an Array and a List using core Java libraries, Guava and Apache Commons Collections.

在这个快速教程中,我们将学习如何使用Java核心库、Guava和Apache Commons Collections在数组和列表之间进行转换

This article is part of the “Java – Back to Basic” series here on Baeldung.

这篇文章是Baeldung网站上“Java–回到基础 “系列的一部分。

2. Convert List to Array

2.将列表转换成数组

2.1. Using Plain Java

2.1.使用普通的Java

Let’s start with the conversion from List to Array using plain Java:

让我们从List到Array的转换开始使用普通Java

@Test
public void givenUsingCoreJava_whenListConvertedToArray_thenCorrect() {
    List<Integer> sourceList = Arrays.asList(0, 1, 2, 3, 4, 5);
    Integer[] targetArray = sourceList.toArray(new Integer[0]);
}

Note that the preferred way for us to use the method is toArray(new T[0]) versus toArray(new T[size]). As Aleksey Shipilëv proves in his blog post, it seems faster, safer, and cleaner.

注意,我们使用该方法的首选方式是toArray(new T[0])toArray(new T[size])。正如Aleksey Shipilëv在他的博客文章中所证明的那样,这似乎更快、更安全、更干净。

2.2. Using Guava

2.2.使用Guava

Now let’s use the Guava API for the same conversion:

现在让我们使用Guava API来进行同样的转换。

@Test
public void givenUsingGuava_whenListConvertedToArray_thenCorrect() {
    List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
    int[] targetArray = Ints.toArray(sourceList);
}

3. Convert Array to List

3.将数组转换为列表

3.1. Using Plain Java

3.1.使用普通的Java

Let’s start with the plain Java solution for converting the array to a List:

让我们从将数组转换为List的普通Java解决方案开始。

@Test
public void givenUsingCoreJava_whenArrayConvertedToList_thenCorrect() {
    Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
    List<Integer> targetList = Arrays.asList(sourceArray);
}

Note that this is a fixed-sized list that will still be backed by the array. If we want a standard ArrayList, we can simply instantiate one:

请注意,这是一个固定大小的列表,它仍然会被数组所支持。如果我们想要一个标准的ArrayList,我们可以简单地实例化一个。

List<Integer> targetList = new ArrayList<Integer>(Arrays.asList(sourceArray));

3.2. Using Guava

3.2.使用Guava

Now let’s use the Guava API for the same conversion:

现在让我们使用Guava API来进行同样的转换。

@Test
public void givenUsingGuava_whenArrayConvertedToList_thenCorrect() {
    Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
    List<Integer> targetList = Lists.newArrayList(sourceArray);
}

3.3. Using Commons Collections

3.3.使用共享资源库

Finally, let’s use the Apache Commons Collections CollectionUtils.addAll API to fill in the elements of the array in an empty List:

最后,让我们使用Apache Commons Collections CollectionUtils.addAllAPI来填充空List中的数组元素。

@Test 
public void givenUsingCommonsCollections_whenArrayConvertedToList_thenCorrect() { 
    Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; 
    List<Integer> targetList = new ArrayList<>(6); 
    CollectionUtils.addAll(targetList, sourceArray); 
}

4. Conclusion

4.结论

The implementation of all of these examples and code snippets can be found over on GitHub. This is a Maven-based project, so it should be easy to import and run as it is.

所有这些例子和代码片段的实现都可以在GitHub上找到。这是一个基于Maven的项目,所以应该很容易导入并按原样运行。