合并可完整的期货
我有这样的代码:
public class ProcessorService {
@Autowired private AsyncService asyncService;
public Void process() {
List<CompletableFuture<List<MyEntity>>> futures = new ArrayList<>();
for (i = 0; i < 3; i++) {
futures.add(asyncService.getSomething());
}
List<MyEntity> flatList = sequence(futures); // compile error
}
private static <T> CompletableFuture<List<T>> sequence(List<CompletableFuture<T>> futures) {
CompletableFuture<Void> allDoneFuture =
CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]));
return allDoneFuture.thenApply(v ->
futures.stream().
map(future -> future.join()).
collect(Collectors.<T>toList())
);
}
}
汇编错误是:
Required type: List<MyEntity>
Provided: CompletableFuture<List<List<MyEntity>>>
如何获得将单个期货中所有元素合并为一个元素的列表?
I have a piece of code like this:
public class ProcessorService {
@Autowired private AsyncService asyncService;
public Void process() {
List<CompletableFuture<List<MyEntity>>> futures = new ArrayList<>();
for (i = 0; i < 3; i++) {
futures.add(asyncService.getSomething());
}
List<MyEntity> flatList = sequence(futures); // compile error
}
private static <T> CompletableFuture<List<T>> sequence(List<CompletableFuture<T>> futures) {
CompletableFuture<Void> allDoneFuture =
CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]));
return allDoneFuture.thenApply(v ->
futures.stream().
map(future -> future.join()).
collect(Collectors.<T>toList())
);
}
}
The compilation error is:
Required type: List<MyEntity>
Provided: CompletableFuture<List<List<MyEntity>>>
How can I get a list that has all the elements from individual futures merged into one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论