问题
I have a class with a String
and an int
field.
public class Data {
private String name;
private int value;
private Data(String name, int value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public int getValue() {
return value;
}
}
I have a List<Data>
and if I want to create a map (grouping) to know the Data object with maximum value for each name, I could do like,
Map<String, Optional<Data>> result = list.stream()
.collect(Collectors.groupingBy(Data::getName,
Collectors.maxBy(Comparator.comparing(Data::getValue))));
But, the RHS is wrapped in an Optional because of the contract of Collectors.maxBy
but I want a Map<String, Data>
.
One option I could think of it to use Collectors.collectingAndThen
, but I will have to create an unnecessary intermediate map.
list.stream()
.collect(Collectors.collectingAndThen(Collectors.groupingBy(Data::getName,
Collectors.maxBy(Comparator.comparing(Data::getValue))),
map -> map.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey,
e -> e.getValue().get()))));
Is there an idiomatic way to achieve this using streams?
回答1:
You can use BinaryOperator.maxBy
with Collectors.toMap
Map<String, Data> result = list.stream()
.collect(Collectors.toMap(Data::getName, Function.identity(),
BinaryOperator.maxBy(Comparator.comparing(Data::getValue))));
来源:https://stackoverflow.com/questions/63159338/unwrap-optional-when-using-collectors-maxby-with-groupingby