- get: a blocking method that wait until the completable future is finished
- supplyAsync: receives lambda with no input then execute the operation; e.g search
- thenApply(map): process the execution result; e.g the search result, and return it
- thenAccept: process the execution result; e.g t search result, and process the result in place without returning anything.
- allOf: run multiple futures. Doesn’t return the result of all the completableFutures. Need to handle each result alone.
- thenRun: process the execution result without receiving it or returning anything.
- Async Threads: Runnable and Supply, we can use them to return completable future.
- Monadic design pattern: Combining completable futures in chain using thenCompose and the thenApply which receives the result of the previous completable future.
- thenCompose(flatMap): receives a function that returns another object of the same type. The input of the lambda is the completableFuture result just like the thenApply.
- thenCombine: used when we want to use the result of two futures.
- thenAcceptBoth: doesn’t send result down the future
Functional Interfaces in Java 8
Represents a predicate (boolean-valued function) of one argument.
Predicate<Integer> isAnAdult = age -> age >= 18;
we can actually try using our new predicate in a stream.
Represents an operation that accepts a single input argument and returns no result.
Consumer<Ticket> ticketPrinter = ticket -> ticket.print();
The new forEach method in the Iterable interface takes the Consumer interface as an argument:
Collection<Ticket> tickets = getTicketsToPrint(); tickets.forEach(ticket -> ticket.print());
This is kind of a factory. It takes no arguments, and just gives you a result. Perfect for returning an instance.
Supplier<TicketHandler> ticketHandlerCreator = () -> new TicketHandler();
Another solution is to use the constructor reference.
Supplier<TicketHandler> ticketHandlerCreator = TicketHandler::new;
Represents a function that accepts one argument and produces a result. Let’s just go straight to an example.