Java LongStream to sum int array elements -


because many integers can overflow when summed, needed long stream job wont accept int array. how can convert each element @ time of streaming instead of using long array?

// arr int[] longstream s = arrays.stream( arr); // error result = s.reduce(0, long::sum);  

edit: appears integer stream turned long 1 using method in tagir valeev's answer.

longstream aslongstream();

use intstream.aslongstream() method:

longstream s = arrays.stream(arr).aslongstream(); 

by way s.reduce(0, long::sum) longer alternative simple sum() method (which internally same):

long result = arrays.stream(arr).aslongstream().sum(); 

Comments