Page 1 of 1
About Question enthuware.ocpjp.v8.2.1891 :
Posted: Fri Oct 30, 2015 9:22 pm
by in2xes
4. System.out.println(names.stream().collect(Collectors.mapping(x->x, Collectors.summarizingInt(x->x))).getSum());
5. System.out.println(names.stream().collect(Collectors.summarizingInt(x->x)).getSum());
both options does not even compile. so answer is 2.
Or change 4 and 5 to
System.out.println(names.stream().collect(Collectors.summarizingInt(x->Integer.valueOf(x.toString()))).getSum());
System.out.println(names.stream().collect(Collectors.mapping(x->x, Collectors.summarizingInt(x->Integer.valueOf(x.toString())))).getSum());
Re: About Question enthuware.ocpjp.v8.2.1891 :
Posted: Fri Oct 30, 2015 10:27 pm
by admin
Not sure why you think it will not compile. I just compiled the code and ran it. It worked as expected:
Code: Select all
import java.util.*;
import java.util.stream.*;
public class TestClass {
public static void main(String[] args) {
List<Integer> names = Arrays.asList(1, 2, 3);
System.out.println(names.stream().mapToInt(x->x).sum());
System.out.println(names.stream().reduce(0, (a, b)->a+b));
System.out.println(names.stream().collect(Collectors.mapping(x->x, Collectors.summarizingInt(x->x))).getSum());
System.out.println(names.stream().collect(Collectors.summarizingInt(x->x)).getSum());
}
}
It prints 6 four times.
Re: About Question enthuware.ocpjp.v8.2.1891 :
Posted: Fri Oct 30, 2015 11:03 pm
by in2xes
Sorry. One of the option is working but not other. I'm yet to figure out the root cause. See attached image for compilation failure.

Re: About Question enthuware.ocpjp.v8.2.1891 :
Posted: Sat Oct 31, 2015 3:19 am
by admin
Can't comment on messages shown by an IDE. Try the command line.
Re: About Question enthuware.ocpjp.v8.2.1891 :
Posted: Sun Sep 18, 2016 6:00 am
by ramy6_1
Hello ,
At Oracle exam topics section 4.6 , it mentioned averagingDouble(), groupingBy(), joining(), and partitioningBy() from Collector class.
Does that mean other functions also included at exam I.e Collectors.summarizingInt ?
Re: About Question enthuware.ocpjp.v8.2.1891 :
Posted: Sun Sep 18, 2016 10:52 am
by admin
Yes, you should know about summarizingInt also. It is quite simple though.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v8.2.1891 :
Posted: Mon Sep 23, 2019 11:14 am
by henrid
It is ridiculous having to memorize the whole API. That is NOT simple. But okay, you guys didn't create the exam.