I was working on a similar problem of predicate chaining and came up with the following
public static <T> Predicate<T> chain (Function<T,Predicate<T>> mapFn, T[]args) {
return Arrays.asList(args)
.stream()
.map(x->mapFn.apply(x))
.reduce(p->false, Predicate::or);
}
The first parameter to chain is a lambda (Function) that returns a lambda (Predicate) so it needs a couple of arrows
public static void yourExample() {
String[] filterVals = { "1", "2" };
Arrays.asList("1","2","3")
.stream()
.filter(chain(x-> (y-> y.equals(x)), filterVals))
.count();
}
For comparison, here is what I was trying to achieve...
public static void myExample() {
String[] suffixes = { ".png", ".bmp" };
Predicate<String> p = chain (x-> y-> y.endsWith(x), suffixes);
File[] graphics = new File("D:/TEMP").listFiles((dir,name)->p.test(name));
Arrays.asList(graphics).forEach(System.out::println);
}