I have the following class:
public class Offer {
private final OfferType type;
private final BigDecimal price;
// constructor, getters and setters
}
and enum type:
public enum OfferType {
STANDARD, BONUS;
}
My use case is that having a list of offers as an input, I want to filter out all the standard ones except the cheapest one. So for the following input data
List<Offer> offers = Arrays.asList(new Offer(OfferType.STANDARD, BigDecimal.valueOf(10.0)),
new Offer(OfferType.STANDARD, BigDecimal.valueOf(20.0)),
new Offer(OfferType.STANDARD, BigDecimal.valueOf(30.0)),
new Offer(OfferType.BONUS, BigDecimal.valueOf(5.0)),
new Offer(OfferType.BONUS, BigDecimal.valueOf(5.0)));
I expect the following result
[Offer [type=STANDARD, price=10.0], Offer [type=BONUS, price=5.0], Offer [type=BONUS, price=5.0]]
Is there a single-line statement (using streams or any third-party library) that allows for doing that?