Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中组合多个List(对于我)或其他类型的CompletionStage响应_Java_Java 8_Completable Future_Completion Stage - Fatal编程技术网

如何在java中组合多个List(对于我)或其他类型的CompletionStage响应

如何在java中组合多个List(对于我)或其他类型的CompletionStage响应,java,java-8,completable-future,completion-stage,Java,Java 8,Completable Future,Completion Stage,我正在尝试创建多个类型列表的CompletionStage,例如CompletionStage。最后,我想将类型的所有响应合并到一个CompletionStage中的一个列表中 CompletionStage<List<Car>> completionStageOne= carClientOne.getCarList(); CompletionStage<List<Car>> completionStageTwo= carClientTwo.get

我正在尝试创建多个类型列表的CompletionStage,例如
CompletionStage
。最后,我想将
类型的所有响应合并到一个CompletionStage中的一个列表中

CompletionStage<List<Car>> completionStageOne= carClientOne.getCarList();
CompletionStage<List<Car>> completionStageTwo= carClientTwo.getCarList();
CompletionStage<List<Car>> completionStageThree= carClientThree.getCarList();
CompletionStage completionStageOne=carClientOne.getCarList();
CompletionStage completionStageTowo=carClientTwo.getCarList();
CompletionStage completionStageThree=carClientThree.getCarList();
在这里,假设我有3个不同的服务,它们会给我不同的汽车列表,作为
CompletionStage

现在,我正试图将它们结合起来,创建一个通用的汽车列表,在这里我遇到了问题。 我使用下面的代码来组合结果

CompletionStage<List<Car>> completionStageOneTwo = completionStageOne
.thenCombine(completionStageTwo,(x, y) -> Stream.concat(x.stream(), y.stream()).collect(Collectors.toList()));

//above will work but if I add the third one then it will not. 

CompletionStage<List<Car>> completionStageFinal = completionStageOneTwo
.thenCombine(completionStageThree,(x, y) -> Stream.concat(x.stream(), y.stream()).collect(Collectors.toList())); 
CompletionStage completionStageOneTwo=completionStageOne
.thenCombine(completionstagewo,(x,y)->Stream.concat(x.Stream(),y.Stream()).collect(collector.toList());
//上面的方法会起作用,但如果我加上第三个,它就不会起作用了。
CompletionStage completionStageFinal=completionStageOneTwo
.thenCombine(completionStageThree,(x,y)->Stream.concat(x.Stream(),y.Stream()).collect(collector.toList());
最后,我在做什么

List<Car> finalList = completionStageFinal.toCompletableFuture().get();
List finalList=completionStageFinal.toCompletableFuture().get();
那么我做错了什么?我怎样才能把这三个结合起来呢?我挡住什么了吗


注意:我已经检查过了,但是没有弄清楚如何在那里使用concat。

让我给你看一个例子。我将展示如何使用
CompletableFuture.AllOf(…)
来等待所有的未来

    // create promises to get cars
    CompletableFuture<List<String>> cars1 = CompletableFuture.completedFuture(Arrays.asList("BMW", "Alfa"));
    CompletableFuture<List<String>> cars2 = CompletableFuture.completedFuture(Collections.singletonList("WV"));
    CompletableFuture<List<String>> cars3 = CompletableFuture.completedFuture(Collections.singletonList("FIAT"));

    // collect promises just for convenience
    List<CompletableFuture<List<String>>> allFutures = Arrays.asList(cars1, cars2, cars3);

    // wait until all cars will be obtained
    CompletableFuture<List<String>> listCompletableFuture =
            CompletableFuture.allOf(cars1, cars2, cars3)
            .thenApply(avoid -> allFutures  //start to collect them
                    .stream()
                    .flatMap(f -> f.join().stream()) //get List from feature. Here these cars has been obtained, therefore non blocking
                    .collect(Collectors.toList())
    );

    // there are here
    listCompletableFuture.join().forEach(System.out::println);

实际上,我遇到了同样的问题,并且有一个例子说明了如何使用多个completable futures来构造一个对象

这比需要的更“进取”,但你应该能够理解这个想法

public static void main (String[] args){
    CompletableFuture<ClassA> classAfuture = futureProvider.retrieveClassA();
    CompletableFuture<ClassB> classBfuture = futureProvider.retrieveClassB();
    CompletableFuture<ClassC> classCfuture = futureProvider.retrieveClassC();

    System.out.println("starting completable futures ...");
    long startTime = System.nanoTime();

    ABCData ABCData = CompletableFuture.allOf(classAfuture, classBfuture, classCfuture)
            .thenApply(ignored ->
                    combineFunction.combind(
                            classAfuture.join(),
                            classBfuture.join(),
                            classCfuture.join())
            ).join();

    long endTime = System.nanoTime();
    long duration = (endTime - startTime);
    System.out.println("completable futures are complete...");
    System.out.println("duration:\t" + Duration.ofNanos(duration).toString());
    System.out.println("result:\t" + ABCData);
} 
publicstaticvoidmain(字符串[]args){
CompletableFuture classAfuture=futureProvider.retrieveClassA();
CompletableFuture classBfuture=futureProvider.retrieveClassB();
CompletableFuture classCfuture=futureProvider.retrieveClassC();
System.out.println(“启动可完成期货…”);
long startTime=System.nanoTime();
ABCData ABCData=CompletableFuture.allOf(classAfuture,classBfuture,classCfuture)
。然后应用(忽略->
combineFunction.combined(
classAfuture.join(),
classBfuture.join(),
classCfuture.join())
).join();
long-endTime=System.nanoTime();
长持续时间=(结束时间-开始时间);
System.out.println(“可完成的期货已完成…”);
System.out.println(“duration:\t”+duration.ofNanos(duration.toString());
System.out.println(“结果:\t”+ABCData);
} 

另外一个好处是,总请求只需要最长的单个请求的时间

你的控制台中有stacktrace吗?你所说的“[…]如果我添加第三个,那么它将不起作用”是什么意思?@didiierl所以当我添加时,
CompletionStage completionStageOneTwo=completionStageOne.thenCombine(completionStageTwo,(x,y)->Stream.concat(x.Stream(),y.Stream()).collect(collector.toList())它将两个结果合并到一个类型列表的CompletionStageThree中,但当我尝试将CompletionStageThree与completionStageOneTwo(它是completionStageOne和CompletionStageTo组合的结果)组合在一起时,它不起作用。它给出了SocketTimeOutException
java.lang.RuntimeException:java.util.concurrent.ExecutionException:java.net.SocketTimeoutException:30000毫秒连接超时
public static void main (String[] args){
    CompletableFuture<ClassA> classAfuture = futureProvider.retrieveClassA();
    CompletableFuture<ClassB> classBfuture = futureProvider.retrieveClassB();
    CompletableFuture<ClassC> classCfuture = futureProvider.retrieveClassC();

    System.out.println("starting completable futures ...");
    long startTime = System.nanoTime();

    ABCData ABCData = CompletableFuture.allOf(classAfuture, classBfuture, classCfuture)
            .thenApply(ignored ->
                    combineFunction.combind(
                            classAfuture.join(),
                            classBfuture.join(),
                            classCfuture.join())
            ).join();

    long endTime = System.nanoTime();
    long duration = (endTime - startTime);
    System.out.println("completable futures are complete...");
    System.out.println("duration:\t" + Duration.ofNanos(duration).toString());
    System.out.println("result:\t" + ABCData);
}