• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

allOf()에서 컬렉션으로 반환하는 것을 설명할 때 코드

22.09.14 10:52 작성 조회수 396

0

12:03분 쯤에 완성된 코드

        List<CompletableFuture<String>> futures = Arrays.asList(hello, world);
        CompletableFuture<String>[] futuresArray = futures.toArray(new CompletableFuture[futures.size()]);
        CompletableFuture<List<String>> results = CompletableFuture.allOf(futuresArray)
                .thenApply(v -> futures.stream()
                        .map(CompletableFuture::join) // join()은 Unchecked Exception, get()은 Checked Exception 발생
                        .collect(Collectors.toList()));
        results.get().forEach(System.out::println);

 

Completablefuture 작업들을 컬렉션으로 만들지않고, thenApply()에서 결과값을 List로 만들어주기

        CompletableFuture<List<String>> results = CompletableFuture.allOf(hello, world)
                .thenApply(v -> Arrays.asList(hello.join(), world.join()));
        results.get().forEach(System.out::println);

이 때 두 개의 코드는 동일한 기능으로 작동해도 된다고 봐도 될까요??

출력값도 동일하고, 강사님께서 말씀해주신대로 hello와 world 작업이 끝난 후에 그 결과값을 join()으로 가져와서 List로 만드는 기능으로 이해하고 있습니다.

답변 1

답변을 작성해보세요.

1

네 맞네요. 훨씬 간결하네요. 귿!

tkddyd420님의 프로필

tkddyd420

질문자

2022.09.14

헉... 빠른 답변 감사합니다.
좋은 강의 열심히 잘 듣도록 하겠습니다!