About Question enthuware.ocpjp.v8.2.1901 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
lerato
Posts: 1
Joined: Fri Mar 13, 2020 2:45 am
Contact:

About Question enthuware.ocpjp.v8.2.1901 :

Post by lerato »

In need of help on the following question ?
Given:
List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17); //1
Stream<Integer> primeStream = primes.stream(); //2

Predicate<Integer> test1 = k->k<10; //3

the given answer on the enthuware mockExam says the correct answer is as follows .

primeStream.collect(Collectors.partitioningBy(test1,Collectors.counting())).values().forEach(System.out::print)

can you make a method call like values() on a terminal operator like the callect() method ? does it even compile ?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1901 :

Post by admin »

What happened when you tried to compile/run it?
values() returns a new Stream and you can certainly process the elements of that stream using forEach.
If you like our products and services, please help us by posting your review here.

RogierA
Posts: 4
Joined: Fri Oct 16, 2020 4:15 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1901 :

Post by RogierA »

why are the values switched in the last (correct) option? from 4 then 3, to 3 then 4.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1901 :

Post by admin »

Since you are passing Collectors.partitioningBy to primeStream.collect, the result is actually a map containing key value pairs (true,4) and (false, 3). Now, when you call values().forEach on this map, the order of the values returned is implementation dependent (i.e. depends on the implementation of the map generated by the collect method). In this case, it returns 3, 4 but some other implementation may return 4, 3.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 48 guests