인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

인프런 커뮤니티 질문&답변

tkddyd420님의 프로필 이미지
tkddyd420

작성한 질문수

더 자바, Java 8

CompletableFuture 2

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

작성

·

607

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
질문자

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

tkddyd420님의 프로필 이미지
tkddyd420

작성한 질문수

질문하기