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?
About Question enthuware.ocpjp.v8.3.1889 :
Moderator: admin
-
- Posts: 31
- Joined: Sun Oct 13, 2013 1:20 pm
- Contact:
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.3.1889 :
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.
-
- Posts: 31
- Joined: Sun Oct 13, 2013 1:20 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.3.1889 :
Okay thanks, and what if the stream is empty? Will it return 0? Or throw an Exception?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.3.1889 :
What happened when you tried it out?
BTW, the Collectors javadoc, clearly says, "If no elements are present, the result is 0.".
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.
-
- Posts: 31
- Joined: Sun Oct 13, 2013 1:20 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.3.1889 :
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!
Who is online
Users browsing this forum: Bing [Bot] and 56 guests