2017-06-29 39 views
1

名單的結果,我有期貨如何獲得期貨

List<Future<Boolean>> futures = service.invokeAll(Arrays.asList(callable1, callable2)); 

我需要的是一種方式來獲得結果

的列表,你可以提供在Java的解決方案的名單?

whenAll()...

+1

不,我們在這裏幫助您,但您需要首先提供您的嘗試。 – creyD

+0

https://stackoverflow.com/q/30025428/1553934看看這個問題。我想你要找的是'CompletableFuture.allOf(...)' – esin88

回答

3

你是什麼之後是allMatch()方法是這樣的:

boolean result = futures.stream().allMatch(booleanFuture -> { 
    try 
    { 
     return booleanFuture.get(); 
    } 
    catch (InterruptedException | ExecutionException e) 
    { 
     throw new RuntimeException(e); 
    } 
}); 

如果你真的意味着結果的列表中,那麼它是map()你以後是這樣的:

List<Boolean> results = futures.stream().map(booleanFuture -> { 
    try 
    { 
     return booleanFuture.get(); 
    } 
    catch (InterruptedException | ExecutionException e) 
    { 
     throw new RuntimeException(e); 
    } 
}).collect(Collectors.toList()); 
0

修改@吸血鬼的結果 你也可以使用像一個笑的parallelStream如果下面是大量數據,請點擊下面的鏈接

List<Boolean> results = futures.parallelStream().map(booleanFuture -> { 
     try 
     { 
      return booleanFuture.get(); 
     } 
     catch (InterruptedException | ExecutionException e) 
     { 
      throw new RuntimeException(e); 
     } 
    }).collect(Collectors.toList());