我对编程比较陌生,过去两天我一直想知道如何制作一个由其他Predicates自定义列表组成的谓词.所以我想出了一些解决方案.下面是一个代码片段,可以给你一个想法.因为我是基于单独阅读各种文档而编写的,所以我有两个问题: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));
}
}
Run Code Online (Sandbox Code Playgroud)