The preceding section covered value accumulation, whereas the reduce operation revolves around value consolidation. The reduce method makes it possible to aggregate the complete data stream into a single value. This is depicted as follows:
Suppose that we want to generate a sum of Fibonacci numbers, as follows:
fibonacciGenerator.take(10).reduce((x,y) -> x+y).subscribe(t -> { System.out.println(t); });
In the preceding code, we did the following things:
- The take operator selected the first 10 elements for the stream.
- The reduce operator took a Bifunction of the long type. The lambda expression returns the sum of the long ...