簡體   English   中英

Lambda表達式內的組合謂詞

[英]Combining Predicates within Lambda Expression

這是一個兩部分的問題。

我有一個布爾字段和幾個String []字段,我需要評估每個謂詞。

以下代碼獲取每個設備的接口。 然后,我想基於謂詞評估那些接口。

  1. 有更有效的方法嗎?
  2. .filter(predicates.stream()。reduce(Predicate :: or)給出此錯誤: 不兼容的類型:無效的方法參考 我不確定如何解決。

      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有一個名為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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM