Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

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

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

2. IntStream to Array

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

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

@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.

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

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

3. IntStream to List

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

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):

@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>.

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

4. IntStream to String

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

In this case, we will generate just the first 3 ints (0, 1 and 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.

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.

5. Conclusions

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.

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

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.