简体   繁体   English

如何组合List中的所有谓词 <Predicate<MyClass> &gt;

[英]How to combine all predicates from List<Predicate<MyClass>>

I have one problem and I'm sure you help me with my wrinkle. 我有一个问题,我相信你帮我解决了皱纹问题。

I have 我有

List<Predicate<TaskFx>> predicates

and I want to use these predicates in 我想在这里使用这些谓词

taskFxList.stream().filter(predicates).collect(Collectors.toList());

as a one predicate merged like: 作为一个谓词合并如下:

predicate1.and(predicate2).and...

I have a table (13 columns) with some results (in JavaFx) and 6 fields to search in this table by the values from these fields. 我有一个表(13列),其中包含一些结果(在JavaFx中)和6个字段,可以通过这些字段中的值在此表中搜索。 I can enter for example values only to 3 fields so my 我只能输入3个字段的值,所以我的

predicates.size() = 3;

The question is how best to prepare dynamically one 问题是如何最好地动态准备一个

Predicate<TaskFx> predicate

consisting all predicates merged by x.and(y).and(z) 包含由x.and(y)。和(z)合并的所有谓词

Thank you very much for your help! 非常感谢您的帮助!

You can stream and reduce them like this: 您可以像这样流式传输和reduce它们:

Predicate<TaskFx> predicate = predicates.stream()
        .reduce(x -> true, Predicate::and);

Alternatively you could just process them like this: 或者你可以像这样处理它们:

List<TaskFx> resultSet = stringList.stream()
                                   .filter(e -> predicates.stream().allMatch(x -> x.test(e)))
                                   .collect(Collectors.toList());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM