Skip to content

Instantly share code, notes, and snippets.

@truongquoc
Created October 17, 2023 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save truongquoc/72459ee610ce1d8c77ddb9f191c61dd9 to your computer and use it in GitHub Desktop.
Save truongquoc/72459ee610ce1d8c77ddb9f191c61dd9 to your computer and use it in GitHub Desktop.
stream API
public void lazyEvaluate() {
List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
long count = numbers.stream()
.filter(n -> {
System.out.println("Filtering: " + n);
return n % 2 == 0;
})
.map(n -> {
System.out.println("Mapping: " + n);
return n * 2;
})
.peek(n -> System.out.println("Peeking: " + n))
.collect(Collectors.counting());
System.out.println("Total count of even numbers: " + count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment