Page 1 of 1
About Question enthuware.ocpjp.v8.3.1889 :
Posted: Tue Sep 10, 2019 12:49 pm
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?
Re: About Question enthuware.ocpjp.v8.3.1889 :
Posted: Tue Sep 10, 2019 10:56 pm
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).
Re: About Question enthuware.ocpjp.v8.3.1889 :
Posted: Wed Sep 11, 2019 12:38 am
by henrid
Okay thanks, and what if the stream is empty? Will it return 0? Or throw an Exception?
Re: About Question enthuware.ocpjp.v8.3.1889 :
Posted: Wed Sep 11, 2019 2:22 am
by admin
What happened when you tried it out?
BTW, the
Collectors javadoc, clearly says, "If no elements are present, the result is 0.".
Re: About Question enthuware.ocpjp.v8.3.1889 :
Posted: Wed Sep 11, 2019 10:07 am
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!