Convert an Array of Primitives to a List – 将一个基元数组转换为一个列表

最后修改: 2020年 7月 13日

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

1. Overview

1.概述

In this short tutorial, we’ll show how to convert an array of primitives to a List of objects of the corresponding type. Typically, we might try to use autoboxing in Java. However, as we’re going to see in the next section, our intuition of how autoboxing works can often be erroneous.

在这个简短的教程中,我们将展示如何将一个基元数组转换为相应类型的对象的List。通常情况下,我们可能会尝试在Java中使用autoboxing>。然而,正如我们将在下一节中看到的,我们对自动装箱的工作原理的直觉往往是错误的。

2. Problem

2 问题

Let’s begin with the definition of the problem. We have an array of primitives (int[]), and we desire to convert that array to a List (List<Integer>). An intuitive first attempt could be:

让我们从问题的定义开始。我们有一个基元数组int[]),我们希望将该数组转换为ListList<Integer> )。一个直观的第一次尝试可以是。

int[] input = new int[]{1,2,3,4};
List<Integer> output = Arrays.asList(input);

Unfortunately, this won’t compile due to type incompatibility. We might expect autoboxing to work with arrays of primitives. However, this instinctive belief isn’t true.

不幸的是,由于类型不兼容,这不会被编译。我们可能期望自动排版能与基元数组一起工作。然而,这种本能的想法并不正确。

Autoboxing only happens for a single element (for example from int to Integer). There is no automatic conversion from an array of primitive types to an array of their boxed reference types (for example from int[] to Integer[]).

自动装箱只发生在单个元素上(例如,从intInteger)。从原始类型的数组到它们的盒式引用类型的数组没有自动转换(例如,从int[]Integer[])。

Let’s start to implement a few solutions to this problem.

让我们开始实施一些解决这个问题的办法。

3. Iteration

3.迭代

Since autoboxing works with single primitive elements, a simple solution is to just iterate over the elements of the array and add them to the List one by one:

由于自动排版对单个原始元素起作用,一个简单的解决方案是直接迭代数组中的元素,并将它们一个一个地添加到List

int[] input = new int[]{1,2,3,4};
List<Integer> output = new ArrayList<Integer>();
for (int value : input) {
    output.add(value);
}

We have solved the problem, but the solution is quite verbose. This brings us to the next implementation.

我们已经解决了这个问题,但这个解决方案相当啰嗦。这就把我们带到了下一个实现。

4. Streams

4.溪流

Since Java 8, we can use the Stream API. We can provide a one-line solution using a Stream:

从Java 8开始,我们可以使用Stream API。我们可以使用Stream提供一个单行解决方案。

int[] input = new int[]{1,2,3,4};
List<Integer> output = Arrays.stream(input).boxed().collect(Collectors.toList());

Alternatively, we could use IntStream:

另外,我们可以使用IntStream

int[] input = new int[]{1,2,3,4};
List<Integer> output = IntStream.of(input).boxed().collect(Collectors.toList());

This certainly looks a lot nicer. Next, we’ll look at a couple of external libraries.

这看起来当然要好得多。接下来,我们来看看几个外部库。

5. Guava

5.番石榴

The Guava library provides a wrapper around this problem. Let’s start by adding the Maven dependency:

Guava库为这个问题提供了一个封装器。让我们从添加Maven依赖性开始。

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
    <type>bundle</type>
</dependency>

We can use Ints.asList(), with similar utility classes for the other primitive types:

我们可以使用Ints.asList(),并为其他原始类型提供类似的实用类。

int[] input = new int[]{1,2,3,4};
List<Integer> output = Ints.asList(input);

6. Apache Commons

6.阿帕奇公社

Another library is the Apache Commons Lang. Again, let’s add the Maven dependency for this library:

另一个库是Apache Commons Lang>。同样,让我们为这个库添加Maven依赖项。

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

More precisely, we use the ArrayUtils class:

更确切地说,我们使用ArrayUtils类。

int[] input = new int[]{1,2,3,4};
Integer[] outputBoxed = ArrayUtils.toObject(input);
List<Integer> output = Arrays.asList(outputBoxed);

7. Conclusion

7.结语

We now have a few options in our toolbox to convert an array of primitives to a List. As we have seen, autoboxing only happens for single elements. In this tutorial, we’ve seen several solutions for applying the conversion.

现在我们的工具箱里有几个选项可以将基元数组转换为List正如我们所见,自动排版只发生在单个元素上。在本教程中,我们已经看到了几种应用转换的解决方案

As always, the full source code of the article is available over on GitHub.

一如既往,文章的完整源代码可在GitHub上获得