我最初尝试的
Map<String, List<Date>> subscriberCreatedDateSet = data.stream().collect(Collectors
.groupingBy(SubscriberActivityAggData::getSubscriberAddress
, Collectors.mapping(SubscriberActivityAggData::getEventTime, Collectors.toList())));
然后Map<String, Date> subscriberCreatedDate = subscriberCreatedDateSet.entrySet().stream()
.map(item->Collections.min(item.getValue()))
.collect(Collectors.mapping());
这是不完整的,因为我找不到正确的方法。is there a way to do it properly? or should I loops and o it
最佳答案
您可以使用toMap
而不是groupingBy
在单个管道中简单地执行以下操作:
Map<String, Date> subscriberCreatedDate = data.stream()
.collect(Collectors.toMap(SubscriberActivityAggData::getSubscriberAddress,
SubscriberActivityAggData::getEventTime,
BinaryOperator.minBy(Comparator.naturalOrder())));
注意-这也节省了生成的Optional
中Map
包装值的开销。