About Question enthuware.ocpjp.ii.v11.2.3331 :
Moderators: Site Manager, fjwalraven
-
- Posts: 12
- Joined: Mon Apr 03, 2017 6:08 am
- Contact:
About Question enthuware.ocpjp.ii.v11.2.3331 :
Why is this question in the category "09 - Parallel Streams"?
Question and answer options don't talk about parallel streams at all!
Question and answer options don't talk about parallel streams at all!
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3331 :
Yes, it should be in "Builtin Functional interface"
thank you for your feedback!
thank you for your feedback!
-
- Posts: 14
- Joined: Thu Nov 21, 2019 5:48 am
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3331 :
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()
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()
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3331 :
RIght. Fixed.
thank you for your feedback!
thank you for your feedback!
-
- Posts: 5
- Joined: Tue Sep 01, 2020 12:58 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3331 :
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.
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.
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3331 :
Yes, the explanation should explain that. Updated.
thank you for your feedback!
thank you for your feedback!
-
- Posts: 21
- Joined: Thu May 05, 2016 2:50 am
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3331 :
regarding the comment in the Explanation:
the mapToDouble returns a DoubleStream and the reduce method from that type of Stream returns a double instead of Double.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
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3331 :
You are right, it should say Double.
-
- Posts: 21
- Joined: Thu May 05, 2016 2:50 am
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3331 :

-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3331 :
No, the code given there is bkStrm.map(b->b.getPrice()).reduce(0.0, bo)
Here, reduce returns Double.
Here, reduce returns Double.
-
- Posts: 21
- Joined: Thu May 05, 2016 2:50 am
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3331 :
I mean this one:
bkStrm.mapToDouble(b->b.getPrice()).reduce(0.0, dbo);
does it return a Double?
bkStrm.mapToDouble(b->b.getPrice()).reduce(0.0, dbo);
does it return a Double?
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3331 :
Oh right. I see what you mean. Yes, it should be double. Fixed.
thank you for your feedback!
thank you for your feedback!
-
- Posts: 1
- Joined: Thu Aug 25, 2022 3:44 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3331 :
Not fixed yet 

-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3331 :
Could you please tell me where are you seeing it not fixed? I checked that is it fixed in 819 and 829 question banks.
-
- Posts: 29
- Joined: Sun Feb 18, 2024 7:21 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3331 :
Code: Select all
OR double total = bkStrm.map(b->b.getPrice()).reduce(0.0, bo).get(); Stream<Double>.reduce(double, BinaryOperator) returns Double
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3331 :
Oh ok, got it now. It is in last example of the explanation. You are right, get() should not be there.
Who is online
Users browsing this forum: Bing [Bot] and 5 guests