Skip to content

Instantly share code, notes, and snippets.

@cenkc
Last active September 8, 2019 21:05
Show Gist options
  • Save cenkc/000c467cca5e2e43b7b283df6482442f to your computer and use it in GitHub Desktop.
Save cenkc/000c467cca5e2e43b7b283df6482442f to your computer and use it in GitHub Desktop.
Iteration with Streams API
// BigDecimal iterator 1 thru 10
Stream.iterate(
BigDecimal.ONE,
bigDecimal -> bigDecimal.add(BigDecimal.ONE)
)
.limit(10)
.forEach(System.out::println);
// Integer iterator 1 thru 10
Stream.iterate(1,n -> n + 1).limit(10).forEach(System.out::println);
// Long iterator 1 thru 10
LongStream.iterate(1, n -> n + 1).limit(10).forEach(System.out::println);
// Factorial
final long factorial = LongStream
.rangeClosed(1, 10)
.reduce(1, (long a, long b) -> a * b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment