Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Java 8: Lambdas, Monads and Java Collections
Search
Grzegorz Piwowarek
July 06, 2015
Programming
1
110
Java 8: Lambdas, Monads and Java Collections
Presented during Confitura 2015
Grzegorz Piwowarek
July 06, 2015
Tweet
Share
More Decks by Grzegorz Piwowarek
See All by Grzegorz Piwowarek
Javaslang DevoxxBE 2016
pivovarit
1
140
Javaslang DevoxxMA
pivovarit
2
1.1k
JavaSlang JavaDayKiev 2016
pivovarit
0
78
Javaslang - functional Java done right
pivovarit
0
220
Java Wars VIII: The Function Awakens
pivovarit
0
690
Java 8 vs. Checked Exceptions
pivovarit
0
110
Other Decks in Programming
See All in Programming
sbt 2
xuwei_k
0
190
Level up your Gemini CLI - D&D Style!
palladius
1
170
バックエンドエンジニアによる Amebaブログ K8s 基盤への CronJobの導入・運用経験
sunabig
0
130
C-Shared Buildで突破するAI Agent バックテストの壁
po3rin
0
210
TVerのWeb内製化 - 開発スピードと品質を両立させるまでの道のり
techtver
PRO
3
1.4k
関数の挙動書き換える
takatofukui
4
770
All(?) About Point Sets
hole
0
260
Microservices rules: What good looks like
cer
PRO
0
560
「文字列→日付」の落とし穴 〜Ruby Date.parseの意外な挙動〜
sg4k0
0
360
AWS CDKの推しポイントN選
akihisaikeda
1
240
GeistFabrik and AI-augmented software development
adewale
PRO
0
250
無秩序からの脱却 / Emergence from chaos
nrslib
2
12k
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.5k
The Power of CSS Pseudo Elements
geoffreycrofte
80
6.1k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
380
Producing Creativity
orderedlist
PRO
348
40k
Agile that works and the tools we love
rasmusluckow
331
21k
Rebuilding a faster, lazier Slack
samanthasiow
84
9.3k
How GitHub (no longer) Works
holman
316
140k
The Language of Interfaces
destraynor
162
25k
Done Done
chrislema
186
16k
BBQ
matthewcrist
89
9.9k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
9
1k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.8k
Transcript
JAVA 8 LAMBDAS, MONADS && JAVA COLLECTIONS Grzegorz Piwowarek
GRZEGORZ PIWOWAREK @PIVOVARIT
GRZEGORZ PIWOWAREK @PIVOVARIT visionsoftondal.com
Plan: -lambda expressions -java.util.function -monad -Optional -Stream
lambda expressions (...) -> statement - Anonymous function
lambda expressions x -> x + 1
lambda expressions x -> x + 1 - No type
information
lambda expressions x -> x + 1 () -> 42
() -> {return 42;} (x, y) -> {} () -> {}
method references
java.util.function
java.util.function
Function<T, R>, BiFunction<T,U,R> Consumer<T> extends Function<T,Void> Supplier<T> extends Function<Void, T>
Predicate<T> extends Function<T, Boolean> UnaryOperator<T> extends Function<T, T> BinaryOperator<T> extends BiFunction<T,T,T>
Function<T, R>, BiFunction<T,U,R> Consumer<T> extends Function<T,Void> Supplier<T> extends Function<Void, T>
Predicate<T> extends Function<T, Boolean> UnaryOperator<T> extends Function<T, T> BinaryOperator<T> extends BiFunction<T,T,T>
Function<T, R>, BiFunction<T,U,R> Consumer<T> extends Function<T,Void> Supplier<T> extends Function<Void, T>
Predicate<T> extends Function<T, Boolean> UnaryOperator<T> extends Function<T, T> BinaryOperator<T> extends BiFunction<T,T,T>
Function<T, R>, BiFunction<T,U,R> Consumer<T> extends Function<T,Void> Supplier<T> extends Function<Void, T>
Predicate<T> extends Function<T, Boolean> UnaryOperator<T> extends Function<T, T> BinaryOperator<T> extends BiFunction<T,T,T>
Function<T, R>, BiFunction<T,U,R> Consumer<T> extends Function<T,Void> Supplier<T> extends Function<Void, T>
Predicate<T> extends Function<T, Boolean> UnaryOperator<T> extends Function<T, T> BinaryOperator<T> extends BiFunction<T,T,T>
MONAD
None
GOOGLE IMAGES...
...
MONAD Design pattern http://got-steam.com/
Why bother? : Boilerplate -1 Readability +1 Complexity -1 Responsibility
-1
type: M<T> "unit": T -> M<T> "bind": M<T>.bind(T -> M<U>)
= M<U> MONAD "bind": M<T>.bind(T -> U)= M<U>
type: M<T> "unit": T -> M<T> "bind": M<T> bind(T ->
M<U>) = M<U> MONAD "bind": M<T>.bind(T -> U)= M<U>
"bind": M<T> bind(T -> U)= M<U> What if U: M<?>
?
"bind": M<T> bind(T -> U)= M<U> What if U: M<?>
?
"bind": M<T> bind(T -> U)= M<U> What if U: M<?>
? http://www.ivanaborovnjak.com/project/box-in-a-box/
Monads in Java 8 Optional Stream CompletableFuture
Monads in Java 8 Optional Stream CompletableFuture
Optional Encapsulation of operations on optional values
Optional type: M<T> "unit": T -> M<T> "bind": M<T> bind(T
-> M<U>) = M<U>
Optional type: Optional<T> "unit": T -> M<T> "bind": M<T> bind(T
-> M<U>) = M<U>
Optional type: Optional<T> "unit": Optional.ofNullable(), Optional.of() "bind": M<T> bind(T ->
M<U>) = M<U>
Optional type: Optional<T> "unit": Optional.ofNullable(), Optional.of() "bind": Optional.flatMap()
Filtering an Optional .filter(Predicate<T>)
Unwrapping an Optional .get() .orElse(T default) .orElseGet(Supplier<T>) .orElseThrow(Supplier<Ex>) .ifPresent(Consumer<T>)
Java 7 style
Java 8 style
Java 8 style - flatMap
Java 7,5 style ;)
Java 7,5 style ;)
Stream Encapsulation of operations on multiple items
Stream type: Stream<T> "unit": Stream.of(), Arrays.stream(), Collection.stream() "bind": Stream.flatMap()
Stream
Stream
Stream
Stream && Optional
Stream lazy-initialized nonreusable
intermediate operations .map() .flatMap() .filter() .peek()
intermediate operations .map() .flatMap() .filter() .peek() Stream not consumed: does
not print anything
Java7
Java8
Consuming Stream .forEach(Consumer<T>) .collect() .reduce(BinaryOperator<T>) .allMatch(), anyMatch(), noneMatch() .findFirst(), findAny()
.count() .toArray()
Stream.reduce()
Collectors .toList(), toMap(), toSet(), toCollection() .minBy(), maxBy() .joining() .partitioningBy() ...and
many others
Collectors.toList()
Collectors.toMap()
Collectors.joining()
Debuggability? IntelliJ IDEA: - v14.0 - partial support - v15.0
- full support
Stream in APIs BufferedReader.lines() Files.newDirectoryStream() Random.ints() ...
you want more?
https://github.com/ jasongoodwin/better-java-monads
Thank You!
REFERENCES: -"MONADIC JAVA" BY MARIO FUSCO -"WHAT'S WRONG WITH JAVA
8" BY PIERRE-YVES SAUMONT -WWW.ORACLE.COM -"A FISTFUL OF MONADS" - LEARN YOU A HASKELL FOR GREAT GOOD