【发布时间】:2015-10-27 08:18:53
【问题描述】:
我最近开始学习流 API。
我正在尝试从以下代码中删除重复项。
是否可以使用基于Predicate 或条件的合并功能?
(我也在考虑使用来自Collectors 的partitioningBy 或groupingBy)
BinaryOperator<LocalDateTime> latestDate = (p, a) -> a.isAfter(p) ? a : p;
BinaryOperator<LocalDateTime> earliestDate = (p, a) -> a.isBefore(p) ? a : p;
Map<Status,LocalDateTime> latest = history.stream()
.filter(isNewOrOpen.negate())
.collect(toMap(History::getStatus,
History::getChangedtetme,
latestDate));
Map<Status,LocalDateTime> earliest = history.stream()
.filter(isNewOrOpen)
.collect(toMap(History::getStatus,
History::getChangedtetme,
earliestDate));
latest.putAll(earliest);
【问题讨论】:
-
所以你有两个单独的列表,即
history和historyGroup? -
@TagirValeev 他们不应该,这是我的错字。改变了。谢谢!
标签: java merge java-8 java-stream