SlideShare a Scribd company logo
Java 8
An Introduction by
Jason Swartz
Agenda
1. Lambda Expressions
2. Streaming Collections
3. Monadic Collections
1. Lambda Expressions
Math Function
f(x) = x * x
Java Function
int sqr(int x) { return x * x; }
Anonymous Function
x -> x * x
Java Anonymous Function
x -> x * x
Java Anonymous Function
(x) -> x * x
Java Anonymous Function
(x, y) -> x + y
Java Anonymous Function
() -> 42
Java Anonymous Function
f = x -> x * x;
int y = f.apply(5);
Java Anonymous Function
f = (x, y) -> x + y;
int y = f.applyAsInt(5, 7);
Java Anonymous Function
r = () -> {
System.out.println("Hello");
};
r.run();
Java Method Reference
c = System.out::println;
c.accept("Hello");
Now You Know Lambdas
2. Streaming Collections
Doubling Items In A List
List<Integer> nums = Arrays
.asList(1, 2, 3);
int sqr(int x) { return x * x; }
Doubling Items In A List
nums2 = new ArrayList<Integer>();
for (int i : nums) {
nums2.add(sqr(i));
}
Doubling Items In A List
nums.stream()
Doubling Items In A List
nums.stream()
.map(this::sqr)
Doubling Items In A List
nums = nums.stream()
.map(this::sqr)
.collect(Collectors.toList());
Doubling Items In A List
nums = nums.stream()
.map(x -> x * x)
.collect(Collectors.toList());
Doubling Items In A List
nums = nums.parallelStream()
.map(x -> x * x)
.collect(Collectors.toList());
Doubling Items In A List
nums = nums.clusterStream()
.map(x -> x * x)
.collect(Collectors.toList());
Doubling Items In A List
nums = nums.dataCenterStream()
.map(x -> x * x)
.collect(Collectors.toList());
Welcome to
Declarative
Programming
Mapping To A List of Lists
nums.stream()
.map(x -> Arrays.list(x, x * x))
.collect(Collectors.toList());
Flatmap That List
nums = nums.stream()
.flatMap(x -> Arrays.list(x, x * x))
.collect(Collectors.toList());
Filtering Items In A List
nums = nums.stream()
.filter(x -> x % 2 == 0)
.collect(Collectors.toList());
Reducing A List
sum = nums.stream()
.reduce((a,i) -> a + i)
.get();
In Summary
sum = nums.stream()
.map(x -> x * x)
.flatMap(x -> Arrays.list(x, x * x))
.filter(x -> x % 2 == 0)
.reduce((a,i) -> a + i)
.get();
map()
flatmap()
reduce()
filter()
Now You Know Streams
3. Monadic Collections
Have you done this?
User u = getUser(“Fred”);
String name = u.getName();
Exception in thread "main" java.lang.
NullPointerException
Null values stink
Use Optional<T>, not null
Optional<User> u = getUser("Fred");
if (! u.isPresent()) return;
String name = u.get().name;
Use Optional<T>, not null
Optional<User> u = getUser("Fred");
Optional<String> name = u
.map(user -> user.name);
Another Example
Checking a user name
User u = getUser(“Fred”);
Boolean result = null;
if (u != null)
Result = checkName(u.getName());
else
Result = false;
Checking a user name
Optional<User> u = getUser("Fred");
Boolean result;
if (u.isPresent())
result = confirmName(u.get().name);
else
result = false;
Confirming a user name with map()
Boolean result = getUser("Fred")
.map(u -> u.name)
.map(this::confirmName)
.orElse(false);
Optional is just a monad, a
monoid in the category of
endofunctors.
What’s the problem?
Optional is just a monad, a
monoid in the category of
endofunctors.
What’s the problem?
Optional is a zero- or one-
sized collection that helps
you cleanly handle
missing values.
More Monadic
Collections?
CompletableFuture is a
zero- or one-sized
collection that helps you
cleanly handle future
values.
Javaslang’s Try is a zero-
or one-sized collection that
helps you cleanly handle
exception-thrown values.
Javaslang’s Lazy is a zero-
or one-sized collection that
helps you cleanly postpone
calculating values.
Now You Know
Monadic Collections
Thanks For Watching!

More Related Content

PDF
Python_ 3 CheatSheet
PDF
Data Structures In Scala
PDF
Python 2.5 reference card (2009)
PDF
Python3 cheatsheet
PDF
Mementopython3 english
PDF
Cheat sheet python3
PDF
Python For Data Science Cheat Sheet
PDF
Haskell 101
Python_ 3 CheatSheet
Data Structures In Scala
Python 2.5 reference card (2009)
Python3 cheatsheet
Mementopython3 english
Cheat sheet python3
Python For Data Science Cheat Sheet
Haskell 101

What's hot (17)

