I was wondering if there is any way to achieve the following within a single iteration over the array. Simply to have two different results out of stream.
double sum = Arrays.stream(doubles).sum();
double sumOfSquares = Arrays.stream(doubles).map(d -> d * d).sum();
The correct approach would be to use . map() which, like the name says, maps one value to another. In your case the first operation you want to do is to map a Person to a JSONObject. The second operation is a reducer function where you want to reduce all JSONObjects to one JSONArray object.
Using IntStream. This method takes a mapper as a parameter, which it uses to do the conversion, then we can call the sum() method to calculate the sum of the stream's elements. In the same fashion, we can use the mapToLong() and mapToDouble() methods to calculate the sums of longs and doubles, respectively.
A stream should be operated on (invoking an intermediate or terminal stream operation) only once. A stream implementation may throw IllegalStateException if it detects that the stream is being reused. So the answer is no, streams are not meant to be reused.
stream(). mapToInt( Integer::intValue ). sum() .
Well, you could with a custom collector, for instance:
double[] res =
Arrays.stream(doubles)
.collect(() -> new double[2],
(arr, e) -> {arr[0]+=e; arr[1]+=e*e;},
(arr1, arr2) -> {arr1[0]+=arr2[0]; arr1[1]+=arr2[1];});
double sum = res[0];
double sumOfSquares = res[1];
but you don't gain much readability in my opinion, so I would stick with the multiple passes solution (or maybe just use a for-loop in this case).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With