Search code examples
javajava-8predicatefunctional-interface

Create custom Predicate with Set<String> and String as parameter


I have a String as "ishant" and a Set<String> as ["Ishant", "Gaurav", "sdnj"] . I need to write the Predicate for this. I have tried as below code, but it is not working

Predicate<Set<String>,String> checkIfCurrencyPresent = (currencyList,currency) -> currencyList.contains(currency);

How can I create a Predicate which will take Set<String> and String as a parameter and can give the result?


Solution

  • A Predicate<T> which you're currently using represents a predicate (boolean-valued function) of one argument.

    You're looking for a BiPredicate<T,U> which essentially represents a predicate (boolean-valued function) of two arguments.

    BiPredicate<Set<String>,String>  checkIfCurrencyPresent = (set,currency) -> set.contains(currency);
    

    or with method reference:

    BiPredicate<Set<String>,String> checkIfCurrencyPresent = Set::contains;