SlideShare a Scribd company logo
1 of 17
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

java 8 new features
java 8 new features java 8 new features
java 8 new features Rohit Verma
 
L3 operators
L3 operatorsL3 operators
L3 operatorsteach4uin
 
Java 8 - Features Overview
Java 8 - Features OverviewJava 8 - Features Overview
Java 8 - Features OverviewSergii Stets
 
Intro To Scala
Intro To ScalaIntro To Scala
Intro To Scalachambeda
 
Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7ArraLafuente
 
Aaa ped-2- Python: Basics
Aaa ped-2- Python: BasicsAaa ped-2- Python: Basics
Aaa ped-2- Python: BasicsAminaRepo
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentationkunal kishore
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentationVan Huong
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in RubyAlex Teut
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 Bdplunkett
 

What's hot (18)

Java basic operators
Java basic operatorsJava basic operators
Java basic operators
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
 
L3 operators
L3 operatorsL3 operators
L3 operators
 
Java 8 - Features Overview
Java 8 - Features OverviewJava 8 - Features Overview
Java 8 - Features Overview
 
Intro To Scala
Intro To ScalaIntro To Scala
Intro To Scala
 
Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7
 
Aaa ped-2- Python: Basics
Aaa ped-2- Python: BasicsAaa ped-2- Python: Basics
Aaa ped-2- Python: Basics
 
05 operators
05   operators05   operators
05 operators
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
 
Chapter3
Chapter3Chapter3
Chapter3
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
Java8 features
Java8 featuresJava8 features
Java8 features
 
Code smells
Code smellsCode smells
Code smells
 
CPP07 - Scope
CPP07 - ScopeCPP07 - Scope
CPP07 - Scope
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in Ruby
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 B
 

Similar to Java 8 Functional Programming - I

Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project LambdaRahman USTA
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxBruceLee275640
 
Functional Interfaces and Method References.pptx
Functional Interfaces and Method References.pptxFunctional Interfaces and Method References.pptx
Functional Interfaces and Method References.pptxMuhammadSalman701062
 
What's new in java 8
What's new in java 8What's new in java 8
What's new in java 8Dian Aditya
 
Lambdas : Beyond The Basics
Lambdas : Beyond The BasicsLambdas : Beyond The Basics
Lambdas : Beyond The BasicsSimon Ritter
 
Java+8-New+Features.pdf
Java+8-New+Features.pdfJava+8-New+Features.pdf
Java+8-New+Features.pdfgurukanth4
 
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing JavaJDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing JavaSimon Ritter
 
Explain Delegates step by step.
Explain Delegates step by step.Explain Delegates step by step.
Explain Delegates step by step.Questpond
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Featurestechfreak
 

Similar to Java 8 Functional Programming - I (20)

Java 8 Features
Java 8 FeaturesJava 8 Features
Java 8 Features
 
Lambdas in Java 8
Lambdas in Java 8Lambdas in Java 8
Lambdas in Java 8
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project Lambda
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
 
Java 8 Intro - Core Features
Java 8 Intro - Core FeaturesJava 8 Intro - Core Features
Java 8 Intro - Core Features
 
14274730 (1).ppt
14274730 (1).ppt14274730 (1).ppt
14274730 (1).ppt
 
Functional Interfaces and Method References.pptx
Functional Interfaces and Method References.pptxFunctional Interfaces and Method References.pptx
Functional Interfaces and Method References.pptx
 
java8
java8java8
java8
 
What's new in java 8
What's new in java 8What's new in java 8
What's new in java 8
 
Lambdas and Laughs
Lambdas and LaughsLambdas and Laughs
Lambdas and Laughs
 
Java 8 streams
Java 8 streamsJava 8 streams
Java 8 streams
 
JDK8 Lambda expressions demo
JDK8 Lambda expressions demoJDK8 Lambda expressions demo
JDK8 Lambda expressions demo
 
Lambdas : Beyond The Basics
Lambdas : Beyond The BasicsLambdas : Beyond The Basics
Lambdas : Beyond The Basics
 
Java8
Java8Java8
Java8
 
Java+8-New+Features.pdf
Java+8-New+Features.pdfJava+8-New+Features.pdf
Java+8-New+Features.pdf
 
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing JavaJDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
 
Java 8 Feature Preview
Java 8 Feature PreviewJava 8 Feature Preview
Java 8 Feature Preview
 
7 Methods and Functional Programming
7  Methods and Functional Programming7  Methods and Functional Programming
7 Methods and Functional Programming
 
Explain Delegates step by step.
Explain Delegates step by step.Explain Delegates step by step.
Explain Delegates step by step.
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Features
 

Recently uploaded

GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 

Recently uploaded (20)

GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
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
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 

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 ytr.ugur@gmail.com *Please do not hesitate to contact me if you have any questions related to slides