SlideShare ist ein Scribd-Unternehmen logo
1 von 41
Functional
Programming with Java
Mehdi Einali
Advanced Programming in Java
1
2
agenda
Java versions
Default method
Functional interface
Double colon
Functional programing
Lambda expression
Optional
Streams
3
Java versions
4
Java versions-1
Version Release Date Main Features
1.0 1996 The first stable version
1.1 1997
• an extensive retooling of the AWT event
model
• Inner classes added to language
• Reflection
• I18n and Unicode Support
1.2(j2se) 1998
• Swing Framework
• Collection Framework
1.3 2000
• Java performance engine Hotspot added
• RMI added
1.4 2002
• Regular Expression support
• Exception Handling high-level api added
• Non-Blocking IO(NIO 1) added
• Java Web Start
5
Java versions-2
Version Release Date Main Features
1.5(J2SE 5)
2004-2009
22 update
• Generic Types
• Annotation
• Autoboxing
• Enumorations
• Varargs
• Foreach
1.6(J2SE 6)
2006-2013
More than 50
updates
Last one 113
• Performance improvements
• Security Improvements
• JDBC 4
J2SE 7
2011-now
Many update
Last one 101
• String in switch
• NIO 2
• Diamond in generic types
• Concurrency high level API
6
Java versions-3
Version Release Date Main Features
Java SE 8
2014-now
Last update 92
• Lambda expression or closure
(Functional Programming)
• JavaScript embedded emulator
• Annotation on Java Type
• default method in interface
Java SE 9
Scheduled
for March 2017
• Better support for multi-gigabyte heaps
• Java module system (OSGI like built-in
system)
• Reactive Streams support
• Better support for parallel systems
Java SE 10 Scheduled
for March 2018
• Object without Identity
• Support 64-bit addressable arrays
7
Default method in interfaces
8
Old interface
9
Default keyword
10
Default method
In ‘the strictest sense’, Default methods
are a step backwards because they allow
you to ‘pollute’ your interfaces with code
they provide the most elegant way to
allow backwards compatibility
The implementation will be used as default
if a concrete class does not provide
implementation for that method.
11
Notes on default
default method can only access
its arguments as interfaces do not have any
state.
Remember interface don’t have object state and its
fields are implicitly public static final
You can write static method in interface and call
it from default method
Both on them don’t depend on method arguments and
class variables rather than object state
All method declarations in an interface, including
static methods, are implicitly public, so you can
omit the public modifier.
12
Notes on default
When we extend an interface that contains
a default method, we can perform
following:
Not override the default method and will
inherit the default method.
Override the default method similar to other
methods we override in subclass.
Redeclare default method as abstract, which
force subclass to override it
13
Conflicts multiple interface
Since classes in java can implement
multiple interfaces, there could be a
situation where 2 or more interfaces has
a default
method with the same signature hence
causing conflicts as java will not know
what methods to use at a time.
14
Functional Programming
15
Java programing paradigms
Prior to JavaSE 8, Java supported three
programming paradigms
procedural programming
object-oriented programming
generic programming.
Java SE 8 adds functional programming
programming that treats computation as the
evaluation of mathematical functions and
avoids changing-state and mutable data.
Stateless functions is building block of code
16
Why functional programing?
Multi core and multi thread processor increase
every day
Functions can pass as building block of
computations.
Distributes on threads
Collections is every important data structure in
algorithms
In memory storage paradigms are increasing
Collection operations is more frequent process consuming
codes
17
collection iteration
External iteration
Developer code do iterations
What you want to accomplish collection oriented task and how
Disadvantages:
Its error prone (keep all intermediate variables consistent)
Duplicate logic
In be optimized in library level
Internal iterations
Just say what you want to accomplish collection oriented task.
18
compare
19
Functional interface
20
Functional interface
An interface that contains exactly one abstract
method (and may also contain default and static
methods).also known as single abstract method
(SAM) interfaces
Functional interfaces are used extensively in
functional programming, because they act as an
object oriented model for a function.
To ensure that your interface meet the
requirements, you should add the optional
@FunctionalInterface annotation.
java.util.function package for basic functional
interfaces
21
sample
22
23
Double colon
24
Double colon static method
Java 8 enables you to pass references of
methods or constructors via
the :: keyword
25
Double colon object method
26
Double colon constructor
27
Lambda expression
28
Lambda expressions
Functional programming is accomplished
with lambda expressions.
A lambda expression represents an
anonymous method—a shorthand notation
for implementing a functional interface,
similar to an anonymous inner class
A lambda consists of a parameter list
followed by the arrow token (->) and a
body
(parameterList) -> {statements}
(int x, int y) -> {return x + y;}
29
syntax
When the body contains only one expression, the
return keyword and curly braces may be omitted,
as in:
(x, y) -> x + y
in this case, the expression’s value is implicitly returned
When the parameter list contains
only one parameter, the parentheses may be
omitted, as in
value -> System.out.printf("%d ", value)
() -> System.out.println("Welcome to lambdas!")
30
scoping
Accessing outer scope variables from
lambda expressions is very similar to
anonymous objects.
You can access effectively final variables
from the local outer scope as well as
instance fields and static variables.
effectively final means:
Explicitly final
Implicitly final: not explicit final keyword but
acts as final variable
31
32
optional
33
optional
Optionals are not functional interfaces, instead
it's a nifty utility to prevent NullPointerException.
Optional is a simple container for a value which
may be null or non-null.
Think of a method which may return a non-null
result but sometimes return nothing. Instead of
returning null you return an Optional in Java 8.
34
sample
35
Streams
36
streams
A java.util.Stream represents a sequence of
elements on which one or more operations
can be performed
Stream operations are either intermediate
or terminal.
terminal operations return a result of a certain
type
intermediate operations return the stream itself
so you can chain multiple method calls in a row
Stream operations can either be executed
sequential or parallel.
37
sample
38
39
Parallel stream
Streams can be either sequential or
parallel.
Operations on sequential streams are
performed on a single thread while
operations on parallel streams are
performed concurrent on multiple threads.
40
sample
41
end

Weitere ähnliche Inhalte

Ähnlich wie 14274730 (1).ppt

Automatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesAutomatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesRaffi Khatchadourian
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in JavaGurpreet singh
 
What is Java Technology (An introduction with comparision of .net coding)
What is Java Technology (An introduction with comparision of .net coding)What is Java Technology (An introduction with comparision of .net coding)
What is Java Technology (An introduction with comparision of .net coding)Shaharyar khan
 
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMUAutomated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMURaffi Khatchadourian
 
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
 
Java+8-New+Features.pdf
Java+8-New+Features.pdfJava+8-New+Features.pdf
Java+8-New+Features.pdfgurukanth4
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8 Bansilal Haudakari
 
Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New featuresSon Nguyen
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An OverviewIndrajit Das
 
Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3Simon Ritter
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Raffi Khatchadourian
 
Lambda Expressions Java 8 Features usage
Lambda Expressions  Java 8 Features usageLambda Expressions  Java 8 Features usage
Lambda Expressions Java 8 Features usageAsmaShaikh478737
 

Ähnlich wie 14274730 (1).ppt (20)

Automatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesAutomatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to Interfaces
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
 
Lambdas in Java 8
Lambdas in Java 8Lambdas in Java 8
Lambdas in Java 8
 
What is Java Technology (An introduction with comparision of .net coding)
What is Java Technology (An introduction with comparision of .net coding)What is Java Technology (An introduction with comparision of .net coding)
What is Java Technology (An introduction with comparision of .net coding)
 
Csci360 20 (1)
Csci360 20 (1)Csci360 20 (1)
Csci360 20 (1)
 
Csci360 20
Csci360 20Csci360 20
Csci360 20
 
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMUAutomated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
 
Java 8 Overview
Java 8 OverviewJava 8 Overview
Java 8 Overview
 
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
 
Java+8-New+Features.pdf
Java+8-New+Features.pdfJava+8-New+Features.pdf
Java+8-New+Features.pdf
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8
 
Java 8
Java 8Java 8
Java 8
 
Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New features
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An Overview
 
Core_Java_Interview.pdf
Core_Java_Interview.pdfCore_Java_Interview.pdf
Core_Java_Interview.pdf
 
2014 java functional
2014 java functional2014 java functional
2014 java functional
 
Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3Lambdas and-streams-s ritter-v3
Lambdas and-streams-s ritter-v3
 
JAVA_BASICS.ppt
JAVA_BASICS.pptJAVA_BASICS.ppt
JAVA_BASICS.ppt
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
 
Lambda Expressions Java 8 Features usage
Lambda Expressions  Java 8 Features usageLambda Expressions  Java 8 Features usage
Lambda Expressions Java 8 Features usage
 

Kürzlich hochgeladen

A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...Akihiro Suda
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencessuser9e7c64
 
Lecture # 8 software design and architecture (SDA).ppt
Lecture # 8 software design and architecture (SDA).pptLecture # 8 software design and architecture (SDA).ppt
Lecture # 8 software design and architecture (SDA).pptesrabilgic2
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxRTS corp
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfYashikaSharma391629
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 

Kürzlich hochgeladen (20)

A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conference
 
Lecture # 8 software design and architecture (SDA).ppt
Lecture # 8 software design and architecture (SDA).pptLecture # 8 software design and architecture (SDA).ppt
Lecture # 8 software design and architecture (SDA).ppt
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 

14274730 (1).ppt

  • 1. Functional Programming with Java Mehdi Einali Advanced Programming in Java 1
  • 2. 2 agenda Java versions Default method Functional interface Double colon Functional programing Lambda expression Optional Streams
  • 4. 4 Java versions-1 Version Release Date Main Features 1.0 1996 The first stable version 1.1 1997 • an extensive retooling of the AWT event model • Inner classes added to language • Reflection • I18n and Unicode Support 1.2(j2se) 1998 • Swing Framework • Collection Framework 1.3 2000 • Java performance engine Hotspot added • RMI added 1.4 2002 • Regular Expression support • Exception Handling high-level api added • Non-Blocking IO(NIO 1) added • Java Web Start
  • 5. 5 Java versions-2 Version Release Date Main Features 1.5(J2SE 5) 2004-2009 22 update • Generic Types • Annotation • Autoboxing • Enumorations • Varargs • Foreach 1.6(J2SE 6) 2006-2013 More than 50 updates Last one 113 • Performance improvements • Security Improvements • JDBC 4 J2SE 7 2011-now Many update Last one 101 • String in switch • NIO 2 • Diamond in generic types • Concurrency high level API
  • 6. 6 Java versions-3 Version Release Date Main Features Java SE 8 2014-now Last update 92 • Lambda expression or closure (Functional Programming) • JavaScript embedded emulator • Annotation on Java Type • default method in interface Java SE 9 Scheduled for March 2017 • Better support for multi-gigabyte heaps • Java module system (OSGI like built-in system) • Reactive Streams support • Better support for parallel systems Java SE 10 Scheduled for March 2018 • Object without Identity • Support 64-bit addressable arrays
  • 7. 7 Default method in interfaces
  • 10. 10 Default method In ‘the strictest sense’, Default methods are a step backwards because they allow you to ‘pollute’ your interfaces with code they provide the most elegant way to allow backwards compatibility The implementation will be used as default if a concrete class does not provide implementation for that method.
  • 11. 11 Notes on default default method can only access its arguments as interfaces do not have any state. Remember interface don’t have object state and its fields are implicitly public static final You can write static method in interface and call it from default method Both on them don’t depend on method arguments and class variables rather than object state All method declarations in an interface, including static methods, are implicitly public, so you can omit the public modifier.
  • 12. 12 Notes on default When we extend an interface that contains a default method, we can perform following: Not override the default method and will inherit the default method. Override the default method similar to other methods we override in subclass. Redeclare default method as abstract, which force subclass to override it
  • 13. 13 Conflicts multiple interface Since classes in java can implement multiple interfaces, there could be a situation where 2 or more interfaces has a default method with the same signature hence causing conflicts as java will not know what methods to use at a time.
  • 15. 15 Java programing paradigms Prior to JavaSE 8, Java supported three programming paradigms procedural programming object-oriented programming generic programming. Java SE 8 adds functional programming programming that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. Stateless functions is building block of code
  • 16. 16 Why functional programing? Multi core and multi thread processor increase every day Functions can pass as building block of computations. Distributes on threads Collections is every important data structure in algorithms In memory storage paradigms are increasing Collection operations is more frequent process consuming codes
  • 17. 17 collection iteration External iteration Developer code do iterations What you want to accomplish collection oriented task and how Disadvantages: Its error prone (keep all intermediate variables consistent) Duplicate logic In be optimized in library level Internal iterations Just say what you want to accomplish collection oriented task.
  • 20. 20 Functional interface An interface that contains exactly one abstract method (and may also contain default and static methods).also known as single abstract method (SAM) interfaces Functional interfaces are used extensively in functional programming, because they act as an object oriented model for a function. To ensure that your interface meet the requirements, you should add the optional @FunctionalInterface annotation. java.util.function package for basic functional interfaces
  • 22. 22
  • 24. 24 Double colon static method Java 8 enables you to pass references of methods or constructors via the :: keyword
  • 28. 28 Lambda expressions Functional programming is accomplished with lambda expressions. A lambda expression represents an anonymous method—a shorthand notation for implementing a functional interface, similar to an anonymous inner class A lambda consists of a parameter list followed by the arrow token (->) and a body (parameterList) -> {statements} (int x, int y) -> {return x + y;}
  • 29. 29 syntax When the body contains only one expression, the return keyword and curly braces may be omitted, as in: (x, y) -> x + y in this case, the expression’s value is implicitly returned When the parameter list contains only one parameter, the parentheses may be omitted, as in value -> System.out.printf("%d ", value) () -> System.out.println("Welcome to lambdas!")
  • 30. 30 scoping Accessing outer scope variables from lambda expressions is very similar to anonymous objects. You can access effectively final variables from the local outer scope as well as instance fields and static variables. effectively final means: Explicitly final Implicitly final: not explicit final keyword but acts as final variable
  • 31. 31
  • 33. 33 optional Optionals are not functional interfaces, instead it's a nifty utility to prevent NullPointerException. Optional is a simple container for a value which may be null or non-null. Think of a method which may return a non-null result but sometimes return nothing. Instead of returning null you return an Optional in Java 8.
  • 36. 36 streams A java.util.Stream represents a sequence of elements on which one or more operations can be performed Stream operations are either intermediate or terminal. terminal operations return a result of a certain type intermediate operations return the stream itself so you can chain multiple method calls in a row Stream operations can either be executed sequential or parallel.
  • 38. 38
  • 39. 39 Parallel stream Streams can be either sequential or parallel. Operations on sequential streams are performed on a single thread while operations on parallel streams are performed concurrent on multiple threads.