Java IntStream Conversions – Java IntStream的转换

最后修改: 2019年 6月 26日

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

1. Introduction

1.绪论

In this quick tutorial, we’ll go through all the possibilities regarding IntStream conversions to other types.

在这个快速教程中,我们将通过关于IntStream转换为其他类型的所有可能性

Interesting readings about boxing and unboxing or iterating are recommended as a complement of this tutorial.

建议阅读有关boxing和unboxingiterating的有趣读物,作为本教程的一个补充。

2. IntStream to Array

2.IntStreamArray

Let’s start exploring how we can convert from an IntStream object to an array of ints.

让我们开始探索如何将IntStream对象转换成ints数组。

For the sake of this example, let’s generate the first 50 even numbers and store them in an array as a result:

在这个例子中,让我们生成前50个偶数,并将它们作为结果存储在一个数组中。

@Test
public void intStreamToArray() {
  int[] first50EvenNumbers = IntStream.iterate(0, i -> i + 2)
    .limit(50)
    .toArray();

  assertThat(first50EvenNumbers).hasSize(50);
  assertThat(first50EvenNumbers[2]).isEqualTo(4);
}

First, let’s create an infinite stream of integers starting at 0 and iterating by adding 2 to each element. Immediately after that, we need to add an intermediate operation limit in order to make this operation, somehow, terminating.

首先,让我们创建一个无限的整数流,从0开始,通过给每个元素加2进行迭代。紧接着,我们需要添加一个中间操作limit,以便使这个操作,以某种方式,终止。

Finally, let’s use the terminating operation collect to gather this Stream to an array.

最后,让我们使用终止操作collect来收集这个Stream到一个数组。

This is a straight-forward way of generating an array of ints.

这是一种直接生成ints数组的方法.

3. IntStream to List

3.IntStreamList

Let’s convert now an IntStream to a List of Integers.

现在让我们把一个IntStream转换成ListIntegers

In this case, just to add more variety to the example, let’s use the method range instead of the method iterate. This method will generate an IntStream from the int 0 to the int 50 (not included since it’s an open range):

在这种情况下,只是为了增加例子的多样性,让我们使用方法range而不是方法iterate。这个方法将生成一个IntStream,从int0到int50(不包括在内,因为它是一个开放范围)。

@Test
public void intStreamToList() {
  List<Integer> first50IntegerNumbers = IntStream.range(0, 50)
    .boxed()
    .collect(Collectors.toList());

  assertThat(first50IntegerNumbers).hasSize(50);
  assertThat(first50IntegerNumbers.get(2)).isEqualTo(2);
}

In this example, we make use of the method range.  The most notorious part here is using the method boxed, that, as its name points out, will box all the int elements in the IntStream and will return a Stream<Integer>.

在这个例子中,我们使用了range方法。 这里最臭名昭著的部分是使用方法boxed,正如它的名字所指出的,它将框住IntStream中的所有int元素并返回一个Stream<Integer>

Finally, we can use a collector to get a list of integers.

最后,我们可以使用一个收集器来获得一个整数的列表。

4. IntStream to String

4.IntStreamString

For our last topic, let’s explore how we could get a String from an IntStream.

对于我们的最后一个主题,让我们探讨一下如何从IntStream中获得一个String

In this case, we will generate just the first 3 ints (0, 1 and 2):

在这种情况下,我们将只生成前3个ints(0、1和2)。

@Test
public void intStreamToString() {
  String first3numbers = IntStream.of(0, 1, 2)
    .mapToObj(String::valueOf)
    .collect(Collectors.joining(", ", "[", "]"));

  assertThat(first3numbers).isEqualTo("[0, 1, 2]");
}

First, in this case, we construct an IntStream with the constructor IntStream.of(). After having the Stream, we need to somehow generate a Stream<String> from an IntStream. Hence, we can use the intermediate mapToObj method that will take an IntStream and will return a Stream of the type of the resulting object mapped in the method called.

首先,在这种情况下,我们用构造函数IntStream构建一个IntStream.of()。有了Stream之后,我们需要以某种方式IntStream生成Stream<String>。因此,我们可以使用中间的mapToObj方法,该方法将接收一个IntStream,并返回一个Stream,该类型的对象在所调用的方法中被映射。

Finally, we use the collector joining that takes a Stream<String> and can append each element of the Stream by using a delimiter, and optionally a prefix and suffix.

最后,我们使用收集器joining,它接收一个Stream<String>,并可以通过使用分隔符,以及可选的前缀和后缀来追加Stream的每个元素。

5. Conclusions

5.结论

In this quick tutorial, we have explored all the alternatives when we need to convert an IntStream to any other type. In particular, we went through examples as generating an array, a List, and a String.

在这个快速教程中,我们探讨了当我们需要将IntStream转换为任何其他类型时的所有选择。特别是,我们经历了生成一个数组、一个List和一个String的例子。

And, as always, the sample code is available over on GitHub.

而且,像往常一样,样本代码可以在GitHub上找到