How to create an infinite stream with Java 8

Yes, there is an easy way:

IntStream.iterate(0, i -> i + 2);

With as usecase:

IntStream.iterate(0, i -> i + 2)
         .limit(100)
         .forEach(System.out::println);

Which prints out 0 to 198 increasing in steps of 2.

The generic method is:

Stream.iterate(T seed, UnaryOperator<T> f);

The latter may be more uncommon in usage.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)