Last active
September 8, 2019 21:05
-
-
Save cenkc/000c467cca5e2e43b7b283df6482442f to your computer and use it in GitHub Desktop.
Iteration with Streams API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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