SlideShare a Scribd company logo
Functional Programming with Java 8
Victor Perepelitsky
slides: http://www.slideshare.net/victorperepelitsky/fun-with-java-8
twitter: @victor_perepel
email: victor.prp@gmail.com
git-hub: https://github.com/victor-prp/java8samples/
This presentation focuses on
What functional programming is ?
Why functional ?
How to use java 8 for functional ?
Functional Programming
In computer science, functional programming is a
programming paradigm, a style of building the structure
and elements of computer programs, that treats
computation as the evaluation of mathematical functions
and avoids changing state and mutable data. It is a declarative
programming paradigm, which means programming is
done with expressions.
Functional Programming
Functions
Avoids mutable data
Declarative
Expressions
Why Functional?
Less code
Expressive code
Correct code
It is FUN
Performance*
Functional building blocks in java 8
Function
Function reference
Lambda
Predefined Java8 functions - java.util.function
Stream API
p2
Basic Function Definition
p1*
Basic Function Usage
p1
Fun Reference - static fun
p1
Fun Reference - static fun
p1
Higher Order Function - Definition
p1*
p1
pureFun doItTwice
pureFun
pureFun
p1
java.util.function
Function<T, R> accepts T, returns R
BiFunction<T, U, R> accepts T and U, returns R
Consumer<T> accepts T, returns void
Supplier<T> accepts nothing, returns T
Predicate<T> accepts T, returns boolean
p1
Lambda - simple example
p1
Lambda - formal definition
Anonymous function (also function literal or
lambda abstraction) is a function definition that is
not bound to an identifier. Lambdas are often:
1. passed as arguments to higher-order
functions, or
2. used to construct the result of a higher-
order function that needs to return a function.
p1
Lambda
Anonymous function
Passed as argument (common usage)
In java 8 it is only syntactic sugar for function
reference
p1
Lambda
p1
Monad
In functional programming, a monad is a structure that
represents computations defined as sequences of
steps.
A type with a monad structure defines what it means to
chain operations, or nest functions of that type together
http://en.wikipedia.org/wiki/Monad_%28functional_prog
p3
In Java 8 Stream is a Monad
Chaining stream operations forming a stream pipeline
p3
menu filter sorted map collect
how to filter how to sort how to map how to collect
List<String >
Java 8 Stream
● A stream represents a sequence of elements and supports
different kind of operations to perform computations upon
those elements
● Stream operations are either intermediate or terminal.
● Intermediate operations return a stream so we can chain
multiple intermediate operations.
● Terminal operations are either void or return a non-stream
result.
p3
Let’s see it again
p3
menu
stream
filter sorted map collect
how to filter how to sort how to map how to collect
List<String >
intermediate final
Stream vs Collection
● Like a collection, a stream provides an interface to a
sequenced set of values of a specific element type
● Because collections are data structures, they’re
mostly about storing and accessing elements with
specific time/space complexities
● Streams are about expressing computations such
as filter, sorted, and map that
● Collections are about data; streams are about
computations
p2
Basic building blocks summary
Function
Function reference
Lambda
Predefined Java8 functions - java.util.function
Stream API
p2
Why Functional?
Less code
Expressive code
Correct code
It is FUN
Performance*
p2
Imperative
p5
Declarative
p5
Referential transparency
An expression is said to be referentially transparent if it can
be replaced with its value without changing the behavior
of a program (in other words, yielding a program that has
the same effects and output on the same input). The
opposite term is referential opaqueness.
http://en.wikipedia.org/wiki/Referential_transparency_(com
puter_science)p2**
Pure Function
1. Given same input, always provides same output
2. Execution does not cause observable side-
effects
1+2 = Referential Transparency
p2
Why pure functions?
Allows automatic optimizations by a compiler
Minimizes moving parts
Easy to test
Increases decoupling
p2
Pure functions - is it enough?
doIt1(..) and doIt2(..) - are pure functions?
but we wrote them in two different ways?!
p2
Imperative
p5
Declarative
p5
Refactor toward functional
Let’s combine pure functions with declarative
style
Example:
Design a system that returns top UK and US music
albums
p3
p3*
p3
p3
We want to search albums
By Name
and
By Year
p3*
version 1
p3
version 2 - pass behaviour
p3
version 3 - compact naming
p3
We want the data in specific form
Example: get years of hits albums
p3*
version 1
p3
version 2
p3
We want the data reduced
Example: get the oldest album
p3
version 1
p3
version 2
p3
So far we saw
Pure functions vs Non pure...
Declarative vs Imperative...
Functions as building blocks…
Passing behavior vs static behavior…
Function reference vs Lambda...
Additional useful techniques
Execution guarantee
Builder pattern
Composition
Currying and Partial application
p4
We want to
Guarantee correct locking
by
Write it once
and
Let it use in multiple places
p4*
Execution guarantee
p4
Builder pattern
StringBuilder is a good example:
p4
How builder pattern helps?
We already saw it - streams
We can create our own builders to introduce
fluent interfaces
p4
Can we search by multiple filters?
p4
We actually want this:
p4*
How can we achieve this?
p4
Composition
p4
Partial Application
p4
Search using match(...)
p4
Search with Partial Application V1
p4
Search with Partial Application V2
p4
Why Functional?
Less code
Expressive code
Correct code
It is FUN
Performance*
Performance - is it better in java8?
Depends:
● Pure functions may be memoized
● Stream API may be heavier
● There are more considerations….
p5
Performance - example
We have doIt1 and doIt2 using both imperative
and declarative styles
p5
Imperative
p5
Declarative
p5
Performance - example
Let’s run it 1000 times
Results:
● doIt1 took: 15ms
● doIt2 took: 63ms
p5
Declarative - improved version
p5
Performance - example
Let’s run it 1000 times
Results:
● doIt1 took: 15ms
● doIt2 took: 63ms
● doIt4 took: 30ms
p5
Can Java 8 be used for functional
programming?
Function reference
Composition
Lambda
Stream API
Currying and Partial Application*
Any problems?
We have to define an interface for a function
Special treatment for functions with primitives
Partial application is not straightforward
Typed exceptions
Guidelines
Aim to declarative
Aim to immutability
Aim to pure functions
Consider function ref over lambda
Be aware of performance when using stream
Use multi paradigm programming
Functional programming with Java 8
Functional programming with Java 8
Functional programming with Java 8
all sample are available at:
https://github.com/victor-prp/java8samples

