Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring 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:

public void intStreamToArray() {
  int[] first50EvenNumbers = IntStream.iterate(0, i -> i + 2)


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

public void intStreamToList() {
  List<Integer> first50IntegerNumbers = IntStream.range(0, 50)


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

public void intStreamToString() {
  String first3numbers = IntStream.of(0, 1, 2)
    .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.

Java bottom

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

Generic footer banner
Comments are closed on this article!