Java CompletableFuture applyToEither()
May 29, 2019
Java CompletableFuture
implements CompletionStage
and Future
interfaces. CompletableFuture.applyToEither
is inherited from CompletionStage
. The applyToEither
method returns a new CompletionStage
that, when either this or the other given stage complete normally, is executed with the corresponding result as argument to the supplied function.
Find the method declaration of
applyToEither
method from Java doc.
<U> CompletionStage<U> applyToEither(CompletionStage<? extends T> other, Function<? super T,U> fn)
The parameter other is the other
CompletionStage
.
The parameter fn is the function to use to compute the value of the returned
CompletionStage
.
applyToEither
returns new CompletionStage
.Find the examples.
Example-1:
ApplyToEitherDemo1.java
package com.concretepage; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; public class ApplyToEitherDemo1 { public static void main(String[] args) throws InterruptedException, ExecutionException { CompletableFuture<Person> primaryFuture = CompletableFuture.completedFuture(new Person("Mohan", "Varanasi")); CompletableFuture<Person> secondaryFuture = CompletableFuture.completedFuture(new Person("Shyam", "Prayagraj")); CompletableFuture<String> future = primaryFuture.applyToEither(secondaryFuture, person -> person.getName() + " - " +person.getCity()); System.out.println(future.get()); } }
Mohan - Varanasi
applyToEither
method applies the result of this completion stage or other completion stage whichever completes normally earlier, to the given function and returns a new completion stage. In the above example, sometimes we will get result for primaryFuture
and sometimes for secondaryFuture
.
Example-2:
ApplyToEitherDemo2.java
package com.concretepage; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; public class ApplyToEitherDemo2 { public static void main(String[] args) throws InterruptedException, ExecutionException { CompletableFuture<Person> mainFuture = CompletableFuture.supplyAsync(() -> getPerson()); CompletableFuture<Person> defaultFuture = CompletableFuture.supplyAsync(() -> getDefaultFuture()); CompletableFuture<String> future = mainFuture.applyToEither(defaultFuture, person -> person.getName() + " - " + person.getCity()); System.out.println(future.join()); } private static Person getPerson() { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } return new Person("Krishna", "Delhi"); } private static Person getDefaultFuture() { return new Person("Default name", "Default city"); } }
Default name - Default city
defaultFuture
will complete earlier than mainFuture
. So applyToEither
will apply the result of defaultFuture
to the given function.
Example-3: Find the example of
applyToEither
with acceptEither
method.
package com.concretepage; import java.util.concurrent.CompletableFuture; public class ApplyToEitherDemo3 { public static void main(String[] args) { CompletableFuture<Person> mainFuture = CompletableFuture.supplyAsync(() -> getPerson()); CompletableFuture<Person> defaultFuture = CompletableFuture.supplyAsync(() -> getDefaultFuture()); CompletableFuture<String> cfuture = mainFuture.applyToEither(defaultFuture, person -> person.getName() + " - " + person.getCity()); CompletableFuture<String> otherCFuture = CompletableFuture.supplyAsync(() -> getMsg()); CompletableFuture<Void> cf = cfuture.acceptEither(otherCFuture, s -> System.out.println(s)); cf.join(); } private static String getMsg() { try { Thread.sleep(400); // Thread.sleep(1000); } catch (InterruptedException e) { System.err.println(e); } return "Namo - Gujraat"; } private static Person getPerson() { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } return new Person("Krishna", "Delhi"); } private static Person getDefaultFuture() { try { Thread.sleep(600); } catch (InterruptedException e) { e.printStackTrace(); } return new Person("Default name", "Default city"); } }
Namo - Gujraat
References
Class CompletableFutureJava CompletableFuture acceptEither()