SlideShare a Scribd company logo
1 of 51
Download to read offline
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

What's hot

Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)
stasimus
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
stasimus
 
Predictably
PredictablyPredictably
Predictably
ztellman
 

What's hot (17)

Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!
 
Collection Core Concept
Collection Core ConceptCollection Core Concept
Collection Core Concept
 
Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)
 
Oop lecture9 13
Oop lecture9 13Oop lecture9 13
Oop lecture9 13
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
 
JAVA 8 : Migration et enjeux stratégiques en entreprise
JAVA 8 : Migration et enjeux stratégiques en entrepriseJAVA 8 : Migration et enjeux stratégiques en entreprise
JAVA 8 : Migration et enjeux stratégiques en entreprise
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Haskell for data science
Haskell for data scienceHaskell for data science
Haskell for data science
 
Predictably
PredictablyPredictably
Predictably
 
Humble introduction to category theory in haskell
Humble introduction to category theory in haskellHumble introduction to category theory in haskell
Humble introduction to category theory in haskell
 
Fp java8
Fp java8Fp java8
Fp java8
 
Scala collections
Scala collectionsScala collections
Scala collections
 
Millionways
MillionwaysMillionways
Millionways
 
JDBC Core Concept
JDBC Core ConceptJDBC Core Concept
JDBC Core Concept
 
Functional Patterns for the non-mathematician
Functional Patterns for the non-mathematicianFunctional Patterns for the non-mathematician
Functional Patterns for the non-mathematician
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 

Similar to Java 8 - An Introduction by Jason Swartz

関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
riue
 
Intro to Functional Programming Workshop (code4lib)
Intro to Functional Programming Workshop (code4lib)Intro to Functional Programming Workshop (code4lib)
Intro to Functional Programming Workshop (code4lib)
Will Kurt
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
Eelco Visser
 
lecrfigfdtj x6 I f I ncccfyuggggrst3.pdf
lecrfigfdtj x6 I f I ncccfyuggggrst3.pdflecrfigfdtj x6 I f I ncccfyuggggrst3.pdf
lecrfigfdtj x6 I f I ncccfyuggggrst3.pdf
HebaEng
 

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

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

More from Jason Swartz

More from Jason Swartz (10)

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

Recently uploaded

Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Recently uploaded (20)

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 

Java 8 - An Introduction by Jason Swartz