Skip to content

Instantly share code, notes, and snippets.

@vichu
Created May 11, 2018 00:00
Show Gist options
  • Save vichu/7a585d79e2abd16fae9099c263600b26 to your computer and use it in GitHub Desktop.
Save vichu/7a585d79e2abd16fae9099c263600b26 to your computer and use it in GitHub Desktop.
Chaining thenApply method on CompletableFutures
CompletableFuture<CompletableFuture<CompletableFuture<Map<String, Object>>>> result = myCompletableFutureBigDecimal.thenApply(bigDecimal ->
myCompletableFutureInt.thenApply( integer ->
myCompletableFutureLong.thenApply(aLong -> {
Map<String, Object> objectHashMap = new HashMap<>();
objectHashMap.put("IntegerValue", integer);
objectHashMap.put("LongValue", aLong);
objectHashMap.put("BigDecimalValue", bigDecimal);
return objectHashMap;
})));
try {
Map<String, Object> re = result
.get(2, TimeUnit.SECONDS)
.get()
.get();
System.out.println(re);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment