- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目前要使用 CompletionStage 的集合做一些简单的事情需要跳过几个难看的圈子:
public static CompletionStage<String> translate(String foo) {
// just example code to reproduce
return CompletableFuture.completedFuture("translated " + foo);
}
public static CompletionStage<List<String>> translateAllAsync(List<String> input) {
List<CompletableFuture<String>> tFutures = input.stream()
.map(s -> translate(s)
.toCompletableFuture())
.collect(Collectors.toList()); // cannot use toArray because of generics Arrays creation :-(
return CompletableFuture.allOf(tFutures.toArray(new CompletableFuture<?>[0])) // not using size() on purpose, see comments
.thenApply(nil -> tFutures.stream()
.map(f -> f.join())
.map(s -> s.toUpperCase())
.collect(Collectors.toList()));
}
public CompletionStage<List<String>> translateAllAsync(List<String> input) {
// allOf takes a collection< futures<X>>,
// and returns a future<collection<x>> for thenApply()
return XXXUtil.allOf(input.stream()
.map(s -> translate(s))
.collect(Collectors.toList()))
.thenApply(translations -> translations.stream()
.map(s -> s.toUpperCase())
.collect(Collectors.toList()));
}
Future<Collection<Future<X>>>
而不是
Future<Collection<X>>
在某些情况下也可能有用。
@Test
public void testTranslate() throws Exception {
List<String> list = translateAllAsync(Arrays.asList("foo", "bar")).toCompletableFuture().get();
Collections.sort(list);
assertEquals(list,
Arrays.asList("TRANSLATED BAR", "TRANSLATED FOO"));
}
最佳答案
我刚刚查看了CompletableFuture.allOf
的源代码,发现它基本上创建了一个节点的二叉树,一次处理两个阶段。我们可以在不使用 toCompletableFuture()
的情况下轻松实现类似的逻辑。显式并一次性处理结果列表生成:
public static <T> CompletionStage<List<T>> allOf(
Stream<? extends CompletionStage<? extends T>> source) {
return allOf(source.collect(Collectors.toList()));
}
public static <T> CompletionStage<List<T>> allOf(
List<? extends CompletionStage<? extends T>> source) {
int size = source.size();
if(size == 0) return CompletableFuture.completedFuture(Collections.emptyList());
List<T> result = new ArrayList<>(Collections.nCopies(size, null));
return allOf(source, result, 0, size-1).thenApply(x -> result);
}
private static <T> CompletionStage<Void> allOf(
List<? extends CompletionStage<? extends T>> source,
List<T> result, int from, int to) {
if(from < to) {
int mid = (from+to)>>>1;
return allOf(source, result, from, mid)
.thenCombine(allOf(source, result, mid+1, to), (x,y)->x);
}
return source.get(from).thenAccept(t -> result.set(from, t));
}
public static CompletionStage<List<String>> translateAllAsync(List<String> input) {
return allOf(input.stream().map(s -> translate(s)))
.thenApply(list -> list.stream()
.map(s -> s.toUpperCase())
.collect(Collectors.toList()));
}
public static CompletionStage<List<String>> translateAllAsync(List<String> input) {
return allOf(input.stream().map(s -> translate(s).thenApply(String::toUpperCase)));
}
@Test
public void testTranslate() throws Exception {
List<String> list = translateAllAsync(Arrays.asList("foo", "bar")).toCompletableFuture().get();
assertEquals(list, Arrays.asList("TRANSLATED FOO", "TRANSLATED BAR"));
}
关于java-8 - 如何使用 CompletionStage 的集合很好地执行 allOf/AnyOf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49934126/
我正试图通过使用 allOf 来弄清楚这个 swagger API 继承问题。这是我的 swagger yaml 文件。 swagger: '2.0' info: title: Test API
我正试图通过使用 allOf 来弄清楚这个 swagger API 继承问题。这是我的 swagger yaml 文件。 swagger: '2.0' info: title: Test API
我有这种情况,其中有 10 个或更多任务被分为许多组。在这些组内,一切都应该同时运行,但是因为每个组都需要前一组的结果(第一组除外),我需要以有序的方式运行它们(组内的任务不需要按顺序运行). 任务本
我有以下一段java代码: CompletableFuture future1 = CompletableFuture.supplyAsync(() -> { try {
一个非常简单的代码片段如下: String[] list = {"a", "b", "c"}; List> completableFutureList = new ArrayList<>(); for
请耐心等待,我通常不使用 spring,也没有使用过较新版本的 java(我说较新,我指的是 1.4 版之后的任何版本) 无论如何,我遇到一个问题,我必须进行休息调用才能使用多个并行请求进行搜索。我在
我有一个按如下方式创建的客户匹配器: private static class FromResidualAllocationMatcher extends BaseMatcher {....} 在我的
如果我有 CompletableFuture future1 = service.request(param1); CompletableFuture future2 = service.reques
此匹配器检查一组匹配器,如果它们都成功则成功。 签名如下: public static Matcher allOf(Iterable> matchers) 为什么这需要一个可迭代的 Matcher
假设我们有两个执行器,1 和 2。 我们可以配置在做的时候使用哪个executor CompletableFuture cf1 = CompletableFuture.supplyAsync(()->
具有以下暂存代码: public static void main(String[] args) throws ExecutionException, InterruptedException {
有两种方法可以强制执行 CompletableFuture 在给定的时间后超时: orTimeout(long timeout, TimeUnit unit) get(long timeout, Ti
我正在寻找一个 ScalaTest 匹配器来检查列表是否包含所有所需的元素(在另一个列表中给出),但也可能是其他元素。 contain allOf 要求获取两个固定元素,由于某种原因,其余元素作为可变
我有一个异步构建多个 DTO 的方法。它在一般用途中运行良好,因此我尝试为其编写一些单元测试。该方法如下所示: public List clientLeaderboard(@RequestBo
我正在学习 Java 1.8 中的 CompletableFuture,但在尝试理解 allOf 时遇到了困难。主线程似乎不会等待任何 CompletableFuture 完成。 参见https://
来自 javadoc, AllOf() If any of the given CompletableFutures complete exceptionally, then the returned
我正在尝试测试使用 CompletableFuture.allOf 的方法。 我模拟 future,以便它们在加入时返回所需的值。 由于某种我无法理解的原因。结果的 join() 需要永远。 任何帮助
我有一个类,它使用 CompletableFutures 向两个依赖服务发出并发请求。 我的代码如下所示: @Builder @Slf4j public class TestClass { @
这个问题已经有答案了: How to implement CompletableFuture.allOf() that completes exceptionally once any of the
我正在学习如何使用 Espresso 进行 UI 测试,我想验证 Intent 的状态。我写的是这样的: intended(allOf( hasAction(equalTo(Intent.AC
我是一名优秀的程序员,十分优秀!