About Question enthuware.ocpjp.ii.v11.2.3331 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
entitybean
Posts: 12
Joined: Mon Apr 03, 2017 6:08 am
Contact:

About Question enthuware.ocpjp.ii.v11.2.3331 :

Post by entitybean »

Why is this question in the category "09 - Parallel Streams"?

Question and answer options don't talk about parallel streams at all!

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

Re: About Question enthuware.ocpjp.ii.v11.2.3331 :

Post by admin »

Yes, it should be in "Builtin Functional interface"
thank you for your feedback!

kabanvau
Posts: 14
Joined: Thu Nov 21, 2019 5:48 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3331 :

Post by kabanvau »

Using Stream<Double> and BinaryOperator<Double>:
BinaryOperator<Double> bo =  (a, b)->a+b;
double total = bkStrm.map(b->b.getPrice()).reduce(dbo).get(); //reduce returns Optional<Double>, so, need to call get()
OR
double total = bkStrm.map(b->b.getPrice()).reduce(0.0, dbo).get(); //reduce returns Double

It should be:
double total = bkStrm.map(b->b.getPrice()).reduce(bo).get(); //reduce returns Optional<Double>, so, need to call get()
OR
double total = bkStrm.map(b->b.getPrice()).reduce(0.0, bo).get(); //without get()

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

Re: About Question enthuware.ocpjp.ii.v11.2.3331 :

Post by admin »

RIght. Fixed.
thank you for your feedback!

carlosx
Posts: 5
Joined: Tue Sep 01, 2020 12:58 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3331 :

Post by carlosx »

on second option (and explanation):
double total = bkStrm.map(b->b.getPrice()).reduce((a, b)->{ return a+b;}).ifPresent(p->p.doubleValue());
The single parameter reduce method returns an OptionalDouble (if it is a DoubleStream) or Optional<Double> if it is Stream<Double>.
OptionalDouble has getAsDouble() that returns a Double and Optional<Double> has get() that returns a Double.

Shouldn't is say that the problem here is that ifPresent returns void?? is ok the explanation about the OptionalDouble but the real issue is you cant assign void to a variable. Looks to me that the explanation doesn't have anything about the issue or the real problem of this answer option.

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

Re: About Question enthuware.ocpjp.ii.v11.2.3331 :

Post by admin »

Yes, the explanation should explain that. Updated.
thank you for your feedback!

dfigueira
Posts: 21
Joined: Thu May 05, 2016 2:50 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3331 :

Post by dfigueira »

regarding the comment in the Explanation:
DoubleBinaryOperator dbo =  (a, b)->a+b;
double total = bkStrm.mapToDouble(b->b.getPrice()).reduce(dbo).getAsDouble(); //reduce returns OptionalDouble, so, need to call getAsDouble()
OR
double total = bkStrm.mapToDouble(b->b.getPrice()).reduce(0.0, dbo);//reduce returns Double
the mapToDouble returns a DoubleStream and the reduce method from that type of Stream returns a double instead of Double.

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

Re: About Question enthuware.ocpjp.ii.v11.2.3331 :

Post by admin »

You are right, it should say Double.

dfigueira
Posts: 21
Joined: Thu May 05, 2016 2:50 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3331 :

Post by dfigueira »

:?: did you mean double?

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

Re: About Question enthuware.ocpjp.ii.v11.2.3331 :

Post by admin »

No, the code given there is bkStrm.map(b->b.getPrice()).reduce(0.0, bo)
Here, reduce returns Double.

dfigueira
Posts: 21
Joined: Thu May 05, 2016 2:50 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3331 :

Post by dfigueira »

I mean this one:
bkStrm.mapToDouble(b->b.getPrice()).reduce(0.0, dbo);

does it return a Double?

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

Re: About Question enthuware.ocpjp.ii.v11.2.3331 :

Post by admin »

Oh right. I see what you mean. Yes, it should be double. Fixed.
thank you for your feedback!

aldabil
Posts: 1
Joined: Thu Aug 25, 2022 3:44 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3331 :

Post by aldabil »

Not fixed yet :D

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

Re: About Question enthuware.ocpjp.ii.v11.2.3331 :

Post by admin »

Could you please tell me where are you seeing it not fixed? I checked that is it fixed in 819 and 829 question banks.

likejudo
Posts: 29
Joined: Sun Feb 18, 2024 7:21 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3331 :

Post by likejudo »

Code: Select all

OR double total = bkStrm.map(b->b.getPrice()).reduce(0.0, bo).get(); Stream<Double>.reduce(double, BinaryOperator) returns Double
Why is there a get() if it returns double?

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

Re: About Question enthuware.ocpjp.ii.v11.2.3331 :

Post by admin »

Oh ok, got it now. It is in last example of the explanation. You are right, get() should not be there.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests