This post will discuss various methods to convert array to stream in Java.

1. Convert Boxed Array to Stream

A Stream<T> can be constructed from a boxed array using one of the following methods:

⮚ Using Arrays.stream()

⮚ Using Stream.of()

⮚ Using List.stream()

2. Convert Primitive Array to Stream

When we convert primitive array to stream, we’ll get primitive streams like IntStream, DoubleStream, and LongStream. We can obtain these primitive streams by using one of the following methods:

⮚ Using Arrays.stream()

⮚ Using IntStream.of()

 
Note – Arrays.asList(arr).stream() and Stream.of(arr) will return Stream<int[]> not IntStream.

3. Filter Array using Stream

We can also filter an array to match certain criteria using streams, as shown below:

Download  Run Code

Output:

NYC
New Delhi

4. Convert stream back to array

We might often have a stream of elements and want to convert it back to the array. We can easily do so, as shown below:

Download  Run Code

Output:

[NYC, Mexico, Beijing, New Delhi, Tokyo]

 
This is equivalent to:

Download  Run Code

Output:

[NYC, Mexico, Beijing, New Delhi, Tokyo]

That’s all about converting an array to Stream in Java.