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
900
JavaSlang JavaDayKiev 2016
pivovarit
0
75
Javaslang - functional Java done right
pivovarit
0
210
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
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
Amazon Qを使ってIaCを触ろう!
maruto
0
400
GitHub Actionsのキャッシュと手を挙げることの大切さとそれに必要なこと
satoshi256kbyte
5
430
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
100
Contemporary Test Cases
maaretp
0
140
subpath importsで始めるモック生活
10tera
0
300
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
170
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.1k
役立つログに取り組もう
irof
28
9.6k
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
290
Featured
See All Featured
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Into the Great Unknown - MozCon
thekraken
32
1.5k
Navigating Team Friction
lara
183
14k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
410
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
What's new in Ruby 2.0
geeforr
343
31k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
How to Ace a Technical Interview
jacobian
276
23k
Being A Developer After 40
akosma
86
590k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
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