I have the following map of the search criteria:
private final Map<String, Predicate> searchMap = new HashMap<>();
private void initSearchMap() {
Predicate<Person> allDrivers = p -> p.getAge() >= 16;
Predicate<Person> allDraftees = p -> p.getAge() >= 18
&& p.getAge() <= 25
&& p.getGender() == Gender.MALE;
Predicate<Person> allPilots = p -> p.getAge() >= 23
&& p.getAge() <=65;
searchMap.put("allDrivers", allDrivers);
searchMap.put("allDraftees", allDraftees);
searchMap.put("allPilots", allPilots);
}
I am using this map in the following way:
pl.stream()
.filter(search.getCriteria("allPilots"))
.forEach(p -> {
p.printl(p.getPrintStyle("westernNameAgePhone"));
});
I would like to know, how can I pass some parameters into the map of predicates?
I.e. I would like to get predicate from a map by its string abbreviation and insert a parameter into the taken out from a map predicate.
pl.stream()
.filter(search.getCriteria("allPilots",45, 56))
.forEach(p -> {
p.printl(p.getPrintStyle("westernNameAgePhone"));
});
Here is the link from I googled out this map-predicate approach.
It seems that what you want is not to store a predicate in a Map. What you want is to be able to store something in a map that is able to create a
Predicate<Person>
from anint
parameter. So what you want is something like this:You would fill it that way:
And you would use it like this:
Of course, it would be clearer if you created your own functional interface:
You would still fill the map the same way, but you would then have slightly more readable code when using it: