JavaIntermediate#streams

What is the difference between Stream.reduce() with and without an identity value?

reduce() with an identity always returns a value (the identity if the stream is empty). reduce() without an identity returns an Optional, since there's no default to fall back on for an empty stream.

Example
int sum = Stream.of(1,2,3).reduce(0, Integer::sum); // 6, has identity
Optional<Integer> max = Stream.of(1,2,3).reduce(Integer::max); // Optional[3]

Related Questions

1
JavaAdvanced#streams

What is a parallel stream and when should you avoid it?

Open
2
JavaAdvanced#memory#gc

What are the different generations in the JVM heap?

Open
3
JavaAdvanced#memory#gc

What causes a memory leak in Java despite having garbage collection?

Open