The Stream is the most important addition in Java 8 and above. This post will discuss various ways to create a stream in Java.

There are many ways to create a stream in Java, which are discussed below.

1. Create a stream from Collection

The Java Collection framework provides two methods, stream() and parallelStream(), to create a sequential and parallel stream from any collection, respectively.

Download  Run Code

Output:

[Java, 8, Stream, API]
[Java, 8, Stream, API]

2. Create a stream from specified values

We can create a sequential stream from the specified values using the Stream.of() method.

Download  Run Code

Output:

[1, 4, 2, 5, 8]

3. Create stream from an array

There are two methods to create a stream from an array:

1. Using Arrays.stream() method

We can use Arrays.stream() to create a sequential stream with the specified array as its source. stream() method is overloaded – the first version takes the whole array, and the other version takes a part of an array.

Download  Run Code

Output:

[Java, 8, Stream, API]
[Java, 8]

2. Using Stream.of() method

We can also pass an array to the Stream.of() method, which takes varargs.

Download  Run Code

Output:

[Java, 8, Stream, API]

4. Create an empty stream

We can use Stream.empty() or Stream.of() method to create an empty stream in Java.

Download  Run Code

Output:

[]

5. Create a stream from Builder

We can use a mutable builder for creating an ordered Stream. A stream builder has a lifecycle, which starts in a building phase, during which we can add elements in the required order, and then transitions to a built phase when the build() method is called.

Download  Run Code

Output:

[Java, 8]
[Stream, API]

6. Create an infinite stream using Stream.iterate() method

We can use the iterate() method to create an infinite stream that accepts two parameters – a seed which is the first term in the stream, and a function to be applied to the previous term of the stream to produce the value of the next term in the stream. We can limit the stream using the limit() method.

Download  Run Code

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0.0, 1.0, 2.0, 3.0, 4.0]

7. Create an infinite stream using Stream.generate() method

Another way of creating an infinite stream of any custom type elements is by passing a method of a Supplier interface to a generate() method on a stream. There are many suppliers provided by Java that we can use, as shown below:

Download  Run Code

Output (will vary):

[476ecc82-de26-4af6-aac0-1ca6dceaa6a0, f381352c-07e5-443e-a183-f697609244e2]
[0.914389687213923, 0.580971605583907, 0.8540320907003832, 0.14422141325963556]
[-2028513234, -1113287020]
[313246806, -895656428, -1799486714, -1662161615, -2062125349]

8. Create a stream from a sequence that matches a pattern

We can create a stream from an input sequence that matches a pattern using the Pattern.splitAsStream() method, as shown below:

Download  Run Code

Output:

[Techie, Delight]

 
Also see:

Create a sequential Stream from an iterator in Java

Convert an Iterable to Stream in Java

That’s all about creating a Stream in Java.