0

whats the 0 for ?

whats the 0 for ? code: import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) { List <Integer> myList = Arrays.asList(1 , 5 ,4 ,4 ,3 ,3); int sum = myList.stream().reduce(0 , (num1 , num2) -> num1 + num2); //the 0 in this line System.out.println(sum); } }

6th Aug 2020, 4:04 PM
Yahel
Yahel - avatar
3 Answers
+ 4
Not good with Java, but usually, a reduce function uses something they call an accumulator, the values in the list will be added into the accumulator. Please cmiiw about this, kinda not sure 🙏
6th Aug 2020, 4:11 PM
Ipang
+ 3
yahel Ipang is correct. The first argument passed, 0, is the initial value for the reduce function where the second argument passed is the accumulator function used to perform the reduction operation on the values of type T from the stream, adding them together in this case.
6th Aug 2020, 10:57 PM
ChaoticDawg
ChaoticDawg - avatar
0
đŸ‘đŸ»đŸ‘đŸ»đŸ‘đŸ»thanks!
6th Aug 2020, 4:52 PM
Yahel
Yahel - avatar