public Optional < T > reduce ( final BiFunction < T , ? super T , T > accumulator ) { boolean found = false ; T result = null ; while ( iterator . hasNext ( ) ) { if ( ! found ) { result = iterator . next ( ) ; found = true ; } else { result = accumulator . apply ( result , iterator . next ( ) ) ; } } return found ? Optional . of ( result ) : Optional . < T > empty ( ) ; }
Performs a reduction on the elements of the stream using an accumulation function and returns an Optional describing the reduced value if any .