如何从 Java 中的自定义谓词列表创建谓词?
How to make a Predicate from a custom list of Predicates in Java?
我对编程还比较陌生,过去两天我一直在想如何制作一个由其他谓词的自定义列表构成的谓词。所以我想出了某种解决方案。下面是一个代码片段,应该会给你一个想法。因为我是在阅读各种文档的基础上编写的,所以我有两个问题:1/这是一个好的解决方案吗? 2/ 对于这个问题还有其他推荐的解决方案吗?
public class Tester {
private static ArrayList<Predicate<String>> testerList;
//some Predicates of type String here...
public static void addPredicate(Predicate<String> newPredicate) {
if (testerList == null)
{testerList = new ArrayList<Predicate<String>>();}
testerList.add(newPredicate);
}
public static Predicate<String> customTesters () {
return s -> testerList.stream().allMatch(t -> t.test(s));
}
}
Java Predicate 有一个很好的 AND 函数,returns new Predicate 是两个谓词的评估。你可以用这个把它们合二为一。
示例:
Predicate<String> a = str -> str != null;
Predicate<String> b = str -> str.length() != 0;
Predicate<String> c = a.and(b);
c.test("Str");
//stupid test but you see the idea :)
您可以有一个静态方法来接收许多谓词和 returns 您想要的谓词:
public static <T> Predicate<T> and(Predicate<T>... predicates) {
// TODO Handle case when argument is null or empty or has only one element
return s -> Arrays.stream(predicates).allMatch(t -> t.test(s));
}
变体:
public static <T> Predicate<T> and(Predicate<T>... predicates) {
// TODO Handle case when argument is null or empty or has only one element
return Arrays.stream(predicates).reduce(t -> true, Predicate::and);
}
我在这里使用 Stream.reduce
,它将身份和运算符作为参数。 Stream.reduce
将 Predicate::and
运算符应用于流的所有元素以生成结果谓词,并使用标识对流的第一个元素进行操作。这就是我使用 t -> true
作为标识的原因,否则结果谓词可能最终评估为 false
.
用法:
Predicate<String> predicate = and(s -> s.startsWith("a"), s -> s.length() > 4);
我对编程还比较陌生,过去两天我一直在想如何制作一个由其他谓词的自定义列表构成的谓词。所以我想出了某种解决方案。下面是一个代码片段,应该会给你一个想法。因为我是在阅读各种文档的基础上编写的,所以我有两个问题:1/这是一个好的解决方案吗? 2/ 对于这个问题还有其他推荐的解决方案吗?
public class Tester {
private static ArrayList<Predicate<String>> testerList;
//some Predicates of type String here...
public static void addPredicate(Predicate<String> newPredicate) {
if (testerList == null)
{testerList = new ArrayList<Predicate<String>>();}
testerList.add(newPredicate);
}
public static Predicate<String> customTesters () {
return s -> testerList.stream().allMatch(t -> t.test(s));
}
}
Java Predicate 有一个很好的 AND 函数,returns new Predicate 是两个谓词的评估。你可以用这个把它们合二为一。
示例:
Predicate<String> a = str -> str != null;
Predicate<String> b = str -> str.length() != 0;
Predicate<String> c = a.and(b);
c.test("Str");
//stupid test but you see the idea :)
您可以有一个静态方法来接收许多谓词和 returns 您想要的谓词:
public static <T> Predicate<T> and(Predicate<T>... predicates) {
// TODO Handle case when argument is null or empty or has only one element
return s -> Arrays.stream(predicates).allMatch(t -> t.test(s));
}
变体:
public static <T> Predicate<T> and(Predicate<T>... predicates) {
// TODO Handle case when argument is null or empty or has only one element
return Arrays.stream(predicates).reduce(t -> true, Predicate::and);
}
我在这里使用 Stream.reduce
,它将身份和运算符作为参数。 Stream.reduce
将 Predicate::and
运算符应用于流的所有元素以生成结果谓词,并使用标识对流的第一个元素进行操作。这就是我使用 t -> true
作为标识的原因,否则结果谓词可能最终评估为 false
.
用法:
Predicate<String> predicate = and(s -> s.startsWith("a"), s -> s.length() > 4);