Skip to content

Instantly share code, notes, and snippets.

@fmcarvalho
Last active May 10, 2019 11:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmcarvalho/2be5ff6419f9d5d8df8c9c11fcd45271 to your computer and use it in GitHub Desktop.
Save fmcarvalho/2be5ff6419f9d5d8df8c9c11fcd45271 to your computer and use it in GitHub Desktop.
Corresponding Stream, Optional and CompletableFuture

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):

Eric-Meijer-flatMap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment