This document discusses functional interfaces and method references in Java. It covers the main functional interfaces in the java.util.function package like Function, Predicate, and Consumer. It explains how to compose functions using andThen() and how method references provide a compact way to refer to existing methods rather than defining anonymous methods. Examples are provided to demonstrate method references with Thread, Runnable, and filtering a list.
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
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.
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