Skip to content

Instantly share code, notes, and snippets.

@cmonkey
Created October 10, 2019 07:26
Show Gist options
  • Save cmonkey/81176d0c46c3bddc136b9945738ba9aa to your computer and use it in GitHub Desktop.
Save cmonkey/81176d0c46c3bddc136b9945738ba9aa to your computer and use it in GitHub Desktop.
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