Created
October 10, 2019 07:26
-
-
Save cmonkey/81176d0c46c3bddc136b9945738ba9aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.concurrent.CompletableFuture; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.function.Supplier; | |
import java.util.stream.Collectors; | |
public class CompletableFutureChallenge{ | |
static ExecutorService executor = Executors.newCachedThreadPool(); | |
public static void main (String... oracleCodeOneAdventure) { | |
CompletableFuture<List<String>> adventureStart = new CompletableFuture<>(); | |
Supplier<List<String>> sanFranSightSupplier = () -> List.of("Alcatraz", "Cable Car", "Golden Gate", "Lombard Street"); | |
adventureStart.completeAsync(sanFranSightSupplier, executor) | |
.thenCompose(sights -> { | |
return CompletableFuture.supplyAsync(() -> sights.stream() | |
.map(String::length) | |
.collect(Collectors.toList())); | |
}) | |
.thenAccept(ratings -> { | |
var rating = ratings.stream() | |
.dropWhile(sightRating -> sightRating <= 12) | |
.findFirst() | |
.orElse(0); | |
System.out.print("Rating: " + rating + " "); | |
}); | |
System.out.print("Time to go home : ( "); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment