Java 在Lambda表达式中组合谓词
这是一个由两部分组成的问题 我有一个布尔字段和几个String[]字段,需要为每个字段计算谓词 下面的代码获取每个设备的接口。然后我想根据谓词评估这些接口Java 在Lambda表达式中组合谓词,java,lambda,predicate,Java,Lambda,Predicate,这是一个由两部分组成的问题 我有一个布尔字段和几个String[]字段,需要为每个字段计算谓词 下面的代码获取每个设备的接口。然后我想根据谓词评估这些接口 有没有更有效的方法 .filter(谓词.stream().reduce(谓词::或)出现以下错误: 我不知道如何解决 parentReference.getDevices().stream().parallel().filter(ASA_PREDICATE).forEach((device) -> { List<
parentReference.getDevices().stream().parallel().filter(ASA_PREDICATE).forEach((device) -> {
List<Predicate<? super TufinInterface>> predicates = new ArrayList();
if (iName != null) {
Predicate< ? super TufinInterface> iNameFilter = myInterface -> Arrays.stream(iName)
.allMatch(input -> myInterface.getName().toLowerCase().contains(input.toLowerCase()));
predicates.add(iNameFilter);
}
if (ipLow != null) {
Predicate< ? super TufinInterface> ipLowFilter = myInterface -> Arrays.stream(ipLow)
.allMatch(input -> myInterface.getName().toLowerCase().contains(input.toLowerCase()));
predicates.add(ipLowFilter);
}
if (ip != null) {
Predicate< ? super TufinInterface> ipFilter = myInterface -> Arrays.stream(ip)
.allMatch(input -> myInterface.getName().toLowerCase().contains(input.toLowerCase()));
predicates.add(ipFilter);
}
if (zone != null) {
Predicate< ? super TufinInterface> zoneFilter = myInterface -> Arrays.stream(zone)
.allMatch(input -> myInterface.getName().toLowerCase().contains(input.toLowerCase()));
predicates.add(zoneFilter);
}
try {
ArrayList<TufinInterface> tufinInterfaces = Tufin.GET_INTERFACES(parentReference.user, parentReference.password, parentReference.hostName, device)
.stream()
.filter(predicates.stream().reduce(Predicate::or)
.orElse(t->true)).parallel().collect(Collectors.toCollection(ArrayList<TufinInterface>::new));
interfaces.addAll(tufinInterfaces);
} catch (IOException | NoSuchAlgorithmException | KeyManagementException | JSONException | Tufin.IncompatibleDeviceException ex) {
Logger.getLogger(InterfaceCommand.class.getName()).log(Level.SEVERE, null, ex);
}
});
parentReference.getDevices().stream().parallel().filter(ASA_谓词).forEach((设备)->{
ListStream有一个名为的函数,可以稍微清理一下过滤器
ArrayList<TufinInterface> tufinInterfaces = Tufin.GET_INTERFACES(parentReference.user, parentReference.password, parentReference.hostName, device)
.stream()
.filter(s -> predicates.stream().anyMatch(pred -> pred.test(s)))
.collect(Collectors.toList());
ArrayList tufinInterfaces=Tufin.GET_接口(parentReference.user、parentReference.password、parentReference.hostName、设备)
.stream()
.filter->predicates.stream().anyMatch(pred->pred.test)))
.collect(Collectors.toList());
我试图使用reduce。是否首选任何匹配项?