PDF
Beginning Haskell, Dive In, Its Not That Scary!
PPT
Collection Core Concept
PPTX
Introduction to Monads in Scala (1)
PPT
Oop lecture9 13
PPT
Collections Framework
PPTX
Introduction to Monads in Scala (2)
PDF
JAVA 8 : Migration et enjeux stratégiques en entreprise
PDF
Basic data structures in python
PDF
Haskell for data science
PDF
Predictably
PDF
Humble introduction to category theory in haskell
PDF
Fp java8
PDF
Scala collections
PPTX
Millionways
PPT
JDBC Core Concept
PPT
Functional Patterns for the non-mathematician
PDF
Arrays in python
Beginning Haskell, Dive In, Its Not That Scary!
Collection Core Concept
Introduction to Monads in Scala (1)
Oop lecture9 13
Collections Framework
Introduction to Monads in Scala (2)
JAVA 8 : Migration et enjeux stratégiques en entreprise
Basic data structures in python
Haskell for data science
Predictably
Humble introduction to category theory in haskell
Fp java8
Scala collections
Millionways
JDBC Core Concept
Functional Patterns for the non-mathematician
Arrays in python
Ad

Similar to Java 8 - An Introduction by Jason Swartz (20)

ODP
Introducing scala
KEY
関数潮流(Function Tendency)
PDF
Scala Bootcamp 1
PDF
Practical cats
PDF
Java 8 lambda expressions
PDF
Futures e abstração - QCon São Paulo 2015
PDF
Spark workshop
PPTX
mat lab introduction and basics to learn
PPT
Intro to Functional Programming Workshop (code4lib)
PDF
Scala Collections
PDF
Why Haskell Matters
PDF
Introduction to R programming
PDF
TI1220 Lecture 6: First-class Functions
PDF
Java VS Python
PDF
Lambda? You Keep Using that Letter
PDF
lecrfigfdtj x6 I f I ncccfyuggggrst3.pdf
PDF
Артём Акуляков - F# for Data Analysis
PPT
An introduction to scala
PDF
Fp in scala part 2
PDF
How to use Map() Filter() and Reduce() functions in Python | Edureka
Introducing scala
関数潮流(Function Tendency)
Scala Bootcamp 1
Practical cats
Java 8 lambda expressions
Futures e abstração - QCon São Paulo 2015
Spark workshop
mat lab introduction and basics to learn
Intro to Functional Programming Workshop (code4lib)
Scala Collections
Why Haskell Matters
Introduction to R programming
TI1220 Lecture 6: First-class Functions
Java VS Python
Lambda? You Keep Using that Letter
lecrfigfdtj x6 I f I ncccfyuggggrst3.pdf
Артём Акуляков - F# for Data Analysis
An introduction to scala
Fp in scala part 2
How to use Map() Filter() and Reduce() functions in Python | Edureka
Ad

More from Jason Swartz (10)

PPTX
High Performance Serverless Functions in Scala
PDF
Functional Database Strategies at Scala Bay
PDF
Functional Database Strategies
PDF
Microservices Tutorial Session at JavaOne 2016
PDF
Everyone's Guide to States, Events and Async-Messaging for Microservices
PDF
Everyone's guide to event sourcing and async-messaging
PDF
Enterprise APIs With Ease - Scala Developers of Barcelona
PDF
Build Enterprise APIs WIth Ease (And Scala)
PDF
OSCON - Get Started Developing With Scala
PDF
APICon SF - Enterprise APIs With Ease
High Performance Serverless Functions in Scala
Functional Database Strategies at Scala Bay
Functional Database Strategies
Microservices Tutorial Session at JavaOne 2016
Everyone's Guide to States, Events and Async-Messaging for Microservices
Everyone's guide to event sourcing and async-messaging
Enterprise APIs With Ease - Scala Developers of Barcelona
Build Enterprise APIs WIth Ease (And Scala)
OSCON - Get Started Developing With Scala
APICon SF - Enterprise APIs With Ease

Recently uploaded (20)

PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
DOCX
573137875-Attendance-Management-System-original
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
PPT on Performance Review to get promotions
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Welding lecture in detail for understanding
PPTX
additive manufacturing of ss316l using mig welding
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPT
Project quality management in manufacturing
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
Automation-in-Manufacturing-Chapter-Introduction.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
573137875-Attendance-Management-System-original
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
R24 SURVEYING LAB MANUAL for civil enggi
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPT on Performance Review to get promotions
CYBER-CRIMES AND SECURITY A guide to understanding
OOP with Java - Java Introduction (Basics)
Welding lecture in detail for understanding
additive manufacturing of ss316l using mig welding
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
bas. eng. economics group 4 presentation 1.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Project quality management in manufacturing
Embodied AI: Ushering in the Next Era of Intelligent Systems
Model Code of Practice - Construction Work - 21102022 .pdf

Java 8 - An Introduction by Jason Swartz