I would love if Java has given consistent names for the same operations among these types:
Stream<T> (S<T> ) |
Optional<T> (O<T> ) |
CompletableFuture<T> (CF<T> ) |
---|---|---|
void forEach(Consumer<T>) |
void ifPresent(Consumer<T>) |
CF<Void> thenAccept(Consumer<T>) |
S<R> map(Function<T, R>) |
O<R> map(Function<T, R>) |
CF<R> thenApply(Function<T, R>) |
S<R> flatMap(Function<T, S<R>>) |
O<R> flatMap(Function<T, O<R>>) |
CF<R> thenCompose(Function<T, CF<R>>) |
S<T> peek(Consumer<T>) |
-- | CF<T> whenComplete(BiConsumer<T,Throwable>) |
S<R> zip(S<U>, BiFunction<T, U, R>) (*) |
-- | CF<R> thenCombine(CF<U>, BiFunction<T, U, R>) |
(*) does not exist in JDK.
About Monads with Java 8
Regarding Monads, I usually simplify it only thinking on flatMap()
(from "Principles of Reactive Programming" course by Erik Meijer):