Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
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
130
Javaslang DevoxxMA
pivovarit
2
1k
JavaSlang JavaDayKiev 2016
pivovarit
0
76
Javaslang - functional Java done right
pivovarit
0
220
Java Wars VIII: The Function Awakens
pivovarit
0
680
Java 8 vs. Checked Exceptions
pivovarit
0
110
Other Decks in Programming
See All in Programming
「Cursor/Devin全社導入の理想と現実」のその後
saitoryc
0
140
ニーリーにおけるプロダクトエンジニア
nealle
0
110
ドメインモデリングにおける抽象の役割、tagless-finalによるDSL構築、そして型安全な最適化
knih
11
2k
PHPで始める振る舞い駆動開発(Behaviour-Driven Development)
ohmori_yusuke
2
170
Claude Codeの使い方
ttnyt8701
1
130
C++20 射影変換
faithandbrave
0
520
Rubyでやりたい駆動開発 / Ruby driven development
chobishiba
1
340
生成AIで日々のエラー調査を進めたい
yuyaabo
0
640
[初登壇@jAZUG]アプリ開発者が気になるGoogleCloud/Azure+wasm/wasi
asaringo
0
130
Railsアプリケーションと パフォーマンスチューニング ー 秒間5万リクエストの モバイルオーダーシステムを支える事例 ー Rubyセミナー 大阪
falcon8823
4
910
Is Xcode slowly dying out in 2025?
uetyo
1
190
イベントストーミング図からコードへの変換手順 / Procedure for Converting Event Storming Diagrams to Code
nrslib
1
300
Featured
See All Featured
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
48
2.8k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
45
7.4k
The Cost Of JavaScript in 2023
addyosmani
51
8.4k
Mobile First: as difficult as doing things right
swwweet
223
9.7k
Building Adaptive Systems
keathley
43
2.6k
Docker and Python
trallard
44
3.4k
Adopting Sorbet at Scale
ufuk
77
9.4k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
20k
Into the Great Unknown - MozCon
thekraken
39
1.9k
Side Projects
sachag
455
42k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
For a Future-Friendly Web
brad_frost
179
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