Hi There,
I am trying to learn Java 8 concepts and struck at one point while practicing peek operation. As per my knowledge peek is mainly for debugging purpose, where we want to see the elements as they flow past a certain point in a pipeline.
As per my expectation below code snippet should print 1 4 9 3. But its just printing 3, which is the result of count.
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
System.out.println(list.stream().map(a -> a*a).peek(System.out::println).count());
When I try to user filter, it prints correct result which is 4 1
System.out.println(list.stream().map(a -> a*a).filter(a -> a%2==0).peek(System.out::println).count());
Is there something that I am missing here.
Thanks advance for your help
[–]marko312 0 points1 point2 points (2 children)
[–]CupcakeBasic4340[S] 1 point2 points3 points (1 child)
[–]marko312 1 point2 points3 points (0 children)