仅在满足某些条件时应用减免

时间:2018-12-04 18:37:47

标签: java java-8 java-stream reduce

是否有一种方法可以让Stream的reduce()方法的“减少”为可选?

我要遍历期间列表,并加入重叠的期间,如果两个期间不重叠,则维持两个期间:

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;
    }
});

2 个答案:

答案 0 :(得分:6)

我认为您不能仅通过流轻松地做到这一点。使用Guava ranges,您可以执行以下操作:

periods.stream()
        .map(p -> Range.closedOpen(p.getStart(), p.getEnd()))
        .collect(TreeRangeSet::<Integer>create, RangeSet::add, RangeSet::addAll)
        .asRanges()
        .stream()
        .map(r -> new PeriodImpl(r.lowerEndpoint(), r.upperEndpoint()))
        .collect(Collectors.toList());

这假定具有如下所示的类结构,但是您可以根据需要进行调整:

interface Period {
    int getStart();
    int getEnd();
}

class PeriodImpl implements Period {
    PeriodImpl(int start, int end) {
        //...
    }
    //...
}

答案 1 :(得分:6)

尝试StreamEx中提供的collapse

// Here I use Range type provided in Google Guava for test.
List<Range<Integer>> list = Arrays.asList(Range.openClosed(1, 3), Range.openClosed(2, 4), Range.closed(5, 5));

StreamEx.of(list)
    .collapse(Range::isConnected, Range::span)
    .forEach(System.out::println);
// (1..4]
// [5..5]