使用
collectingAndThen:
List beansByDomain = beans.stream()
.collect(groupingBy( (ObjectInstance oi) -> oi.getObjectName().getDomain(),
mapping((ObjectInstance oi) -> oi.getObjectName().getCanonicalKeyPropertyListString(),
collectingAndThen(toList(), (l -> l.stream().sorted().collect(toList()))) )));
您可以提取收集器以使代码更具可读性:
public static Collector> toSortedList() {
return Collectors.collectingAndThen(Collectors.toList(),
l -> l.stream().sorted().collect(toList()));
}
List beansByDomain = beans.stream()
.collect(groupingBy( (ObjectInstance oi) -> oi.getObjectName().getDomain(),
mapping((ObjectInstance oi) -> oi.getObjectName().getCanonicalKeyPropertyListString(),
toSortedList())));