SlideShare a Scribd company logo
Functional Programming
in Java 8 - I
Context
✓ Lambda Expressions
✓ Built-in Functional Interfaces
✓ Optionals
Writing Simple Lambdas
—> A lambda expression is a block of code that gets passed
around. A lambda expression can be thought as a continued
anonymous method. A lambda expression has parameters and a
body just like regular methods do, but it doesn’t have a name like
a real method.
Lambda Syntax
• The concise way The verbose way
They are all valid
Which Variables Can My
Lambda Access?
• Lambdas are allowed to access instance and static variables.
• Method parameters and local variables could be used if they
are not assigned new values. Finals and effectively final
variables.
Functional Interface
• A Functional Interface
has exactly one
abstract method.
• It could have any
numbers of default
methods.
Built-In Functional Interfaces
• Java 8 provides built-in functional interfaces in java.util.function
package.
Interface Parameters Return Type Method
Supplier<T> 0 T get
Consumer<T> 1(T) void accept
BiConsumer<T,U> 2(T,U) void accept
Predicate<T> 1(T) boolean test
BiPredicate<T> 2(T,U) boolean test
UnaryOperator<T> 1(T) T apply
BinaryOperator<T,T> 2(T,T) T apply
Function<T,R> 1(T) R apply
BiFunction<T,U,R> 2(T,U) R apply
Implementing Supplier
• Supplier interfaced is used when you want to
generate or supply values without taking
parameters.
• Related method name is get().
Implementing Consumers
• Consumer interfaces used when you want to do
something with parameter but not return
anything.
• Related method name is accept().
Implementing Predicates
• Predicates are often used for distinguishing
elements.
• Related method name is test(). And() and Negate()
are useful default methods.
Implementing Functions
• Functions take parameters and return values
possibly in different type.
• Related method name is apply().
Implementing Operators
• UnaryOperator and BinaryOperator are types of
specialised functions.
• Related method name is apply().
Implementing Custom
Functional Interface
• Creating our own custom interface is an option.
Optionals
• An optional could have a value inside or not.
• Optional.empty() Optional.of()
100.0
Optional Methods
Method Optional Empty
Optional with
Value
get() Throws an Exception Returns value
ifPresent() Returns false Returns true
ifPresent(Consumer cons) Does nothing Calls consumer
orElse(T other)
Returns other
parameter
Returns value
orElseThrow(Supplier s)
Throws supplied
exception
Returns value
orElseGet(Supplier s)
Returns supplied
value
Returns value
Optional Methods
• Returning an optional instead of null is more clear
statement that tells there might be no value.
• Usage of sample methods on an empty optional
Thank You
References
OCA Oracle Certified Associate Java SE 8 Programmer I Study Guide
OCP Oracle Certified Professional Java SE 8 Programmer II Study Guide
You may reach piece of codes in the slides on Github page
https://github.com/AngelThread/Java8/tree/master/Functional_Programming_in_Java_8
Ugur Yeter
ytr.ugur@gmail.com
*Please do not hesitate to contact me if you have any questions related to slides

More Related Content

What's hot (18)

Java basic operators
Java basic operatorsJava basic operators
Java basic operators
Emmanuel Alimpolos
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
Rohit Verma
 
L3 operators
L3 operatorsL3 operators
L3 operators
teach4uin
 
Java 8 - Features Overview
Java 8 - Features OverviewJava 8 - Features Overview
Java 8 - Features Overview
Sergii Stets
 
Intro To Scala
Intro To ScalaIntro To Scala
Intro To Scala
chambeda
 
Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7
ArraLafuente
 
Aaa ped-2- Python: Basics
Aaa ped-2- Python: BasicsAaa ped-2- Python: Basics
Aaa ped-2- Python: Basics
AminaRepo
 
05 operators
05   operators05   operators
05 operators
dhrubo kayal
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
Shakti Singh Rathore
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
kunal kishore
 
Chapter3
Chapter3Chapter3
Chapter3
Subhadip Pal
 
Operators in java
Operators in javaOperators in java
Operators in java
Muthukumaran Subramanian
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Van Huong
 
Java8 features
Java8 featuresJava8 features
Java8 features
Elias Hasnat
 
Code smells
Code smellsCode smells
Code smells
Narayann Swaami
 
CPP07 - Scope
CPP07 - ScopeCPP07 - Scope
CPP07 - Scope
Michael Heron
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in Ruby
Alex Teut
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 B
dplunkett
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
Rohit Verma
 
L3 operators
L3 operatorsL3 operators
L3 operators
teach4uin
 
Java 8 - Features Overview
Java 8 - Features OverviewJava 8 - Features Overview
Java 8 - Features Overview
Sergii Stets
 
Intro To Scala
Intro To ScalaIntro To Scala
Intro To Scala
chambeda
 
Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7
ArraLafuente
 
Aaa ped-2- Python: Basics
Aaa ped-2- Python: BasicsAaa ped-2- Python: Basics
Aaa ped-2- Python: Basics
AminaRepo
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
kunal kishore
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Van Huong
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in Ruby
Alex Teut
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 B
dplunkett
 

Similar to Java 8 Functional Programming - I (20)

Insight into java 1.8, OOP VS FP
Insight into java 1.8, OOP VS FPInsight into java 1.8, OOP VS FP
Insight into java 1.8, OOP VS FP
Syed Awais Mazhar Bukhari
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
icarter09
 
Lambdas in Java 8
Lambdas in Java 8Lambdas in Java 8
Lambdas in Java 8
Tobias Coetzee
 
14274730 (1).ppt
14274730 (1).ppt14274730 (1).ppt
14274730 (1).ppt
aptechaligarh
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An Overview
Indrajit Das
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 
Intro to java 8
Intro to java 8Intro to java 8
Intro to java 8
John Godoi
 
Week-1..................................
Week-1..................................Week-1..................................
Week-1..................................
kmjanani05
 
Introduction to functional programming with java 8
Introduction to functional programming with java 8Introduction to functional programming with java 8
Introduction to functional programming with java 8
JavaBrahman
 
Functional programming
Functional programmingFunctional programming
Functional programming
Lhouceine OUHAMZA
 
Java 8 Interview Questions and Answers PDF By ScholarHat.pdf
Java 8 Interview Questions and Answers PDF By ScholarHat.pdfJava 8 Interview Questions and Answers PDF By ScholarHat.pdf
Java 8 Interview Questions and Answers PDF By ScholarHat.pdf
Scholarhat
 
Lambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APILambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream API
Prabu U
 
Java8
Java8Java8
Java8
Freeman Zhang
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
LivePerson
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
Ganesh Samarthyam
 
Fun with java 8
Fun with java 8Fun with java 8
Fun with java 8
Victor Perepelitsky
 
Java 8 Intro - Core Features
Java 8 Intro - Core FeaturesJava 8 Intro - Core Features
Java 8 Intro - Core Features
GlobalLogic Ukraine
 
Java 8-revealed
Java 8-revealedJava 8-revealed
Java 8-revealed
Hamed Hatami
 
Lambda Functions in Java 8
Lambda Functions in Java 8Lambda Functions in Java 8
Lambda Functions in Java 8
Ganesh Samarthyam
 
java8
java8java8
java8
Arik Abulafya
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
icarter09
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An Overview
Indrajit Das
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 
Intro to java 8
Intro to java 8Intro to java 8
Intro to java 8
John Godoi
 
Week-1..................................
Week-1..................................Week-1..................................
Week-1..................................
kmjanani05
 
Introduction to functional programming with java 8
Introduction to functional programming with java 8Introduction to functional programming with java 8
Introduction to functional programming with java 8
JavaBrahman
 
Java 8 Interview Questions and Answers PDF By ScholarHat.pdf
Java 8 Interview Questions and Answers PDF By ScholarHat.pdfJava 8 Interview Questions and Answers PDF By ScholarHat.pdf
Java 8 Interview Questions and Answers PDF By ScholarHat.pdf
Scholarhat
 
Lambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APILambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream API
Prabu U
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
LivePerson
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
Ganesh Samarthyam
 
Ad

Recently uploaded (20)

IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
IntroSlides-June-GDG-Cloud-Munich community [email protected]
IntroSlides-June-GDG-Cloud-Munich community gathering@Netlight.pdfIntroSlides-June-GDG-Cloud-Munich community gathering@Netlight.pdf
IntroSlides-June-GDG-Cloud-Munich community [email protected]
Luiz Carneiro
 
Flow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docxFlow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docx
rifka575530
 
New Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docxNew Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docx
misheetasah
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank
Guru Nanak Technical Institutions
 
May 2025: Top 10 Read Articles Advanced Information Technology
May 2025: Top 10 Read Articles Advanced Information TechnologyMay 2025: Top 10 Read Articles Advanced Information Technology
May 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
Structure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS pptStructure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS ppt
Wahajch
 
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdfIrja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptxDevelopment of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journeyRigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
First Review PPT gfinal gyft ftu liu yrfut go
First Review PPT gfinal gyft  ftu liu yrfut goFirst Review PPT gfinal gyft  ftu liu yrfut go
First Review PPT gfinal gyft ftu liu yrfut go
Sowndarya6
 
Artificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowyArtificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowy
dominikamizerska1
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
ijfcstjournal
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
11th International Conference on Data Mining (DaMi 2025)
11th International Conference on Data Mining (DaMi 2025)11th International Conference on Data Mining (DaMi 2025)
11th International Conference on Data Mining (DaMi 2025)
kjim477n
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdfHow Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
Flow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docxFlow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docx
rifka575530
 
New Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docxNew Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docx
misheetasah
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
May 2025: Top 10 Read Articles Advanced Information Technology
May 2025: Top 10 Read Articles Advanced Information TechnologyMay 2025: Top 10 Read Articles Advanced Information Technology
May 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
Structure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS pptStructure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS ppt
Wahajch
 
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdfIrja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptxDevelopment of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journeyRigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
First Review PPT gfinal gyft ftu liu yrfut go
First Review PPT gfinal gyft  ftu liu yrfut goFirst Review PPT gfinal gyft  ftu liu yrfut go
First Review PPT gfinal gyft ftu liu yrfut go
Sowndarya6
 
Artificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowyArtificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowy
dominikamizerska1
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
ijfcstjournal
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
11th International Conference on Data Mining (DaMi 2025)
11th International Conference on Data Mining (DaMi 2025)11th International Conference on Data Mining (DaMi 2025)
11th International Conference on Data Mining (DaMi 2025)
kjim477n
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdfHow Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
Ad

Java 8 Functional Programming - I

  • 2. Context ✓ Lambda Expressions ✓ Built-in Functional Interfaces ✓ Optionals
  • 3. Writing Simple Lambdas —> A lambda expression is a block of code that gets passed around. A lambda expression can be thought as a continued anonymous method. A lambda expression has parameters and a body just like regular methods do, but it doesn’t have a name like a real method.
  • 4. Lambda Syntax • The concise way The verbose way They are all valid
  • 5. Which Variables Can My Lambda Access? • Lambdas are allowed to access instance and static variables. • Method parameters and local variables could be used if they are not assigned new values. Finals and effectively final variables.
  • 6. Functional Interface • A Functional Interface has exactly one abstract method. • It could have any numbers of default methods.
  • 7. Built-In Functional Interfaces • Java 8 provides built-in functional interfaces in java.util.function package. Interface Parameters Return Type Method Supplier<T> 0 T get Consumer<T> 1(T) void accept BiConsumer<T,U> 2(T,U) void accept Predicate<T> 1(T) boolean test BiPredicate<T> 2(T,U) boolean test UnaryOperator<T> 1(T) T apply BinaryOperator<T,T> 2(T,T) T apply Function<T,R> 1(T) R apply BiFunction<T,U,R> 2(T,U) R apply
  • 8. Implementing Supplier • Supplier interfaced is used when you want to generate or supply values without taking parameters. • Related method name is get().
  • 9. Implementing Consumers • Consumer interfaces used when you want to do something with parameter but not return anything. • Related method name is accept().
  • 10. Implementing Predicates • Predicates are often used for distinguishing elements. • Related method name is test(). And() and Negate() are useful default methods.
  • 11. Implementing Functions • Functions take parameters and return values possibly in different type. • Related method name is apply().
  • 12. Implementing Operators • UnaryOperator and BinaryOperator are types of specialised functions. • Related method name is apply().
  • 13. Implementing Custom Functional Interface • Creating our own custom interface is an option.
  • 14. Optionals • An optional could have a value inside or not. • Optional.empty() Optional.of() 100.0
  • 15. Optional Methods Method Optional Empty Optional with Value get() Throws an Exception Returns value ifPresent() Returns false Returns true ifPresent(Consumer cons) Does nothing Calls consumer orElse(T other) Returns other parameter Returns value orElseThrow(Supplier s) Throws supplied exception Returns value orElseGet(Supplier s) Returns supplied value Returns value
  • 16. Optional Methods • Returning an optional instead of null is more clear statement that tells there might be no value. • Usage of sample methods on an empty optional
  • 17. Thank You References OCA Oracle Certified Associate Java SE 8 Programmer I Study Guide OCP Oracle Certified Professional Java SE 8 Programmer II Study Guide You may reach piece of codes in the slides on Github page https://github.com/AngelThread/Java8/tree/master/Functional_Programming_in_Java_8 Ugur Yeter [email protected] *Please do not hesitate to contact me if you have any questions related to slides