Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817
Moderator: admin
in2xes
Posts: 6 Joined: Tue Oct 20, 2015 9:47 am
Contact:
Post
by in2xes » Fri Oct 30, 2015 9:22 pm
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());
admin
Site Admin
Posts: 10115 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Fri Oct 30, 2015 10:27 pm
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.
If you like our products and services, please help us by posting your
review here .
in2xes
Posts: 6 Joined: Tue Oct 20, 2015 9:47 am
Contact:
Post
by in2xes » Fri Oct 30, 2015 11:03 pm
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.
admin
Site Admin
Posts: 10115 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Sat Oct 31, 2015 3:19 am
Can't comment on messages shown by an IDE. Try the command line.
If you like our products and services, please help us by posting your
review here .
ramy6_1
Posts: 124 Joined: Wed Feb 12, 2014 2:44 am
Contact:
Post
by ramy6_1 » Sun Sep 18, 2016 6:00 am
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 ?
admin
Site Admin
Posts: 10115 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Sun Sep 18, 2016 10:52 am
Yes, you should know about summarizingInt also. It is quite simple though.
HTH,
Paul.
If you like our products and services, please help us by posting your
review here .
henrid
Posts: 31 Joined: Sun Oct 13, 2013 1:20 pm
Contact:
Post
by henrid » Mon Sep 23, 2019 11:14 am
It is ridiculous having to memorize the whole API. That is NOT simple. But okay, you guys didn't create the exam.
Users browsing this forum: No registered users and 6 guests