List of Primitive Integer Values in Java – Java中的原始整数值列表

最后修改: 2019年 2月 3日

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

1. Overview

1.概述

In this tutorial, we’ll learn how to construct a list containing primitive integer values.

在本教程中,我们将学习如何构造一个包含原始整数值的列表

We’ll explore solutions using core Java and external libraries.

我们将探索使用核心Java和外部库的解决方案。

2. Autoboxing

2.Autoboxing

In Java, generic type arguments must be reference types. This means we can’t do something like List<int>.

在Java中,generic type参数必须是引用类型。这意味着我们不能做类似List<int>的事情。

Instead, we can use List<Integer> and take advantage of autoboxing. Autoboxing helps us use the List<Integer> interface as if it contained primitive int values. Under the hood, it is still a collection of Objects and not primitives.

相反,我们可以使用List<Integer>并利用自动框选的优势。自动框选帮助我们使用List<Integer>接口,就像它包含原始的int值一样。在引擎盖下,它仍然是一个Objects的集合,而不是基元。

The core Java solution is just an adjustment to be able to use primitives with generic collections. Moreover, it comes with the cost of boxing and unboxing conversions.

核心的Java解决方案只是进行了调整,以便能够使用带有通用collections的基元。此外,它还需要付出装箱和拆箱转换的代价。

However, there are other options in Java and additional third-party libraries that we can use. Let’s see how to use them below.

然而,在Java中还有其他的选择和额外的第三方库,我们可以使用。下面让我们看看如何使用它们。

3. Using the Stream API

3.使用流API

Oftentimes, we don’t actually need to create a list as much as we just need to operate on it.

很多时候,我们实际上并不需要创建一个列表,而只是需要对其进行操作。

In these cases, it might work to use Java 8’s Stream API instead of creating a list altogether. The IntSream class provides a sequence of primitive int elements that supports sequential aggregate operations.

在这些情况下,使用 Java 8 的 Stream API 而不是完全创建一个列表可能是可行的。IntSream类提供了一个原始int元素的序列,支持连续的聚合操作。

Let’s have a quick look at an example:

让我们快速看一下一个例子。

IntStream stream = IntStream.of(5, 10, 0, 2, -8);

The IntStream.of() static method returns a sequential IntStream.

IntStream.of() 静态方法返回一个顺序的IntStream

Similarly, we can create an IntStream from an existing array of ints:

同样地,我们可以从现有的IntStream数组中创建一个int

int[] primitives = {5, 10, 0, 2, -8};
IntStream stream = IntStream.of(primitives);

Moreover, we can apply the standard Stream API operations to iterate, filter and aggregate the ints. For example, we can calculate the average of the positive int values:

此外,我们可以应用标准的Stream API操作来迭代、过滤和聚合int。例如,我们可以计算正int值的平均值。

OptionalDouble average = stream.filter(i -> i > 0).average();

Most importantly, no autoboxing is used while working with the streams.

最重要的是,在使用流时,没有使用自动排版

Though, if we definitely need a concrete list, we’ll want to take a look at one of the following third-party libraries.

不过,如果我们肯定需要一个具体的列表,我们会想看看以下第三方库之一。

4. Using Trove 

4.使用Trove

Trove is a high-performance library which provides primitive collections for Java.

Trove是一个高性能的库,为Java提供原始集合

To setup Trove with Maven, we need to include the trov4j dependency in our pom.xml:

要用Maven设置Trove,我们需要在pom.xml中包含trov4j依赖。

<dependency>
    <groupId>net.sf.trove4j</groupId>
    <artifactId>trove4j</artifactId>
    <version>3.0.2</version>
</dependency>

With Trove, we can create lists, maps, and sets.

通过Trove,我们可以创建列表、地图和集合。

For instance, there is an interface TIntList with its TIntArrayList implementation to work with a list of int values:

例如,有一个接口TIntList和它的TIntArrayList实现来处理int值的列表。

TIntList tList = new TIntArrayList();

Even though TIntList can’t directly implement List, it’s methods are very comparable. Other solutions that we discuss follow a similar pattern.

尽管TIntList不能直接实现List,但它的方法是非常类似的。我们讨论的其他解决方案也遵循类似的模式。

The greatest benefit of using TIntArrayList is performance and memory consumption gains. No additional boxing/unboxing is needed as it stores the data inside of an int[] array.

使用TIntArrayList的最大好处是性能和内存消耗的提升。由于它将数据存储在一个int[]数组中,所以不需要额外的装箱/解箱。

5. Using Fastutil

5.使用Fastutil

Another high-performance library to work with the primitives is Fastutil. Let’s add the fastutil dependency:

另一个用于处理基元的高性能库是Fastutil。让我们添加fastutil依赖项

<dependency>
    <groupId>it.unimi.dsi</groupId>
    <artifactId>fastutil</artifactId>
    <version>8.1.0</version>
</dependency>

Now, we’re ready to use it:

现在,我们准备使用它了。

IntArrayList list = new IntArrayList();

The default constructor IntArrayList() internally creates an array of primitives with the default capacity of 16.  In the same vein, we can initialize it from an existing array:

默认构造函数IntArrayList()在内部创建了一个默认容量为16的基元数组。 同样地,我们可以从一个现有的数组中初始化它。

int[] primitives = new int[] {5, 10, 0, 2, -8};
IntArrayList list = new IntArrayList(primitives);

6. Using Colt

6.使用柯尔特

Colt is an open source, a high-performance library for scientific and technical computing. The cern.colt package contains resizable lists holding primitive data types such as int.

Colt是一个开源的、用于科学和技术计算的高性能库cern.colt包包含可调整大小的列表,持有原始数据类型,如int.

First, let’s add the colt dependency:

首先,让我们添加colt依赖性

<dependency>
    <groupId>colt</groupId>
    <artifactId>colt</artifactId>
    <version>1.2.0</version>
</dependency>

The primitive list that offers this library is cern.colt.list.IntArrayList:

提供这个库的原始列表是cern.colt.list.IntArrayList:

cern.colt.list.IntArrayList coltList = new cern.colt.list.IntArrayList();

The default initial capacity is ten.

默认的初始容量为10。

7. Using Guava

7.使用番石榴

Guava provides a number of ways of interfacing between primitive arrays and collection APIs. The com.google.common.primitives package has all the classes to accommodate primitive types.

Guava提供了许多在原始数组和集合API之间进行接口的方法com.google.common.primitives包有所有的类来适应原始类型。

For example, the ImmutableIntArray class lets us create an immutable list of int elements.

例如,ImmutableIntArray类让我们创建一个int元素的不可变列表。

Let’s suppose, we have the following array of int values:

我们假设,我们有以下的int值数组。

int[] primitives = new int[] {5, 10, 0, 2};

We can simply create a list with the array:

我们可以简单地用数组创建一个列表。

ImmutableIntArray list = ImmutableIntArray.builder().addAll(primitives).build();

Furthermore, it provides a list API with all the standard methods we would expect.

此外,它还提供了一个具有我们所期望的所有标准方法的列表API。

8. Conclusion

8.结论

In this quick article, we showed multiple ways of creating lists with the primitive integers. In our examples, we used the Trove, Fastutil, Colt, and Guava libraries.

在这篇快速文章中,我们展示了用原始整数创建列表的多种方法在我们的例子中,我们使用了Trove、Fastutil、Colt和Guava库

As usual, the complete code for this article is available over on GitHub.

像往常一样,本文的完整代码可以在GitHub上找到