Apply stream to filter out all elements satisfying the condition except one

Viewed 954

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?

4 Answers

Not with a single stream operation though:

List<Offer> some = offers.stream()
                         .filter(x -> x.getType() != OfferType.STANDARD)
                         .collect(Collectors.toCollection(ArrayList::new));

offers.stream()
      .filter(x -> x.getType() == OfferType.STANDARD)
      .min(Comparator.comparing(Offer::getPrice))
      .ifPresent(some::add);

If you find yourself doing this a lot, may be spin a custom collector:

 public static Collector<Offer, ?, List<Offer>> minCollector() {
    class Acc {

        Offer min = null;
        List<Offer> result = new ArrayList<>();

        void add(Offer offer) {
            if (offer.getType() == OfferType.STANDARD) {
                if (min == null) {
                    min = offer;
                } else {
                    min = offer.getPrice()
                               .compareTo(min.getPrice()) > 0 ? min : offer;
                }
            } else {
                result.add(offer);
            }
        }

        Acc combine(Acc another) {
            this.min = reduceMin(this.min, another.min);
            result.addAll(another.result);
            return this;
        }

        List<Offer> finisher() {
            result.add(min);
            return result;
        }

        private Offer reduceMin(Offer left, Offer right) {
            return Collections.min(Arrays.asList(left, right),
                                   Comparator.nullsLast(Comparator.comparing(Offer::getPrice)));
        }
    }

    return Collector.of(Acc::new, Acc::add, Acc::combine, Acc::finisher);
}

And usage would be:

List<Offer> result = offers.stream()
                           .collect(minCollector());

Is there a single-line statement (using streams or any third-party library) that allows for doing that?

Do things in two times, it would be more readable.

1) Compute the cheapest price for the Standard type offers :

Optional<Offer> minPriceOffer = 
offers.stream()
      .filter(o -> o.getType() == OfferType.STANDARD)
      .min(Comparator.comparing(Offer::getPrice));

2) Exclude Standard offers with this price in the collected list :

List<Offer> offersFiltered = 
offers.stream()
      .filter(o -> {  
               if (o.getType() == OfferType.STANDARD                         
                    && !o.getPrice().equals(minPriceOffer.get().getPrice())) 
                  return false;
               // else
               return true;
             }
       )
      .collect(toList();

Here are two streams that:

  1. Group by offer type
  2. converts each group's offers to a stream
  3. selects the standard offers, sorts them, and limits to 1 element (min by price)
  4. Merges the two streams

The code looks like this:

List<Offer> result = offers.stream()
        .collect(Collectors.groupingBy(Offer::getType))
        .entrySet()
        .stream()
        .flatMap(entry -> entry.getKey() == OfferType.STANDARD ? 
                            entry.getValue().stream()
                            .sorted(Comparator.comparing(Offer::getPrice))
                            .limit(1)
                          :  entry.getValue().stream())
        .collect(Collectors.toList());
List<Offer> result = offers.stream().filter(e -> OfferType.BONUS == e.getType()).collect(toList());
offers.stream().filter(e -> OfferType.STANDARD == e.getType()).findAny().ifPresent(result::add);
Related