10 Ways to Create a Stream in Java

Stream API is the most important addition in Java 8. In this post, we 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

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 specified values using 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. Arrays.stream()

We can use Arrays.stream() to create a sequential Stream with the specified array as its source. stream() method is overloaded – 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. Stream.of()

We can also pass an array to 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 elements can be added 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()

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

 

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

Another way of creating an infinite stream of any custom type elements is by passing a function 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 stream from a sequence that matches a pattern

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

 

Download   Run Code

Output:

[Techie, Delight]

 

9. Create stream from Iterator

Please Refer: Create a Sequential Stream from an Iterator in Java

 

10. Create stream from Iterable

Please Refer: Convert an Iterable to Stream in Java

 
 
Thanks for reading.

Please use our online compiler to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
Notify of