2016-06-30 87 views
0

我正在尋找用Java lambda表達式替換下面的代碼。在下面的例子中,如果找到匹配,我需要出來循環,並且爲了斷言將boolean設置爲true條件用Java lambda表達式替換自定義列表搜索

 Long teamId; 
    boolean matchFound = false; 
    List<WorkflowSubscriptions> workflowSubscriptionsList = fetchSubscriptions(); 
    for (WorkflowSubscriptions workflowSubscriptions : workflowSubscriptionsList) { 
     for (WorkflowCompositeInfo workflowCompositeInfo : workflowSubscriptions.getWorkflowCompositeInfo()) { 
      if (workflowCompositeInfo.getId() > 0 && workflowCompositeId == workflowCompositeId.intValue()) { 
       teamId = Long.valueOf(workflowSubscriptions.getId()); 
       matchFound = true; 
      } 
     } 
    } 
    if (!matchFound) { 
     throw new CustomParameterizedException("Workflow does not have valid subscriptions"); 
    } 
+0

你有沒有問之前嘗試過嗎?你有什麼想法? –

+0

我準備的下面的代碼是爲列表的一個級別。現在我在列表中列出StreamSupport.stream(可選.ofNullable(rwpSubscriptions).orElse(Collections.emptyList())) .filter(s-> s.getCompositeId()。equals(workflowCompositeId)) .findFirst () .map(s-> s.getTeamId()) .orElseThrow(() - > new CustomParameterizedException(「Workflow does not have a valid subscription」)) – Balaji

+0

@YassinHajaj發帖供您參考 – Balaji

回答

1

試試這個:

fetchSubscriptions().stream() 
    .filter(p -> w.getId() > 0 && workflowCompositeId == w.intValue()) 
    .limit(1) 
    .forEach(w -> {teamId = Long.valueOf(w); matchFound = true;}); 

UPDATE

爲了避免有關修改錯誤最後一個變量。你可以簡單地創建一個值類:

public class MyValue { 
    public boolean matchFound; 
    public Long teamId; 
} 

然後:

final MyValue value = new MyValue(); 

fetchSubscriptions().stream() 
    .filter(p -> w.getId() > 0 && workflowCompositeId == w.intValue()) 
    .limit(1) 
    .forEach(w -> {value.teamId = Long.valueOf(w); value.matchFound = true;} ); 

// Here, find your data in value.teamId and value.matchFound 
+0

非常感謝,讓我試試然後回來。最初我有一個級別的列表,但現在我已經發布了我有兩個StreamSupport.stream(Optional.ofNullable(rwpSubscriptions).orElse(Collections.emptyList())) .filter(s-> s.getCompositeId()。equals (工作流合作伙伴) .findFirst() .map(s-> s.getTeamId()) .orElseThrow(() - > new CustomParameterizedException(「Workflow does not have valid subscription」)) – Balaji

+0

此外,我正在使用Java 8只是爲了發展。我需要使用Java 8的後端版本並實現相同的功能。只是想知道上述內容是否可以在Java 8的後端版本上運行? – Balaji

+0

這真的取決於backport的實現。但是沒有理由不應該。它符合API – mprivat