Given:
What is the output?
Given:
What is the output?
The code defines a stream processing pipeline that converts a list of Person objects to an IntStream of their ages, filters out ages that are 20 or older, and then sums the remaining ages. Specifically, the filter condition is p < 20, meaning only ages less than 20 are included in the sum. The ages of the people are 18, 23, 23, and 12. From these, the ages that are less than 20 are 18 and 12. These are summed to produce 30, which is the output.
persons.stream() .mapToInt(Person::getAge) .filter(p->p<20) .reduce(0, (a,b)->a+b); Here the order of operations is not optimal The main operations in a Stream pipeline and the order in which they occur are: filter sorted map collect IntStream mapToInt(ToIntFunction<? super T> mapper) ToIntFuncion f = (Person x) -> x.getAge(); or method reference Person::getAge Stream<T> filter(Predicate<T> predicate) Predicate predicate = p->p<20 int reduce(int identity, IntBinaryOperator op) IntBinarytOperator<T> = (a,b) -> a+b
predicated filter is < 20 so that elements match with condition, sum 18 + 12 => 20
correct 30
the correct answer is A, tested
A is correct is a simple sum for all age that match with the condition. reduce begin in 0 and sum all elements