Type a reduce that sums a stream of integers
Type a reduce that sums a stream of integers
Answer
int sum = Stream.of(1, 2, 3, 4, 5) .reduce(0, Integer::sum);
reduce(identity, accumulator): starts with 0 and applies Integer::sum to fold each element in. Returns an int directly when an identity is provided.