Used map and sum to calculate the sum of the squares of an IntStreams values. Reimplement stream

Question:

Used map and sum to calculate the sum of the squares of an IntStream’s values. Reimplement stream pipeline in Fig. 17.9 to replace map and sum with the following reduce, which receives a lambda that does not represent an associative operation:

.reduce((x, y) -> x + y * y)

Error-Prevention Tip 17.2 cautioned you that reduce’s argument must be an associative operation. Execute the reimplemented stream pipeline using a parallel stream. Does it produce the correct sum of the squares of the IntStream’s values?

Fig. 17.9 I // Fig. 17.9: IntStreamOperations.java // Demonstrating IntStream operations. 3 import java.util.Arrays; 4

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question

Java How To Program Late Objects Version

ISBN: 9780136123712

8th Edition

Authors: Paul Deitel, Deitel & Associates

Question Posted: