SlideShare a Scribd company logo
Fun with Java 8
Victor Perepelitsky
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_programming%29p3
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_
(computer_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
Fun with java 8
Fun with java 8
Fun with java 8
all sample are available at: https://github.
com/victor-prp/java8samples

More Related Content

PPTX
PHP = PHunctional Programming
PPTX
PPTX
FUNCTION CPU
PDF
(3) cpp procedural programming
PDF
New c sharp4_features_part_i
DOC
4. function
PDF
Functional Programming In PHP I
DOC
Lex tool manual
PHP = PHunctional Programming
FUNCTION CPU
(3) cpp procedural programming
New c sharp4_features_part_i
4. function
Functional Programming In PHP I
Lex tool manual

What's hot (20)

PDF
Modular Programming in C
PDF
Grokking Techtalk #38: Escape Analysis in Go compiler
PPT
Unit3 cspc
PPT
Unit 5 cspc
DOC
c.p function
PDF
Lecture 3 RE NFA DFA
PPT
PDF
Python functional programming
PDF
Functional converter project
DOCX
PDF
Php classes in nagpur
PPTX
PDF
Flowex - Railway Flow-Based Programming with Elixir GenStage.
PPTX
9. control statement
PPT
Cd2 [autosaved]
PPTX
Eclipse Day India 2015 - Java 8 Overview
PDF
Lexical and Parser tool for CBOOP program
PPT
Verilog Tasks and functions
PPTX
Programmable Logic Array
PPTX
Link quries
Modular Programming in C
Grokking Techtalk #38: Escape Analysis in Go compiler
Unit3 cspc
Unit 5 cspc
c.p function
Lecture 3 RE NFA DFA
Python functional programming
Functional converter project
Php classes in nagpur
Flowex - Railway Flow-Based Programming with Elixir GenStage.
9. control statement
Cd2 [autosaved]
Eclipse Day India 2015 - Java 8 Overview
Lexical and Parser tool for CBOOP program
Verilog Tasks and functions
Programmable Logic Array
Link quries
Ad

Similar to Fun with java 8 (20)

PPTX
Functional programming with Java 8
PDF
Java Lambda internals with invoke dynamic
PDF
Lambda.pdf
PDF
Apollo Server
PDF
Smart Migration to JDK 8
PDF
Java 8 Interview Questions and Answers PDF By ScholarHat.pdf
PDF
Java 8-revealed
PPTX
Java 8 streams
PDF
Java 8
PPTX
C++ tutorial assignment - 23MTS5730.pptx
PPTX
Python Programming Basics for begginners
PDF
New c sharp3_features_(linq)_part_iv
ODP
New c sharp3_features_(linq)_part_iv
PDF
Python Programming Course Presentations
PDF
Introduction to functional programming
PPTX
Functions in c++, presentation, short and sweet presentation, and details of ...
 
PDF
ReactiveX
 
PPTX
Java8 training - Class 1
PPTX
Functional programming
PPTX
R programming Language
Functional programming with Java 8
Java Lambda internals with invoke dynamic
Lambda.pdf
Apollo Server
Smart Migration to JDK 8
Java 8 Interview Questions and Answers PDF By ScholarHat.pdf
Java 8-revealed
Java 8 streams
Java 8
C++ tutorial assignment - 23MTS5730.pptx
Python Programming Basics for begginners
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
Python Programming Course Presentations
Introduction to functional programming
Functions in c++, presentation, short and sweet presentation, and details of ...
 
ReactiveX
 
Java8 training - Class 1
Functional programming
R programming Language
Ad

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Essential Infomation Tech presentation.pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Introduction to Artificial Intelligence
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
medical staffing services at VALiNTRY
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
history of c programming in notes for students .pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Odoo Companies in India – Driving Business Transformation.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Navsoft: AI-Powered Business Solutions & Custom Software Development
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Nekopoi APK 2025 free lastest update
Wondershare Filmora 15 Crack With Activation Key [2025
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Softaken Excel to vCard Converter Software.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
2025 Textile ERP Trends: SAP, Odoo & Oracle
Essential Infomation Tech presentation.pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
Introduction to Artificial Intelligence
wealthsignaloriginal-com-DS-text-... (1).pdf
medical staffing services at VALiNTRY
Adobe Illustrator 28.6 Crack My Vision of Vector Design
history of c programming in notes for students .pptx

Fun with java 8