How to Iterate a Stream in Java


Here we can learn how to iterate a stream with Stream.iterate.

Source Code

package com.beginner.examples;

import java.util.stream.Stream;

public class StreamIterateExample {

    public static void main(String[] args){
    	//use Stream.iterate().
    	Stream.iterate(1, i -> i + 2).limit(2).forEach(x -> System.out.println(x));
    }
}

Output:

1
3

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments