java—生成泛型列表的“composablefuture”的“allof”的泛型版本

6tr1vspr  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(258)

我在努力理解 CompletableFuture api,我被困在下面的问题。 allOf 本身只返回 CompletableFuture<void> ,因此只有稍微好一点的接口和一个名为 allResultsOf :此类函数应为泛型,并作为类型的参数对象接收 List<CompletableFuture<T>> 并返回类型为 CompletableFuture<List<T>> 我在函数声明中的尝试是:

public static CompletableFuture<List<T>> allResultsOf(List<CompletableFuture<T>> completableFutures) {
                CompletableFuture<Void> allFutures = CompletableFuture
                .allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()]));

                CompletableFuture<List<T>> allCompletableFuture = allFutures.thenApply(
                    future -> {
                        return completableFutures.stream()
                            .map(completableFuture -> completableFuture.join())
                            .collect(Collectors.toList());
                });

                return allCompletableFuture;
            }

但是,在我(附件)的示例代码段中,此函数会产生以下编译错误:
错误:不兼容的类型:list<completablefuture<test.greetholder>>无法转换为list<completablefuture<?>>completablefutureallcompletablefuture=allresultsof(completablefutures);
我不确定这里有什么问题
示例代码(摘自此处):

import java.util.*;
import java.util.stream.*;
import java.lang.*;
import java.io.*;
import java.util.concurrent.*;

// The main method must be in a class named "Main".
class Main {
    static class Test {
        private ExecutorService executor;
        public Test(ExecutorService es) {
            executor = es;
        }
        public class GreetHolder {

            private String greet;

            public GreetHolder(String greet) {
                this.greet = greet;
            }

            public String getGreet() {
                return greet;
            }

            public void setGreet(String greet) {
                this.greet = greet;
            }

            public String toString() {
                return getGreet();
            }
        }

        private CompletableFuture<GreetHolder> getGreeting(String lang) {
            return CompletableFuture.supplyAsync( () -> {
                try {
                    System.out.println("Task execution started for lang =" + lang);
                    Thread.sleep(200);
                    System.out.println("Task execution stopped for lang =" + lang);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return new GreetHolder(getGreet(lang));
            }, executor);
        }
        public String getGreet(String lang) {
            if (lang.equals("EN")) {
                return "Hello";
            } else if (lang.equals("ES")) {
                return "Hola";
            } else if (lang.equals("SN")) {
                return "Ayubovan";
            } else {
                throw new IllegalArgumentException("Invalid lang param");
            }
        }

        public CompletableFuture<List<Test.GreetHolder>> run() {
            List<String>  langList = Arrays.asList("EN", "ES", "SN"); //, "EX");

            List<CompletableFuture<Test.GreetHolder>> completableFutures =
            langList.stream().map(lang -> getGreeting(lang))
                //.map(CompletableFuture::join);
                .collect(Collectors.toList());

            // return completableFutures;

            CompletableFuture<Void> allFutures = CompletableFuture
            .allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()]));

            CompletableFuture<List<GreetHolder>> allCompletableFuture = allFutures.thenApply(
                future -> {
                    System.out.println(String.format("%s <- future", future));
                    return completableFutures.stream()
                        .map(completableFuture -> completableFuture.join())
                        .collect(Collectors.toList());
            });

            System.out.println(String.format("%s <- allCompletableFuture", allCompletableFuture));

            //return allCompletableFuture;

            CompletableFuture completableFuture = allCompletableFuture.thenApply(
                greets -> {
                       return greets
                            .stream()
                            .map(GreetHolder::getGreet)
                            .collect(Collectors.toList());
            });
            return completableFuture;

        }

        public static CompletableFuture<List<T>> allResultsOf(List<CompletableFuture<T>> completableFutures) {
            CompletableFuture<Void> allFutures = CompletableFuture
            .allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()]));

            CompletableFuture<List<T>> allCompletableFuture = allFutures.thenApply(
                future -> {
                    System.out.println(String.format("%s <- future", future));
                    return completableFutures.stream()
                        .map(completableFuture -> completableFuture.join())
                        .collect(Collectors.toList());
            });

            return allCompletableFuture;
        }

        public CompletableFuture<List<Test.GreetHolder>> run_tidier() {
            List<String>  langList = Arrays.asList("EN", "ES", "SN"); //, "EX");

            List<CompletableFuture<Test.GreetHolder>> completableFutures =
            langList.stream().map(lang -> getGreeting(lang))
                //.map(CompletableFuture::join);
                .collect(Collectors.toList());

            CompletableFuture<List<GreetHolder>> allCompletableFuture = allResultsOf(completableFutures);

            System.out.println(String.format("%s <- allCompletableFuture", allCompletableFuture));            

            CompletableFuture completableFuture = allCompletableFuture.thenApply(
                greets -> {
                       return greets
                            .stream()
                            .map(GreetHolder::getGreet)
                            .collect(Collectors.toList());
            });
            return completableFuture;

        }
    }
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService pool = Executors.newFixedThreadPool(2);
        Test t = new Test(pool);
        System.out.println(String.format("%s world!", t.getGreet("EN")));
        CompletableFuture cf = t.run();
        System.out.println(String.format("%s <- cf", cf.get()));
    }
}
inn6fuwd

inn6fuwd1#

正如matt timmermans所建议的,您需要一个类型变量。
改变 allResultsOf 为此编译:

public static <T> CompletableFuture<List<T>> allResultsOf(List<CompletableFuture<T>> completableFutures) {
            CompletableFuture<Void> allFutures = CompletableFuture
                    .allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()]));

            CompletableFuture<List<T>> allCompletableFuture = allFutures.thenApply(
                    future -> {
                        System.out.println(String.format("%s <- future", future));
                        return completableFutures.stream()
                                .map(completableFuture -> completableFuture.join())
                                .collect(Collectors.toList());
                    });

            return allCompletableFuture;
        }

相关问题