Logging the result of filter while using java streams filter by predicate

前端 未结 2 1114
抹茶落季
抹茶落季 2021-01-12 02:37

The scenario is there are different types of filters I have created which filters the list of some objects based on the property of the object.

So for that I have

2条回答
  •  情深已故
    2021-01-12 03:06

    You can simply add brackets to your lambda expression and add the logging statement right before the validation :

    return event -> {
        // LOG.info(event.getName() + " was filtered...") or whatever you use for logging.
        return event.getPrizeCount() >= minimumNumOfPrizes;
    }
    

    Note that there exists a peek operation which is meant to be used mostly for logging on java streams :

    events.stream()
          .peek(event -> System.out.println("Filtering event" + event.getName()))
          .filter(isEligible())
          .collect(Collectors.toList());
    

    but that is not helping here, as you need to log in AbstractEventFilter implementations.

提交回复
热议问题