About Question enthuware.ocpjp.v8.3.1889 :

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

Moderator: admin

Post Reply
henrid
Posts: 31
Joined: Sun Oct 13, 2013 1:20 pm
Contact:

About Question enthuware.ocpjp.v8.3.1889 :

Post by henrid »

Why is collect(Collectors.averagingDouble(...)) returning a Double and not an OptionalDouble, just like the DoubleStream.average() method?
collect(Collectors.maxBy(...)) results in an Optional<T> just like Stream.max(), so why this difference?

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

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

Post by admin »

Because Collectors.averagingDouble(...) takes in a ToDoubleFunction, which returns a double instead of OptionalDouble or Optional<T> (as is the case with maxBy).
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:

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

Post by henrid »

Okay thanks, and what if the stream is empty? Will it return 0? Or throw an Exception?

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

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

Post by admin »

What happened when you tried it out?
BTW, the Collectors javadoc, clearly says, "If no elements are present, the result is 0.".
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:

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

Post by henrid »

I was planning to try it out, and I read a lot of API, but I missed that. It takes a lot of time to study all the thousand details that are not in de books, but Enthuware exam kits are very helpful. Thanks!

Post Reply

Who is online

Users browsing this forum: No registered users and 46 guests