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
920
JavaSlang JavaDayKiev 2016
pivovarit
0
75
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
良いユニットテストを書こう
mototakatsu
10
3.3k
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
160
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
5
1.2k
Fibonacci Function Gallery - Part 2
philipschwarz
PRO
0
150
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
1.1k
AWSのLambdaで PHPを動かす選択肢
rinchoku
2
330
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
1.1k
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
130
Amazon S3 NYJavaSIG 2024-12-12
sullis
0
110
Online-Dokumentation, die hilft: Strukturen, Prozesse, Tools
ahus1
0
100
nekko cloudにおけるProxmox VE利用事例
irumaru
3
480
Recoilを剥がしている話
kirik
5
7.5k
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Raft: Consensus for Rubyists
vanstee
137
6.7k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.2k
A Philosophy of Restraint
colly
203
16k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Embracing the Ebb and Flow
colly
84
4.5k
The Invisible Side of Design
smashingmag
299
50k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
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