mr.*_*r.M 8 java lambda predicates
我有以下搜索条件的地图:
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);
}
Run Code Online (Sandbox Code Playgroud)
我通过以下方式使用此地图:
pl.stream()
.filter(search.getCriteria("allPilots"))
.forEach(p -> {
p.printl(p.getPrintStyle("westernNameAgePhone"));
});
Run Code Online (Sandbox Code Playgroud)
我想知道,如何将一些参数传递到谓词的映射中?
即我想通过字符串缩写从地图中获取谓词,并将参数插入从地图谓词中取出的参数.
pl.stream()
.filter(search.getCriteria("allPilots",45, 56))
.forEach(p -> {
p.printl(p.getPrintStyle("westernNameAgePhone"));
});
Run Code Online (Sandbox Code Playgroud)
这里是链接从我用Google搜索这个地图谓语方法.
JB *_*zet 12
看来你想要的不是在谓词中存储谓词.你想要的是能够在地图中存储能够Predicate<Person>从int参数创建的东西.所以你想要的是这样的:
Map<String, IntFunction<Predicate<Person>>> searchMap = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
你会这样填写:
searchMap.put("allPilots", maxAge ->
(p -> p.getAge() >= 23
&& p.getAge() <= maxAge));
Run Code Online (Sandbox Code Playgroud)
你会像这样使用它:
Predicate<Person> allPilotsAgedLessThan45 =
searchMap.get("allPilots").apply(45);
Run Code Online (Sandbox Code Playgroud)
当然,如果您创建自己的功能界面会更清楚:
@FunctionalInterface
public MaxAgePersonPredicateFactory {
Predicate<Person> limitToMaxAge(int maxAge);
}
Run Code Online (Sandbox Code Playgroud)
您仍然会以相同的方式填充地图,但在使用时您将获得稍微更易读的代码:
Predicate<Person> allPilotsAgedLessThan45 =
searchMap.get("allPilots").limitToMaxAge(45);
Run Code Online (Sandbox Code Playgroud)
据我了解,您想要做的是覆盖 named 中的一些参数Predicate,但我认为这会导致混淆。如果我要求确定某人是否有资格成为飞行员的谓词,我不想在我调用它时担心知道要求是什么。
使用 a 动态生成谓词Map会很棘手 - 我认为最好的方法是使用 a Map<String, PredicateFactory>,其中 thePredicateFactory是某种复杂的类,可以在给定一些参数的情况下生成谓词:
Map<String, PredicateFactory<T>> predicates = new HashMap<>();
public Predicate<T> get(String name, Object... params) {
return predicates.get(name).apply(params);
}
Run Code Online (Sandbox Code Playgroud)
在我看来,生成“动态”谓词的更好方法是使用静态方法:
public class PredicatesQuestion {
public static void main(String[] args) {
List<Person> people = Arrays.asList(new Person(20, Gender.MALE),
new Person(45, Gender.FEMALE), new Person(50, Gender.MALE),
new Person(65, Gender.MALE));
people.stream()
.filter(personIsBetweenAges(16, 25))
.forEach(person -> {
System.out.println(person.getAge() + ", " + person.getGender());
});
}
private static Predicate<Person> personIsMale() {
return person -> person.getGender() == Gender.MALE;
}
private static Predicate<Person> personIsBetweenAges(int lower, int upper) {
return personIsAtLeast(lower).and(personIsYoungerThan(upper));
}
private static Predicate<Person> personIsAtLeast(int age) {
return person -> person.getAge() >= age;
}
private static Predicate<Person> personIsYoungerThan(int age) {
return person -> person.getAge() < age;
}
}
Run Code Online (Sandbox Code Playgroud)
然后根据需要创建描述性谓词是微不足道的:
private static Predicate<Person> personIsOfDrivingAge() {
return personIsAtLeast(17);
}
private static Predicate<Person> couldBePilot() {
return personIsBetweenAges(23, 65).and(personIsMale());
}
Run Code Online (Sandbox Code Playgroud)
你试图通过覆盖一些参数来实现的那种事情通过一些组合仍然很清楚:
people.stream()
.filter(couldBePilot().and(personIsBetweenAges(45, 56)))
.forEach(person -> {
System.out.println(person.getAge() + ", " + person.getGender());
});
Run Code Online (Sandbox Code Playgroud)