【发布时间】:2022-01-13 17:40:49
【问题描述】:
我的实体列表如下所示:
class History {
public String code;
public Integer version;
public LocalDateTime creationDate;
}
对于每个具有最大版本的代码,我想获取creationDate。我是这样做的,但我认为它可以更容易地完成......
Map<String, History> historyMaxVersion = histories.stream()
.collect(Collectors.toMap(History::getCode, Function.identity(),
BinaryOperator.maxBy(Comparator.comparing(History::getVersion))));
Map<String, LocalDateTime> historiesLastUpdatedDates = historyMaxVersion.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue()
.getCreationDate()));
【问题讨论】:
标签: java java-stream java-11