More Related Content

What's hot (20)

PPTX
Java Lambda Expressions.pptx
SameerAhmed593310
 
PPTX
Introduction to java 8 stream api
Vladislav sidlyarevich
 
PDF
OOP and FP
Mario Fusco
 
PPTX
Java 8 lambda
Manav Prasad
 
PDF
Lambda Expressions in Java
Erhan Bagdemir
 
PPTX
Lambda Expressions in Java 8
icarter09
 
PDF
Java11 New Features
Haim Michael
 
ODP
Introduction to Java 8
Knoldus Inc.
 
PDF
JavaScript Promises
Derek Willian Stavis
 
PDF
Java 8 Default Methods
Haim Michael
 
PPSX
SOLID Principles and The Clean Architecture
Mohamed Galal
 
PDF
Java 8 features
NexThoughts Technologies
 
PPT
Java Collections Framework
Sony India Software Center
 
PPSX
Collections - Lists, Sets
Hitesh-Java
 
PDF
Java 8 Stream API. A different way to process collections.
David Gómez García
 
PPTX
Introduction to spring boot
Santosh Kumar Kar
 
PDF
Spring Boot
Pei-Tang Huang
 
PDF
Java 9 New Features
Ali BAKAN
 
PPSX
Rest api standards and best practices
Ankita Mahajan
 
PDF
Java 8 Lambda Expressions
Scott Leberknight
 
Java Lambda Expressions.pptx
SameerAhmed593310
 
Introduction to java 8 stream api
Vladislav sidlyarevich
 
OOP and FP
Mario Fusco
 
Java 8 lambda
Manav Prasad
 
Lambda Expressions in Java
Erhan Bagdemir
 
Lambda Expressions in Java 8
icarter09
 
Java11 New Features
Haim Michael
 
Introduction to Java 8
Knoldus Inc.
 
JavaScript Promises
Derek Willian Stavis
 
Java 8 Default Methods
Haim Michael
 
SOLID Principles and The Clean Architecture
Mohamed Galal
 
Java 8 features
NexThoughts Technologies
 
Java Collections Framework
Sony India Software Center
 
Collections - Lists, Sets
Hitesh-Java
 
Java 8 Stream API. A different way to process collections.
David Gómez García
 
Introduction to spring boot
Santosh Kumar Kar
 
Spring Boot
Pei-Tang Huang
 
Java 9 New Features
Ali BAKAN
 
Rest api standards and best practices
Ankita Mahajan
 
Java 8 Lambda Expressions
Scott Leberknight
 

Viewers also liked (7)

PPT
Introduction to lambda expression & lambda calculus
Kim Leo
 
PDF
Functional Programming for Busy Object Oriented Programmers
Diego Freniche Brito
 
PPTX
Intro to Functional Programming
Jordan Parmer
 
PDF
Функциональное программирование: мифы и реальность
CUSTIS
 
PDF
Павел Павлов - Scala для профессионалов - Joker 2013
ScalaNsk
 
PPTX
Insight into java 1.8, OOP VS FP
Syed Awais Mazhar Bukhari
 
PPTX
Quantum computing - Introduction
rushmila
 
Introduction to lambda expression & lambda calculus
Kim Leo
 
Functional Programming for Busy Object Oriented Programmers
Diego Freniche Brito
 
Intro to Functional Programming
Jordan Parmer
 
Функциональное программирование: мифы и реальность
CUSTIS
 
Павел Павлов - Scala для профессионалов - Joker 2013
ScalaNsk
 
Insight into java 1.8, OOP VS FP
Syed Awais Mazhar Bukhari
 
Quantum computing - Introduction
rushmila
 
Ad

Similar to Functional programming with Java 8 (20)

PDF
Fun with java 8
Victor Perepelitsky
 
PDF
Java Lambda internals with invoke dynamic
Mohit Kumar
 
PDF
Lambda.pdf
ManishWalia18
 
PDF
Apollo Server
NodeXperts
 
ODP
New c sharp3_features_(linq)_part_iv
Nico Ludwig
 
PPTX
C++ tutorial assignment - 23MTS5730.pptx
sp1312004
 
PDF
How to use Parquet as a Sasis for ETL and Analytics
DataWorks Summit
 
PDF
New c sharp3_features_(linq)_part_iv
Nico Ludwig
 
PDF
Java 8 Interview Questions and Answers PDF By ScholarHat.pdf
Scholarhat
 
PPTX
Java8 training - Class 1
Marut Singh
 
PPTX
Python Programming Basics for begginners
Abishek Purushothaman
 
PDF
ReactiveX
BADR
 
PDF
Java 8-revealed
Hamed Hatami
 
PDF
Java 8
vilniusjug
 
PDF
Smart Migration to JDK 8
Geertjan Wielenga
 
PPTX
R programming Language
SarthakBhargava7
 
PPTX
Python Programming Language
Laxman Puri
 
PPTX
Functional Programming.pptx
KarthickT28
 
PPTX
Programming paradigms Techniques_part2.pptx
ssuser5ecd1a
 
PDF
Introduction to functional programming
Thang Mai
 
Fun with java 8
Victor Perepelitsky
 
Java Lambda internals with invoke dynamic
Mohit Kumar
 
