Method iterate() of Stream object in Java

In Java 8, the iterate() method has been introduced and it has only two parameters and will help us to create an infinite stream.

Content:

Example:

When running the above example, you will see that a Stream object contains endless numbers beginning at 1.

With Java 9, the iterate() method has improved by adding another parameter, the Predicate interface, which allows us to stop these endless numbers based on the conditions we define with the Predicate interface.

Content:

Example:

Result:

Method iterate() of Stream object in Java


Add Comment