[英]Combining Predicates within Lambda Expression
This is a two part question. 这是一个两部分的问题。
I have a boolean field and several String[] fields and I need to evaluate predicates for each one. 我有一个布尔字段和几个String []字段,我需要评估每个谓词。
The following code gets interfaces for each device. 以下代码获取每个设备的接口。 I then want to evaluate those interfaces based on the predicates.
然后,我想基于谓词评估那些接口。
.filter(predicates.stream().reduce(Predicate::or) gives this error: .filter(predicates.stream()。reduce(Predicate :: or)给出此错误:
that I'm unsure how to resolve.
我不确定如何解决。
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); } });
Stream has a function called anyMatch that can clean up your filter a bit. Stream有一个名为anyMatch的功能,可以稍微清理一下过滤器。
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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.