I have List of futures
List<Future<Boolean>> futures = service.invokeAll( Arrays.asList( callable1, callable2 ));
what i need is a way to get a list of results
can you provide a solution in java?
something like whenAll()...
I have List of futures
List<Future<Boolean>> futures = service.invokeAll( Arrays.asList( callable1, callable2 ));
what i need is a way to get a list of results
can you provide a solution in java?
something like whenAll()...
What you are after is the allMatch()
method like this:
boolean result = futures.stream().allMatch(booleanFuture -> {
try
{
return booleanFuture.get();
}
catch (InterruptedException | ExecutionException e)
{
throw new RuntimeException(e);
}
});
If you really meant a list of results, then it is map()
you are after like this:
List<Boolean> results = futures.stream().map(booleanFuture -> {
try
{
return booleanFuture.get();
}
catch (InterruptedException | ExecutionException e)
{
throw new RuntimeException(e);
}
}).collect(Collectors.toList());
Modifiying @Vampire 's results You can also use a parallelStream like one shown below if it is a lot of data
List<Boolean> results = futures.parallelStream().map(booleanFuture -> {
try
{
return booleanFuture.get();
}
catch (InterruptedException | ExecutionException e)
{
throw new RuntimeException(e);
}
}).collect(Collectors.toList());