I'm trying to compare an object against a list of objects to know if this particular object is on the list, however there are some rules
- I don't want to override any existing methods
- I don't want to use custom comparators
- Solution should use already existing predicates
Code below:
List<String> PREDEFINED_CODES = Arrays.asList(PredefinedCodes.1, PredefinedCodes.2, PredefinedEventSetupCodes.3, PredefinedEventSetupCodes.4);
private BiPredicate<Event, String> isEventOfCode(){
return (event, code) -> code.equals(event.getCode());
}
private Predicate<Event> isEvendPredefined(){
return event -> PREDEFINED_CODES.stream().findAny(code -> isEventOfCode().test(event,code)).isPresent();
}
Note: This is simplified version of code to show my problem
Now the problem, I'm having this code -> isEventOfCode().test(event,code) in this lambda I can not test values against isEventOfCode predicate because Wrong 2nd argument type. Found: '<lambda parameter>', required: 'java.lang.String' although I know that PREDEFINED_CODES is a list of string, therefore code is of type String
Is there some workaround for calling the isEventOfCode() predicate with a parameter from lambda expression? I can not modify type of isEventOfCode() since its used in other parts of code.
BiPredicate<Event, String>returning method have? If you want to encapsulate thecode.equals(event.getCode())operation, you can use an ordinary method.