The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

tcadams289 (2) [Avatar] Offline
#1
Hi Guys,

Book looks great, really enjoying lambdas.

Small suggestion right after Figure 4.12 change these reduce statements to use a different starting value. For min, zero will be returned if you have all positive numbers in the stream.

Maybe:
int max = num1.stream().reduce(Integer.MIN_VALUE, Integer::max);
int min = num1.stream().reduce(Integer.MAX_VALUE, Integer::min);

would be a better example?

Thanks
Tom
undjask (13) [Avatar] Offline
#2
Re: Suggestion for min/max reduce functions
My feeling is, the better example might be not using the reduce-function with
the identity element.
Instead you should use the identityless version which will result in some type of
Optional depending on the type of the stream. If your target stream contains
zero elements, Optional.isPresent()==false otherwise you will get a suitable
result out of the Optional.
raoul-gabriel.urma (37) [Avatar] Offline
#3
Re: Suggestion for min/max reduce functions
Thanks for reading our book and for your feedback!

Yes a better version would be:

Optional<Integer> maxOpt = numbers.stream().reduce(Integer::max);
int max = maxOpt.orElse(someIntValue);

I will add some comments on this for the next iteration smilie.