SlideShare ist ein Scribd-Unternehmen logo
1 von 15
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
Reactive Systems Functional Interfaces & Method References

Weitere ähnliche Inhalte

Ähnlich wie Reactive Systems Functional Interfaces & Method References

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 StreamsEmiel Paasschens
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project LambdaRahman USTA
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8icarter09
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streamsjessitron
 
Java 8 Functional Programming - I
Java 8 Functional Programming - IJava 8 Functional Programming - I
Java 8 Functional Programming - IUgur Yeter
 
Week-1..................................
Week-1..................................Week-1..................................
Week-1..................................kmjanani05
 
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
 
Stream Api.pdf
Stream Api.pdfStream Api.pdf
Stream Api.pdfAkaks
 
java 8 new features
java 8 new features java 8 new features
java 8 new features Rohit Verma
 
Lambda expressions java8 - yousry
Lambda expressions java8 - yousryLambda expressions java8 - yousry
Lambda expressions java8 - yousryyousry ibrahim
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An OverviewIndrajit Das
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014Nayden Gochev
 
Lambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APILambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APIPrabu U
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8 Bansilal Haudakari
 
Java 8 - Features Overview
Java 8 - Features OverviewJava 8 - Features Overview
Java 8 - Features OverviewSergii Stets
 
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
 

Ähnlich wie Reactive Systems Functional Interfaces & Method References (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
 

Kürzlich hochgeladen

Citronella presentation SlideShare mani upadhyay
Citronella presentation SlideShare mani upadhyayCitronella presentation SlideShare mani upadhyay
Citronella presentation SlideShare mani upadhyayupadhyaymani499
 
Neurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trNeurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trssuser06f238
 
Environmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial BiosensorEnvironmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial Biosensorsonawaneprad
 
Pests of Bengal gram_Identification_Dr.UPR.pdf
Pests of Bengal gram_Identification_Dr.UPR.pdfPests of Bengal gram_Identification_Dr.UPR.pdf
Pests of Bengal gram_Identification_Dr.UPR.pdfPirithiRaju
 
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.PraveenaKalaiselvan1
 
Davis plaque method.pptx recombinant DNA technology
Davis plaque method.pptx recombinant DNA technologyDavis plaque method.pptx recombinant DNA technology
Davis plaque method.pptx recombinant DNA technologycaarthichand2003
 
User Guide: Pulsar™ Weather Station (Columbia Weather Systems)
User Guide: Pulsar™ Weather Station (Columbia Weather Systems)User Guide: Pulsar™ Weather Station (Columbia Weather Systems)
User Guide: Pulsar™ Weather Station (Columbia Weather Systems)Columbia Weather Systems
 
User Guide: Magellan MX™ Weather Station
User Guide: Magellan MX™ Weather StationUser Guide: Magellan MX™ Weather Station
User Guide: Magellan MX™ Weather StationColumbia Weather Systems
 
Base editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editingBase editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editingNetHelix
 
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...lizamodels9
 
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPirithiRaju
 
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)riyaescorts54
 
The dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptxThe dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptxEran Akiva Sinbar
 
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptx
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptxSTOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptx
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptxMurugaveni B
 
GenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptxGenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptxBerniceCayabyab1
 
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdf
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdfBUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdf
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdfWildaNurAmalia2
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfSELF-EXPLANATORY
 
Pests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdfPests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdfPirithiRaju
 
REVISTA DE BIOLOGIA E CIÊNCIAS DA TERRA ISSN 1519-5228 - Artigo_Bioterra_V24_...
REVISTA DE BIOLOGIA E CIÊNCIAS DA TERRA ISSN 1519-5228 - Artigo_Bioterra_V24_...REVISTA DE BIOLOGIA E CIÊNCIAS DA TERRA ISSN 1519-5228 - Artigo_Bioterra_V24_...
REVISTA DE BIOLOGIA E CIÊNCIAS DA TERRA ISSN 1519-5228 - Artigo_Bioterra_V24_...Universidade Federal de Sergipe - UFS
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentationtahreemzahra82
 

Kürzlich hochgeladen (20)

Citronella presentation SlideShare mani upadhyay
Citronella presentation SlideShare mani upadhyayCitronella presentation SlideShare mani upadhyay
Citronella presentation SlideShare mani upadhyay
 
Neurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trNeurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 tr
 
Environmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial BiosensorEnvironmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial Biosensor
 
Pests of Bengal gram_Identification_Dr.UPR.pdf
Pests of Bengal gram_Identification_Dr.UPR.pdfPests of Bengal gram_Identification_Dr.UPR.pdf
Pests of Bengal gram_Identification_Dr.UPR.pdf
 
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
 
Davis plaque method.pptx recombinant DNA technology
Davis plaque method.pptx recombinant DNA technologyDavis plaque method.pptx recombinant DNA technology
Davis plaque method.pptx recombinant DNA technology
 
User Guide: Pulsar™ Weather Station (Columbia Weather Systems)
User Guide: Pulsar™ Weather Station (Columbia Weather Systems)User Guide: Pulsar™ Weather Station (Columbia Weather Systems)
User Guide: Pulsar™ Weather Station (Columbia Weather Systems)
 
User Guide: Magellan MX™ Weather Station
User Guide: Magellan MX™ Weather StationUser Guide: Magellan MX™ Weather Station
User Guide: Magellan MX™ Weather Station
 
Base editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editingBase editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editing
 
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
 
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
 
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
 
The dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptxThe dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptx
 
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptx
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptxSTOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptx
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptx
 
GenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptxGenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptx
 
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdf
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdfBUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdf
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdf
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
 
Pests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdfPests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdf
 
REVISTA DE BIOLOGIA E CIÊNCIAS DA TERRA ISSN 1519-5228 - Artigo_Bioterra_V24_...
REVISTA DE BIOLOGIA E CIÊNCIAS DA TERRA ISSN 1519-5228 - Artigo_Bioterra_V24_...REVISTA DE BIOLOGIA E CIÊNCIAS DA TERRA ISSN 1519-5228 - Artigo_Bioterra_V24_...
REVISTA DE BIOLOGIA E CIÊNCIAS DA TERRA ISSN 1519-5228 - Artigo_Bioterra_V24_...
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentation
 

Reactive Systems Functional Interfaces & Method References

  • 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