About Question enthuware.ocpjp.v8.2.1799 :
Posted: Mon Mar 29, 2021 7:09 am
I think that the explanation is wrong :
If you append a terminal operation such as count(), [for example, ls.stream().map(func).peek(System.out::print).count(); ], it will print 149.
When using the count() operation on a Stream , if there are no other intermediate operation who can change the number of elements of the Stream( example : filter .. ) , the count operation will execute directly on the source of Stream and no intermediate operation will be executed .
List<Integer> ls = Arrays.asList(1, 2, 3);
Function<Integer, Integer> func = a->a*a; //1
ls.stream().map(func).peek(System.out::print).count(); //2 //will run but will not print anything
map operation has no effect on the number of elements of the Stream ,so it will be never executed
?
If you append a terminal operation such as count(), [for example, ls.stream().map(func).peek(System.out::print).count(); ], it will print 149.
When using the count() operation on a Stream , if there are no other intermediate operation who can change the number of elements of the Stream( example : filter .. ) , the count operation will execute directly on the source of Stream and no intermediate operation will be executed .
List<Integer> ls = Arrays.asList(1, 2, 3);
Function<Integer, Integer> func = a->a*a; //1
ls.stream().map(func).peek(System.out::print).count(); //2 //will run but will not print anything
map operation has no effect on the number of elements of the Stream ,so it will be never executed
?