Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW