SlideShare uma empresa Scribd logo
1 de 32
LHOUCEINE Ouhamza: Java Developer,
+10 years of IT experience.
©2020
ouhamza.web.pro@gmail.com
3
Level 1 - beginner
Lambda expressions and streams API
Imperative programming
Object-oriented programming
Functional programming
Google trends currently ranks functional programming as more popular than Object-
oriented programming
Programming perspective
Functional programming is a paradigm that allows programming using
expressions i.e. declaring functions, passing functions as arguments and
using functions as statements.
It also simplifies the creation of concurrent programs. Concurrency (or
parallel processing) is vital for improving application performance.
Functional programming
5
In functional programming, there are two very important rules:
Immutable state: It means a data object should not be changed after it is
created.
Pure function: always returns the same result for the same arguments.
Rules of functional programming
6
● Usage of functions as input to and output from other functions,
higher order functions.
● Usage of map , filter , and reduce type functions instead of looping.
● Immutable state.
● Recursion in place of looping.
● Composing functions from other functions.
● Distinguishing “pure” functions from functions with side effects.
Advantages of functional programming
7
Programming language which supports functional programming:
Programming languages
8
❏ Lambda expressions are the most talked feature of Java 8.
❏ you could think about lambda expressions as a way of supporting functional
programming in Java.
❏ To assign a lambda expression you need to have a Functional Interface.
Lambda expressions
10
Is a interface with a single abstract method
interface Welcome {
abstract void welcome(String string);
}
Functional interface
11
@FunctionalInterface annotation is optional.
Lambda expression (1)
12
Let’s say you have a list of numbers :
List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9);
If you were asked to print the numbers from the list?
for (Integer number : numbers) {
System.out.println(number);
}
Lambda expression (2)
13
We Will can use aggregate operations.
numbers.forEach(number -> System.out.println(number));
We can still improve it further using method reference
operator.
numbers.forEach(System.out::println);
Lambda expression (3)
14
Stream represents a sequence of objects from a source ( array,
collection or from database ).
With streams. You can use lambda expressions with more functions,
called aggregate operations like:
ForEach, Reduce, Filter, Map, flatMap, Collect, Count...
Streams Api
15
Intermediate operations such as filter() return a new stream on which further
processing can be done. Terminal operations, such as forEach(), mark the stream
as consumed.
A stream pipeline consists of a stream source, followed by zero or more
intermediate operations, and a terminal operation.
Method Types and Pipelines
16
When even you have a problem with null parameter think of Optional.
It’s the best solution to use Java 8 feature.
Optional class
17
18
DEMO
https://github.com/ouhamzalhss/functional-programming
19
Level 2 - advanced
Functional interface
Clean code + java 8
20
Java 8 introduced @FunctionalInterface, an interface that has
exactly one abstract method. The compiler will treat any interfaces
meeting the definition of a functional interface as a functional interface;
it means the @FunctionalInterface annotation is optional.
Functional Interface
21
In Java 8, BiFunction is a functional interface; it takes two
arguments and returns an object.
BiFunction interface
T – Type of the first argument to the function.
U – Type of the second argument to the function.
R – Type of the result of the function.
22
In Java 8, Consumer is a functional interface; it takes an argument and
returns nothing.
Consumer Interface
23
Let us see the six basic function interfaces.
Functional Interface
24
25
❏ Use Lambda Expressions: anonymous function.
❏ But you never use this: -> {.....
...}
It should be one line.
❏ Extract heavy lambda into named::methods
Clean code: Flat lambda
26
27
❏ No nullable parameters (use Optional )
❏ No optional parameters
Instead: thaOption.map(myfunction).
❏ Avoid returning null, you should throw exception or return Optional:
Clean code : parameters
28
❏ You must suffer if you work with non-runtime exceptions
❏ You can use libraries like jool,lombok : rethrow checked as
runtime exception.
Clean code: Exceptions
29
Clean code: Best practices
30
❏ Functions and type over classes.
❏ Composition over inheritance.
❏ Purity over mutability.
❏ Options over nulls.
❏ Runtime over checked exceptions.
❏ Don’t iterate use map, reduce, filter…
❏ Remove extra wrappings with .flatMap
❏ Only use a stream once.
Best practices
31
Huge thanks to you
friends
32
DEMO
https://github.com/ouhamzalhss/functional-programming-advanced

Mais conteúdo relacionado

Mais procurados

Spring Boot & Containers - Do's & Don'ts
Spring Boot & Containers - Do's & Don'tsSpring Boot & Containers - Do's & Don'ts
Spring Boot & Containers - Do's & Don'tsJulien Wittouck
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentationVan Huong
 
java 8 new features
java 8 new features java 8 new features
java 8 new features Rohit Verma
 
Manipulating Android tasks and back stack
Manipulating Android tasks and back stackManipulating Android tasks and back stack
Manipulating Android tasks and back stackRan Nachmany
 
Introduction à spring boot
Introduction à spring bootIntroduction à spring boot
Introduction à spring bootAntoine Rey
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsNewCircle Training
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8icarter09
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modelingMario Fusco
 
Collections In Java
Collections In JavaCollections In Java
Collections In JavaBinoj T E
 
Workshop Spring - Session 4 - Spring Batch
Workshop Spring -  Session 4 - Spring BatchWorkshop Spring -  Session 4 - Spring Batch
Workshop Spring - Session 4 - Spring BatchAntoine Rey
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Domenic Denicola
 

Mais procurados (20)

Java 11 to 17 : What's new !?
Java 11 to 17 : What's new !?Java 11 to 17 : What's new !?
Java 11 to 17 : What's new !?
 
Spring Boot & Containers - Do's & Don'ts
Spring Boot & Containers - Do's & Don'tsSpring Boot & Containers - Do's & Don'ts
Spring Boot & Containers - Do's & Don'ts
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
 
Hibernate jpa
Hibernate jpaHibernate jpa
Hibernate jpa
 
Manipulating Android tasks and back stack
Manipulating Android tasks and back stackManipulating Android tasks and back stack
Manipulating Android tasks and back stack
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
 
Introduction to java 8 stream api
Introduction to java 8 stream apiIntroduction to java 8 stream api
Introduction to java 8 stream api
 
Introduction à spring boot
Introduction à spring bootIntroduction à spring boot
Introduction à spring boot
 
Spring ioc
Spring iocSpring ioc
Spring ioc
 
spring-boot-fr.pdf
spring-boot-fr.pdfspring-boot-fr.pdf
spring-boot-fr.pdf
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 
String Handling
String HandlingString Handling
String Handling
 
Java 8 Streams
Java 8 StreamsJava 8 Streams
Java 8 Streams
 
Java collections
Java collectionsJava collections
Java collections
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Workshop Spring - Session 4 - Spring Batch
Workshop Spring -  Session 4 - Spring BatchWorkshop Spring -  Session 4 - Spring Batch
Workshop Spring - Session 4 - Spring Batch
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
 

Semelhante a Functional programming

Semelhante a Functional programming (20)

Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An Overview
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
java8
java8java8
java8
 
Introduction to Java 8
Introduction to Java 8Introduction to Java 8
Introduction to Java 8
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in Java
 
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
 
Colloquium Report
Colloquium ReportColloquium Report
Colloquium Report
 
Function
FunctionFunction
Function
 
Java 8
Java 8Java 8
Java 8
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Lambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APILambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream API
 
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
 
Java 8
Java 8Java 8
Java 8
 
Java 8 Intro - Core Features
Java 8 Intro - Core FeaturesJava 8 Intro - Core Features
Java 8 Intro - Core Features
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its API
 
JAVA_BASICS.ppt
JAVA_BASICS.pptJAVA_BASICS.ppt
JAVA_BASICS.ppt
 
Gude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic ServerGude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic Server
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in Java
 
Advance python programming
Advance python programming Advance python programming
Advance python programming
 

Mais de Lhouceine OUHAMZA (11)

Présentation sur internet.pptx
Présentation sur internet.pptxPrésentation sur internet.pptx
Présentation sur internet.pptx
 
WEB SERVICE SOAP, JAVA, XML, JAXWS
WEB SERVICE SOAP, JAVA, XML, JAXWSWEB SERVICE SOAP, JAVA, XML, JAXWS
WEB SERVICE SOAP, JAVA, XML, JAXWS
 
Complete Java Course
Complete Java CourseComplete Java Course
Complete Java Course
 
Prometheus and Grafana
Prometheus and GrafanaPrometheus and Grafana
Prometheus and Grafana
 
Kubernetes
KubernetesKubernetes
Kubernetes
 
Scrum course
Scrum courseScrum course
Scrum course
 
Jenkins
JenkinsJenkins
Jenkins
 
Extreme Programming (XP)
Extreme Programming (XP)Extreme Programming (XP)
Extreme Programming (XP)
 
Systemes authentification
Systemes authentificationSystemes authentification
Systemes authentification
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Presentation of framework Angular
Presentation of framework AngularPresentation of framework Angular
Presentation of framework Angular
 

Último

Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
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.pdfankushspencer015
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 

Último (20)

Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
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
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
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
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
(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
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 

Functional programming

  • 1.
  • 2. LHOUCEINE Ouhamza: Java Developer, +10 years of IT experience. ©2020 ouhamza.web.pro@gmail.com
  • 3. 3 Level 1 - beginner Lambda expressions and streams API
  • 4. Imperative programming Object-oriented programming Functional programming Google trends currently ranks functional programming as more popular than Object- oriented programming Programming perspective
  • 5. Functional programming is a paradigm that allows programming using expressions i.e. declaring functions, passing functions as arguments and using functions as statements. It also simplifies the creation of concurrent programs. Concurrency (or parallel processing) is vital for improving application performance. Functional programming 5
  • 6. In functional programming, there are two very important rules: Immutable state: It means a data object should not be changed after it is created. Pure function: always returns the same result for the same arguments. Rules of functional programming 6
  • 7. ● Usage of functions as input to and output from other functions, higher order functions. ● Usage of map , filter , and reduce type functions instead of looping. ● Immutable state. ● Recursion in place of looping. ● Composing functions from other functions. ● Distinguishing “pure” functions from functions with side effects. Advantages of functional programming 7
  • 8. Programming language which supports functional programming: Programming languages 8
  • 9.
  • 10. ❏ Lambda expressions are the most talked feature of Java 8. ❏ you could think about lambda expressions as a way of supporting functional programming in Java. ❏ To assign a lambda expression you need to have a Functional Interface. Lambda expressions 10
  • 11. Is a interface with a single abstract method interface Welcome { abstract void welcome(String string); } Functional interface 11 @FunctionalInterface annotation is optional.
  • 13. Let’s say you have a list of numbers : List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9); If you were asked to print the numbers from the list? for (Integer number : numbers) { System.out.println(number); } Lambda expression (2) 13
  • 14. We Will can use aggregate operations. numbers.forEach(number -> System.out.println(number)); We can still improve it further using method reference operator. numbers.forEach(System.out::println); Lambda expression (3) 14
  • 15. Stream represents a sequence of objects from a source ( array, collection or from database ). With streams. You can use lambda expressions with more functions, called aggregate operations like: ForEach, Reduce, Filter, Map, flatMap, Collect, Count... Streams Api 15
  • 16. Intermediate operations such as filter() return a new stream on which further processing can be done. Terminal operations, such as forEach(), mark the stream as consumed. A stream pipeline consists of a stream source, followed by zero or more intermediate operations, and a terminal operation. Method Types and Pipelines 16
  • 17. When even you have a problem with null parameter think of Optional. It’s the best solution to use Java 8 feature. Optional class 17
  • 19. 19 Level 2 - advanced Functional interface Clean code + java 8
  • 20. 20 Java 8 introduced @FunctionalInterface, an interface that has exactly one abstract method. The compiler will treat any interfaces meeting the definition of a functional interface as a functional interface; it means the @FunctionalInterface annotation is optional. Functional Interface
  • 21. 21 In Java 8, BiFunction is a functional interface; it takes two arguments and returns an object. BiFunction interface T – Type of the first argument to the function. U – Type of the second argument to the function. R – Type of the result of the function.
  • 22. 22 In Java 8, Consumer is a functional interface; it takes an argument and returns nothing. Consumer Interface
  • 23. 23 Let us see the six basic function interfaces. Functional Interface
  • 24. 24
  • 25. 25 ❏ Use Lambda Expressions: anonymous function. ❏ But you never use this: -> {..... ...} It should be one line. ❏ Extract heavy lambda into named::methods Clean code: Flat lambda
  • 26. 26
  • 27. 27 ❏ No nullable parameters (use Optional ) ❏ No optional parameters Instead: thaOption.map(myfunction). ❏ Avoid returning null, you should throw exception or return Optional: Clean code : parameters
  • 28. 28 ❏ You must suffer if you work with non-runtime exceptions ❏ You can use libraries like jool,lombok : rethrow checked as runtime exception. Clean code: Exceptions
  • 29. 29 Clean code: Best practices
  • 30. 30 ❏ Functions and type over classes. ❏ Composition over inheritance. ❏ Purity over mutability. ❏ Options over nulls. ❏ Runtime over checked exceptions. ❏ Don’t iterate use map, reduce, filter… ❏ Remove extra wrappings with .flatMap ❏ Only use a stream once. Best practices
  • 31. 31 Huge thanks to you friends

Notas do Editor

  1. 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.
  2. Pure functions only: Functional code is idempotent. A function should return values only based on the arguments passed and should not affect(side-effect) or depend on global state. Such functions always produce the same result for the same arguments.
  3. default and static methods in Interfaces. Functional Interfaces and Lambda Expressions. Java Stream API for Bulk Data Operations on Collections. Java Time API. Optional class
  4. Checked exception makes your code ugly by adding of try-catch-finally block.