In stream API we can Use any number of terminal operations to generate desired output
Java 8 has brought a lot of features and Stream API is one of the most popular feature of the Java 8. In this post, we will see as how to create infinite stream with Java 8. Show
Advertisements Infinite stream with Java 8There are mainly two methods defined in the Stream class to create infinite streams and both are 1 defined in the Stream class.
Before we dive into the details of infinite stream with Java 8, let’s start with a few basic concepts of Java Stream API 1. Intermediate and terminal operationsThere are many operations that can be used with a stream object. A pipeline is created whenever we create a stream from a source. We can put these operations inside the pipeline to get the desired output. Stream operations are divided into categories: 4 and 5. Intermediate operations return one stream and terminal operations return non-stream values.Both of these 4 and 5 operations are combined to create a stream pipeline.We create a stream pipeline from a source like an 8 or 9 and it is followed by zero or more 4 operations and finally a terminal operation. We can’t use a stream after the terminal operation is completed. The stream is considered as consumed.Following are the intermediate operations we can use with a stream:
And terminal operations are:
We can use more than one intermediate operation, but these operations will be executed only after a terminal operation is called. All intermediate operations are executed immediately once a terminal operation is called.We can also create an infinite stream without using a terminal operation. It will run for an indefinite amount of time, similar to an infinite loop. 2. How to create an infinite streamCollections are not infinite. We can hold only a finite number of values in a collection. But stream can produce values infinitely.Stream API has two different static methods defined to create an 0: generate and iterate. The generate method is defined as below:
1 is the type of stream elements and 2 is the supplier. The supplier is used to generate each value in the stream. It returns an infinite sequential unordered stream.The Iterate method is defined as below:
Similar to 3, 1 is the type of stream elements. seed is the initial element. This element is used with the function 5 to produce the elements. The function 5 is applied to the previous element to produce the current element.This method returns an infinite sequential ordered stream.The difference between 7 and 8 is that 7 returns one infinite sequential ordered stream but 8 returns one infinite sequential unordered stream. We can use any of these two methods to create an infinite stream.2.1 ExampleLet’s try to understand 7 with an example. As we have seen before, the first argument of 7 is a 3 or starting value and the second argument is a 4 that calculates the current value based on the previous value.Let’s take a look at the below example:
Output: Advertisements Advertisements
Let’s take a look at some important points:
2.2. Stream.generate method to create an infinite stream with Java 8The 9 method takes only one function and generates the data using that function. Let’s try it with an example program:
Output:
2.3 Create an infinite integer streamIn this example, we will create an infinite integer stream. We can use IntStream.iterate or 6to create an infinite stream. Let’s take a look at the below example:
If you run this program, it will keep running indefinitely. We are using iterate with the seed value 0. On each step, it is adding 1 to the previous value. The forEach terminal operator is used to print the values in a new line.Since we are not using any intermediate operator, it will not stop. Either you need to stop it manually or it will run out of memory error. 2.4. Create an infinite random integer streamThe above example uses 7 to create an infinite integer stream. In this example, we will use 8. As explained before, this method takes one 9 and generates the values based on this function. Since we need to create an infinite random integer stream, We can pass one random number generator function to 8. Below is the complete program:
Output:
It might produce different outputs on your machine. Since we are not using any terminal operation here, this program will keep running indefinitely and it will keep printing random numbers until we stop the program.We can use a terminal operation like 1to limit the number of execution.2.5 Create an infinite stream with custom objectsIn a real-world application, we have to deal with custom objects rather than predefined data types. It works in a similar manner. Let’s take a look at the below program
If you run this program, it will print something like below 0SummaryIn this article we saw how to create infinite stream with Java 8.We learned how to create an infinite stream using 2 and 3 methods with different examples. The source code for this article is available on the GitHub repository.
How many terminal operations can a stream pipeline have?A stream can have only one terminal operation, it cannot be chained.
What is the terminal operation available in stream API?The Stream API gives you two terminal operations to find an element: findFirst() and findAny() . These two methods do not take any argument and return a single element of your stream. To properly handle the case of empty streams, this element is wrapped in an optional object.
How many terminal operations can you perform use on any given stream?Pipeline of operations can have maximum one terminal operation, that too at the end. Intermediate operations are lazily loaded.
Can we have stream without terminal operation?Non-Terminal Operations. The non-terminal stream operations of the Java Stream API are operations that transform or filter the elements in the stream. When you add a non-terminal operation to a stream, you get a new stream back as result.
|