Stream iterate(T,Predicate,UnaryOperator) method in Java with examples Last Updated : 25 Apr, 2019 Comments Improve Suggest changes Like Article Like Report The iterate(T, java.util.function.Predicate, java.util.function.UnaryOperator) method allows us to iterate stream elements till the specified condition. This method returns a sequential ordered Stream produced by iterative application of the given next function to an initial element, conditioned on satisfying hasNext predicate passed as parameter. The stream terminates as soon as the hasNext predicate returns false. The resulting sequence returned by this method may be empty if passed predicate does not hold on the seed value. Otherwise, the first element will be the supplied seed value, the next element will be the result of applying the next function to the seed value, and so on iteratively until the hasNext predicate indicates that the stream should terminate. Syntax: static <T> Stream<T> iterate(T seed, Predicate<T> hasNext, UnaryOperator<T> next) Parameters: This method accepts three parameters: seed: which is the initial element, hasNext: which is a predicate to apply to elements to determine when the stream must terminate and next: which is a function to be applied to the previous element to produce a new element. Return value: This method returns a new sequential Stream. Below programs illustrate iterate(T, java.util.function.Predicate, java.util.function.UnaryOperator) method: Program 1: Java // Java program to demonstrate // Stream.iterate method import java.util.stream.Stream; public class GFG { public static void main(String[] args) { // create a stream using iterate Stream<Integer> stream = Stream.iterate(1, i -> i <= 20, i -> i * 2); // print Values stream.forEach(System.out::println); } } The output printed on console of IDE is shown below. Output: Program 2: Java // Java program to demonstrate // Stream.iterate method import java.util.stream.Stream; public class GFG { public static void main(String[] args) { // create a stream using iterate Stream<Double> stream = Stream.iterate(2.0, decimal -> decimal > 0.25, decimal -> decimal / 2); // print Values stream.forEach(System.out::println); } } The output printed on console of IDE is shown below. Output: References: https://docs.oracle.com/javase/10/docs/api/java/util/stream/Stream.html#iterate(T, java.util.function.Predicate, java.util.function.UnaryOperator) Comment More infoAdvertise with us Next Article Stream iterate(T,Predicate,UnaryOperator) method in Java with examples A AmanSingh2210 Follow Improve Article Tags : Java Java - util package Java-Functions java-stream Practice Tags : Java Similar Reads Stream peek() Method in Java with Examples In Java, Stream provides an powerful alternative to process data where here we will be discussing one of the very frequently used methods named peek() which being a consumer action basically returns a stream consisting of the elements of this stream, additionally performing the provided action on ea 2 min read Optional stream() method in Java with examples The stream() method of java.util.Optional class in Java is used to get the sequential stream of the only value present in this Optional instance. If there is no value present in this Optional instance, then this method returns returns an empty Stream. Syntax: public Stream<T> stream() Paramete 2 min read OptionalInt stream() method in Java with examples The stream() method help us to get value contain by OptionalInt as IntStream. If a value is present, method returns a sequential IntStream containing only that value, otherwise returns an empty IntStream. Syntax: public IntStream stream() Parameters: This method accepts nothing. Return value: This m 1 min read Stream takeWhile() method in Java with examples The takeWhile(java.util.function.Predicate) method returns a stream of the remaining elements of this stream after taken the longest prefix of elements that match the given predicate if the stream is ordered else a stream of a subset of elements taken from this stream that match the given predicate. 3 min read Stream noneMatch() Method in Java with Examples A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. noneMatch() of Stream class returns whether no elements of this stream match the provided predicate. It may not evaluate the predicate on all elements if not necessary for determinin 3 min read Stream ofNullable(T) method in Java with examples The ofNullable(T) method returns a sequential Stream containing a single element if this stream is non-null otherwise method returns an empty Stream. It helps to handle the null stream and NullPointerException. Syntax: static <T> Stream<T> ofNullable(T t) Parameters: This method accepts 2 min read Stream skip() method in Java with examples Prerequisite : Streams in java The skip(long N) is a method of java.util.stream.Stream object. This method takes one long (N) as an argument and returns a stream after removing first N elements. skip() can be quite expensive on ordered parallel pipelines, if the value of N is large, because skip(N) 3 min read Stream.of(T t) in Java with examples Stream of(T t) returns a sequential Stream containing a single element i.e, a singleton sequential stream. A sequential stream work just like for-loop using a single core. On the other hand, a Parallel stream divide the provided task into many and run them in different threads, utilizing multiple co 2 min read Stream.of(T... values) in Java with examples Stream of(T... values) returns a sequential ordered stream whose elements are the specified values. A sequential stream works like a for-loop using a single core. On the other hand, a Parallel stream divides the provided task into many tasks and runs them in different threads, utilizing multiple cor 3 min read Stream mapToInt() in Java with examples Stream mapToInt(ToIntFunction mapper) returns an IntStream consisting of the results of applying the given function to the elements of this stream. Stream mapToInt(ToIntFunction mapper) is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream in 2 min read Like