Java streams' filters use Predicates to allow or deny an element through the stream.
Thanks to .compose or .andThen the filter can be restricted.
Yet what if I wanted to have it more permissive according to some flags?
For example, I might want to have isAppleAllowed and isPearAllowed parametrically set by the user.
The use of .filter(f -> isAppleAllowed || isPearAllowed) doesn't work, because if either is True, both are allowed.
What can I use to extend the filter more permissively?
Thanks in advance for any help.
I had to change this answer, because what you're looking for was different than what I originally answered. Using two separate calls to
Stream#filter, as I previously suggested, wouldn't make sense, as bothPredicates would need to rely on each other in that case.Something like this should work though, as it checks the attributes of the element before attempting to filter it. Assuming the element can be either an apple or a pear, then it will be filtered through if
isAppleAllowedorisPearAllowedistrue, respectively.