【发布时间】:2021-05-14 07:34:48
【问题描述】:
我正在编写下面的代码,但它似乎很尴尬。能不能写得更好?最后,代码必须创建一个谓词,该谓词由带有 OR 条件的谓词列表组成。
第一个谓词,它应该被附加到谓词列表中
Predicate<SomeDto> p1 = t -> StringUtils.isBlank(t.getColor());
来自 someListOfColors 的谓词列表
List<Predicate<SomeDto>> collect = Arrays.stream(somelistOfColors)
.map(ta -> {
Predicate<SomeDto> p2 = t -> t.getColor().equals(ta.getCode());
return p2;
}).collect(Collectors.toList());
所有谓词都应该用OR条件连接
or(p1, collect.toArray(new Predicate[collect.size()]));
OR条件的方法
private Predicate<SomeDto> or(Predicate<SomeDto>... predicates) {
return s -> Arrays.stream(predicates).anyMatch(t -> t.test(s));
}
【问题讨论】:
标签: java java-stream predicate