Java 8 – Convert IntStream to Array – Java Code Geeks

I show You how To Make Huge Profits In A Short Time With Cryptos!

A quick guide on how to convert IntSteam to Array in Java 8.

1. Overview

In this tutorial we will learn how to convert IntStream to array of ints in java 8.

IntStream is used for create endless flows with number series pattern.

But, some of the time, we might need to convert the series of numbers to an array.

2. Java 8 – Convert IntStream to array

Take the example to generate the first 100 odd numbers from IntStream and put them together in an array of integers.


After creating the IntStream, we need to extract the first 100 values ​​from it using limit (100) function. Then use the collection terminal operation using toArray () method. toArray () The method converts the intermediate stream to an inter array.

package com.javaprogramto.java8.intstream.toarray;


public class IntStreamToArrayExample {

	public static void main(String[] args) {
		IntStream oddNumbers = IntStream.iterate(1, i -> i +2);
		int[] oddArray = oddNumbers.limit(100).toArray();
		System.out.println("Odd array length - "+oddArray.length);


Go out

Odd array length - 100

3. Conclusion

In this article we have seen how to convert an int stream to an array of integer values ​​in java 8.


IntStream API

IntStream Examples

Source link