Is there a way to let the 'reduction' of the reduce() method of Stream be optional?
I want to iterate over a list of Periods and join the periods that overlap and maintain both periods if they don't overlap:
interface Period {
boolean overlaps(Period other);
}
List<Period> periods = new ArrayList<>();
periods.stream().reduce(new BinaryOperator<Period>() {
@Override
public Period apply(Period period, Period period2) {
if (period.overlaps(period2)){
// join period and period2 into period.
}else{
"return both"
// don't reduce and maintain period and period2 in the list.
}
return null;
}
});