我有以下方法:
private List<Residence> getHighestPriceResidenceDistinct(List<Residence> residences, Sort.Direction direction, int numOfResidences) {
return residences.stream()
.collect(
Collectors.groupingBy(
SlotWinTransaction::getResidenceId,
Collectors.maxBy(Comparator.comparing(Residence::getPrice))
)
)
.values()
.stream()
.filter(Optional::isPresent)
.map(Optional::get)
.sorted(Comparator.comparing(Residence::getPrice).reversed())
.limit(numOfResidences)
.collect(Collectors.toList());
}
It returns n
number of Residence
objects - in a list. A Residence
object has fields price::BigDecimal
, and id::String
. The above method, requests the numOfResidences
unique, most expensive Residence objects, and -due to the addition of reversed()
- lists them starting from the most expensive one. The getHighestPriceResidenceDistinct method also has the Sort.Direction
parameter. I would like to add a lambda expression
, that defines if the reversed()
will be used based on the value of the direction parameter.
这是你想要的?