Lambda.pdf
ManishWalia18
 
Apollo Server
NodeXperts
 
New c sharp3_features_(linq)_part_iv
Nico Ludwig
 
C++ tutorial assignment - 23MTS5730.pptx
sp1312004
 
How to use Parquet as a Sasis for ETL and Analytics
DataWorks Summit
 
New c sharp3_features_(linq)_part_iv
Nico Ludwig
 
Java 8 Interview Questions and Answers PDF By ScholarHat.pdf
Scholarhat
 
Java8 training - Class 1
Marut Singh
 
Python Programming Basics for begginners
Abishek Purushothaman
 
ReactiveX
BADR
 
Java 8-revealed
Hamed Hatami
 
Java 8
vilniusjug
 
Smart Migration to JDK 8
Geertjan Wielenga
 
R programming Language
SarthakBhargava7
 
Python Programming Language
Laxman Puri
 
Functional Programming.pptx
KarthickT28
 
Programming paradigms Techniques_part2.pptx
ssuser5ecd1a
 
Introduction to functional programming
Thang Mai
 
Ad

More from LivePerson (20)

PPTX
Microservices on top of kafka
LivePerson
 
PPTX
Graph QL Introduction
LivePerson
 
PPTX
Kubernetes your tests! automation with docker on google cloud platform
LivePerson
 
PPTX
Growing into a proactive Data Platform
LivePerson
 
PPTX
Measure() or die()
LivePerson
 
PPTX
Resilience from Theory to Practice
LivePerson
 
PPTX
System Revolution- How We Did It
LivePerson
 
PPTX
Liveperson DLD 2015
LivePerson
 
PDF
Http 2: Should I care?
LivePerson
 
PDF
Mobile app real-time content modifications using websockets
LivePerson
 
PDF
Mobile SDK: Considerations & Best Practices
LivePerson
 
PPTX
Apache Avro in LivePerson [Hebrew]
LivePerson
 
PPTX
Apache Avro and Messaging at Scale in LivePerson
LivePerson
 
PPTX
Data compression in Modern Application
LivePerson
 
PPTX
Support Office Hour Webinar - LivePerson API
LivePerson
 
PPTX
SIP - Introduction to SIP Protocol
LivePerson
 
PPTX
Scalding: Reaching Efficient MapReduce
LivePerson
 
PDF
Building Enterprise Level End-To-End Monitor System with Open Source Solution...
LivePerson
 
PPTX
Introduction to Data Science
LivePerson
 
PPTX
From a Kafkaesque Story to The Promised Land at LivePerson
LivePerson
 
Microservices on top of kafka
LivePerson
 
Graph QL Introduction
LivePerson
 
Kubernetes your tests! automation with docker on google cloud platform
LivePerson
 
Growing into a proactive Data Platform
LivePerson
 
Measure() or die()
LivePerson
 
Resilience from Theory to Practice
LivePerson
 
System Revolution- How We Did It
LivePerson
 
Liveperson DLD 2015
LivePerson
 
Http 2: Should I care?
LivePerson
 
Mobile app real-time content modifications using websockets
LivePerson
 
Mobile SDK: Considerations & Best Practices
LivePerson
 
Apache Avro in LivePerson [Hebrew]
LivePerson
 
Apache Avro and Messaging at Scale in LivePerson
LivePerson
 
Data compression in Modern Application
LivePerson
 
Support Office Hour Webinar - LivePerson API
LivePerson
 
SIP - Introduction to SIP Protocol
LivePerson
 
Scalding: Reaching Efficient MapReduce
LivePerson
 
Building Enterprise Level End-To-End Monitor System with Open Source Solution...
LivePerson
 
Introduction to Data Science
LivePerson
 
From a Kafkaesque Story to The Promised Land at LivePerson
LivePerson
 

Recently uploaded (20)

PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 

Functional programming with Java 8

Editor's Notes

  • #8: part 1 - fun def