SlideShare uma empresa Scribd logo
Reactive Systems
Functional Interfaces &
Method References
MIKE.CONNOLLY@LIT.IE
Java Lambdas
What we will cover
•java.util.function Interfaces
•Method References
Functional Interfaces
•What is the type of a lambda expression?
• We have used an interface with a single abstract method to
represent a method
• We call this kind of interface a functional interface
•We have seen Functional Interfaces and how to create them
• If we had ready made lambda types (like Strings or Doubles)
we would reduce our code even more
•Java introduced lots of ready made function Interfaces for
just this purpose!
Java.util.function
•Rather than creating an interface for each specific lambda we want to create Java provide a
library of ready to go lambda interfaces
• https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
We will now look at Function<>, Predicate<> and Consumer<> Function Interfaces
Java.util.function.Function
•Function<T,R> Represents a function
that accepts one argument of type T and
produces a result of type R
•The apply(T t) method applies this function
to the given argument.
Java.util.function.Predicate
•Predicate<T> Represents a predicate
(boolean-valued function) of one
argument.
•The test(T t) method evaluates this
predicate on the given argument.
Java.util.function.Predicate
•or(Predicate<? super T> other)
Returns a composed predicate that represents a
short-circuiting logical OR of this predicate and
another
and(Predicate<? super T> other) Returns a
composed predicate that represents a short-
circuiting logical AND of this predicate and another
negate() Returns a predicate that represents the
logical negation of this predicate.
Java.util.function.BiFunction
•BiFunction<T,U,R>
•Represents a function that accepts two
arguments and produces a result
• T - the type of the first argument to the function
• U - the type of the second argument to the
function
• R - the type of the result of the function
•apply(T t, U u)
• Applies this function to the given arguments.
Java.util.function.Consumer
•Consumer<T>
•Represents an operation that accepts a
single input argument and returns no
result.
•accept(T t)
• Performs this operation on the
given argument.
• Returns void
Functional Composition
•The andThen() method can be used to compose new functions from existing
ones.
•It creates a new Function from the Function that called andThen() and the
Function passed to the andThen()
•A Function composed with andThen() will first call the Function that called
andThen(), then it will call the Function passed to andThen().
•Consumer & BiFunction also have andThen() methods.
Java Method References
•We use lambda expressions to create anonymous methods
•Sometimes lambdas only call an existing method and do nothing else
•It is easier to understand the code if we refer to the existing method by name
•Method references are compact, easy-to-read lambda expressions for methods that
already have a name
•References:
•https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
•Java Brains - https://youtu.be/lwwIZuwYmNI
Java Method References
Kind Syntax Examples
Reference to a static method
ContainingClass::staticMethodName Person::compareByAge
Reference to an instance method of a
particular object
containingObject::instanceMethodName
myApp::appendStrings2
Reference to an instance method of an
arbitrary object of a particular type
ContainingType::methodName
String::compareToIgnoreCase
Reference to a constructor ClassName::new
HashSet::new
Java Method References Example
•Thread accepts a Runnable as an argument.
Runnable is a functional interface whose single
abstract method is run (zero parameters and
void return type)
•When an instance of Thread calls it’s start
method, the Runnable run method runs in a new
thread of execution.
•Here we create a lambda that just calls the
System.out.println() function
•We see that the method reference in thread2
has the same behaviour as calling the method in
thread1
Java Method References
Example
•Here we create a list of Car objects and a
Predicate similar to earlier examples
• The perform conditionally method accepts a
list a predicate and a Consumer
• If any element from the list satisfies the
Predicate the Consumer operates on it.
•In this case our Consumer is defined, using
a method reference, as the block of code
associated with System.out.println
Functional Interfaces and Method References.pptx

Mais conteúdo relacionado

Semelhante a Functional Interfaces and Method References.pptx

Week-1..................................
Week-1..................................Week-1..................................
Week-1..................................
kmjanani05
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014
Nayden Gochev
 
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Nayden Gochev
 

Semelhante a Functional Interfaces and Method References.pptx (20)

Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project Lambda
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
 
Java 8 Functional Programming - I
Java 8 Functional Programming - IJava 8 Functional Programming - I
Java 8 Functional Programming - I
 
Week-1..................................
Week-1..................................Week-1..................................
Week-1..................................
 
Java8 features
Java8 featuresJava8 features
Java8 features
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
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
 
Stream Api.pdf
Stream Api.pdfStream Api.pdf
Stream Api.pdf
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
 
Lambda expressions java8 - yousry
Lambda expressions java8 - yousryLambda expressions java8 - yousry
Lambda expressions java8 - yousry
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An Overview
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014
 
14274730 (1).ppt
14274730 (1).ppt14274730 (1).ppt
14274730 (1).ppt
 
Lambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APILambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream API
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8
 
Java 8 - Features Overview
Java 8 - Features OverviewJava 8 - Features Overview
Java 8 - Features Overview
 
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
 

Último

Exomoons & Exorings with the Habitable Worlds Observatory I: On the Detection...
Exomoons & Exorings with the Habitable Worlds Observatory I: On the Detection...Exomoons & Exorings with the Habitable Worlds Observatory I: On the Detection...
Exomoons & Exorings with the Habitable Worlds Observatory I: On the Detection...
Sérgio Sacani
 
The solar dynamo begins near the surface
The solar dynamo begins near the surfaceThe solar dynamo begins near the surface
The solar dynamo begins near the surface
Sérgio Sacani
 
Mitosis...............................pptx
Mitosis...............................pptxMitosis...............................pptx
Mitosis...............................pptx
Cherry
 
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdfPests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
PirithiRaju
 
Continuum emission from within the plunging region of black hole discs
Continuum emission from within the plunging region of black hole discsContinuum emission from within the plunging region of black hole discs
Continuum emission from within the plunging region of black hole discs
Sérgio Sacani
 
Jet reorientation in central galaxies of clusters and groups: insights from V...
Jet reorientation in central galaxies of clusters and groups: insights from V...Jet reorientation in central galaxies of clusters and groups: insights from V...
Jet reorientation in central galaxies of clusters and groups: insights from V...
Sérgio Sacani
 

Último (20)

A Giant Impact Origin for the First Subduction on Earth
A Giant Impact Origin for the First Subduction on EarthA Giant Impact Origin for the First Subduction on Earth
A Giant Impact Origin for the First Subduction on Earth
 
Plasma proteins_ Dr.Muralinath_Dr.c. kalyan
Plasma proteins_ Dr.Muralinath_Dr.c. kalyanPlasma proteins_ Dr.Muralinath_Dr.c. kalyan
Plasma proteins_ Dr.Muralinath_Dr.c. kalyan
 
Structural annotation................pptx
Structural annotation................pptxStructural annotation................pptx
Structural annotation................pptx
 
Emergent ribozyme behaviors in oxychlorine brines indicate a unique niche for...
Emergent ribozyme behaviors in oxychlorine brines indicate a unique niche for...Emergent ribozyme behaviors in oxychlorine brines indicate a unique niche for...
Emergent ribozyme behaviors in oxychlorine brines indicate a unique niche for...
 
Exomoons & Exorings with the Habitable Worlds Observatory I: On the Detection...
Exomoons & Exorings with the Habitable Worlds Observatory I: On the Detection...Exomoons & Exorings with the Habitable Worlds Observatory I: On the Detection...
Exomoons & Exorings with the Habitable Worlds Observatory I: On the Detection...
 
WASP-69b’s Escaping Envelope Is Confined to a Tail Extending at Least 7 Rp
WASP-69b’s Escaping Envelope Is Confined to a Tail Extending at Least 7 RpWASP-69b’s Escaping Envelope Is Confined to a Tail Extending at Least 7 Rp
WASP-69b’s Escaping Envelope Is Confined to a Tail Extending at Least 7 Rp
 
INSIGHT Partner Profile: Tampere University
INSIGHT Partner Profile: Tampere UniversityINSIGHT Partner Profile: Tampere University
INSIGHT Partner Profile: Tampere University
 
Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...
Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...
Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...
 
GBSN - Microbiology (Unit 6) Human and Microbial interaction
GBSN - Microbiology (Unit 6) Human and Microbial interactionGBSN - Microbiology (Unit 6) Human and Microbial interaction
GBSN - Microbiology (Unit 6) Human and Microbial interaction
 
The solar dynamo begins near the surface
The solar dynamo begins near the surfaceThe solar dynamo begins near the surface
The solar dynamo begins near the surface
 
B lymphocytes, Receptors, Maturation and Activation
B lymphocytes, Receptors, Maturation and ActivationB lymphocytes, Receptors, Maturation and Activation
B lymphocytes, Receptors, Maturation and Activation
 
SCHISTOSOMA HEAMATOBIUM life cycle .pdf
SCHISTOSOMA HEAMATOBIUM life cycle  .pdfSCHISTOSOMA HEAMATOBIUM life cycle  .pdf
SCHISTOSOMA HEAMATOBIUM life cycle .pdf
 
Mitosis...............................pptx
Mitosis...............................pptxMitosis...............................pptx
Mitosis...............................pptx
 
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdfPests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
 
GBSN - Microbiology (Unit 7) Microbiology in Everyday Life
GBSN - Microbiology (Unit 7) Microbiology in Everyday LifeGBSN - Microbiology (Unit 7) Microbiology in Everyday Life
GBSN - Microbiology (Unit 7) Microbiology in Everyday Life
 
