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

Ishant Gaurav :

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?

Ousmane D. :

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;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=37147&siteId=1