javajava-streampredicate

Chaining predicates into one using the Stream API


I have a list of predicates (List<Predicate<String>>) which I want to chain into one using or methods. How can I use the Stream API to achieve that?


Solution

  • You can use reduce to join them all together with or, yielding a single Predicate<String>. Like this:

    List<Predicate<String>> predicates = getPredicates();
    
    Predicate<String> p = predicates.stream()
        .reduce(s -> false, Predicate::or);