Skip to content

Instantly share code, notes, and snippets.

@orend
Last active September 27, 2019 14:11
Show Gist options
  • Save orend/208ec76e505f8c15f6ba78b45e25e449 to your computer and use it in GitHub Desktop.
Save orend/208ec76e505f8c15f6ba78b45e25e449 to your computer and use it in GitHub Desktop.
// given
Stream<Integer> infiniteStream = Stream.iterate(0, i -> i + 2);
// when
List<Integer> collect = infiniteStream
.limit(10)
.collect(Collectors.toList());
// then
assertEquals(collect, Arrays.asList(0, 2, 4, 6, 8, 10, 12, 14, 16, 18));
---------------------------------
int[] fibs = {0, 1};
Stream<Integer> fibonacci = Stream.generate(() -> {
int result = fibs[1];
int fib3 = fibs[0] + fibs[1];
fibs[0] = fibs[1];
fibs[1] = fib3;
return result;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment