小编Jan*_*kop的帖子

如何从Java的自定义谓词列表中创建谓词?

我对编程比较陌生,过去两天我一直想知道如何制作一个由其他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)

java lambda predicate java-8 java-stream

3
推荐指数
1
解决办法
2356
查看次数

标签 统计

java ×1

java-8 ×1

java-stream ×1

lambda ×1

predicate ×1