Continuum emission from within the plunging region of black hole discs
Continuum emission from within the plunging region of black hole discsContinuum emission from within the plunging region of black hole discs
Continuum emission from within the plunging region of black hole discs
 
GBSN - Biochemistry (Unit 4) Chemistry of Carbohydrates
GBSN - Biochemistry (Unit 4) Chemistry of CarbohydratesGBSN - Biochemistry (Unit 4) Chemistry of Carbohydrates
GBSN - Biochemistry (Unit 4) Chemistry of Carbohydrates
 
Application of Mass Spectrometry In Biotechnology
Application of Mass Spectrometry In BiotechnologyApplication of Mass Spectrometry In Biotechnology
Application of Mass Spectrometry In Biotechnology
 
METHODS OF TRANSCRIPTOME ANALYSIS....pptx
METHODS OF TRANSCRIPTOME ANALYSIS....pptxMETHODS OF TRANSCRIPTOME ANALYSIS....pptx
METHODS OF TRANSCRIPTOME ANALYSIS....pptx
 
Jet reorientation in central galaxies of clusters and groups: insights from V...
Jet reorientation in central galaxies of clusters and groups: insights from V...Jet reorientation in central galaxies of clusters and groups: insights from V...
Jet reorientation in central galaxies of clusters and groups: insights from V...
 

Functional Interfaces and Method References.pptx

  • 1. Reactive Systems Functional Interfaces & Method References MIKE.CONNOLLY@LIT.IE
  • 2. Java Lambdas What we will cover •java.util.function Interfaces •Method References
  • 3. Functional Interfaces •What is the type of a lambda expression? • We have used an interface with a single abstract method to represent a method • We call this kind of interface a functional interface •We have seen Functional Interfaces and how to create them • If we had ready made lambda types (like Strings or Doubles) we would reduce our code even more •Java introduced lots of ready made function Interfaces for just this purpose!
  • 4. Java.util.function •Rather than creating an interface for each specific lambda we want to create Java provide a library of ready to go lambda interfaces • https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html We will now look at Function<>, Predicate<> and Consumer<> Function Interfaces
  • 5. Java.util.function.Function •Function<T,R> Represents a function that accepts one argument of type T and produces a result of type R •The apply(T t) method applies this function to the given argument.
  • 6. Java.util.function.Predicate •Predicate<T> Represents a predicate (boolean-valued function) of one argument. •The test(T t) method evaluates this predicate on the given argument.
  • 7. Java.util.function.Predicate •or(Predicate<? super T> other) Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another and(Predicate<? super T> other) Returns a composed predicate that represents a short- circuiting logical AND of this predicate and another negate() Returns a predicate that represents the logical negation of this predicate.
  • 8. Java.util.function.BiFunction •BiFunction<T,U,R> •Represents a function that accepts two arguments and produces a result • T - the type of the first argument to the function • U - the type of the second argument to the function • R - the type of the result of the function •apply(T t, U u) • Applies this function to the given arguments.
  • 9. Java.util.function.Consumer •Consumer<T> •Represents an operation that accepts a single input argument and returns no result. •accept(T t) • Performs this operation on the given argument. • Returns void
  • 10. Functional Composition •The andThen() method can be used to compose new functions from existing ones. •It creates a new Function from the Function that called andThen() and the Function passed to the andThen() •A Function composed with andThen() will first call the Function that called andThen(), then it will call the Function passed to andThen(). •Consumer & BiFunction also have andThen() methods.
  • 11. Java Method References •We use lambda expressions to create anonymous methods •Sometimes lambdas only call an existing method and do nothing else •It is easier to understand the code if we refer to the existing method by name •Method references are compact, easy-to-read lambda expressions for methods that already have a name •References: •https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html •Java Brains - https://youtu.be/lwwIZuwYmNI
  • 12. Java Method References Kind Syntax Examples Reference to a static method ContainingClass::staticMethodName Person::compareByAge Reference to an instance method of a particular object containingObject::instanceMethodName myApp::appendStrings2 Reference to an instance method of an arbitrary object of a particular type ContainingType::methodName String::compareToIgnoreCase Reference to a constructor ClassName::new HashSet::new
  • 13. Java Method References Example •Thread accepts a Runnable as an argument. Runnable is a functional interface whose single abstract method is run (zero parameters and void return type) •When an instance of Thread calls it’s start method, the Runnable run method runs in a new thread of execution. •Here we create a lambda that just calls the System.out.println() function •We see that the method reference in thread2 has the same behaviour as calling the method in thread1
  • 14. Java Method References Example •Here we create a list of Car objects and a Predicate similar to earlier examples • The perform conditionally method accepts a list a predicate and a Consumer • If any element from the list satisfies the Predicate the Consumer operates on it. •In this case our Consumer is defined, using a method reference, as the block of code associated with System.out.println