functional interfaces & lambda expressions Flashcards
any interface that has exactly one abstract method is called …
a functional interface
abstract method
methods from e.g. an interface that has a signature but not an implementation (the class that implements the interface will define the method with its own implementation - don’t forget annotation @Override)
is List a functional interface?
no - it does not have only one method, it has many methods. A functional interface must have one method.
can a developer create a new functional interface?
yes they can. Java libraries also provide many functional interfaces - they are called “the standard Java functional interfaces”. (we cover four in ATA)
What are the four functional interfaces from Java that are covered in ATA?
Function, Supplier, Consumer, Predicate
To implement an interface the standard way…
- The class header declares which interface it’s implementing
- The class implementation overrides each abstract method
- Create and document entire class
A great deal of effort just to use the single method inside!
(hint: instead, use lambda expressions)
lambda expressions provide…
a quick and easy way to implement a functional interface without expliciitly defining a class or even a formal method declaration.
syntax of a lambda expression
argument -> method implementation or (arg1, arg2) -> method implementation or () -> method implementation or (arg(s)) -> { mulit-line method implementation } Because each functional interface contains exactly one abstract method, lambda expressions allow us to implement only that one method (following the arrow) to satisfy the interface. We don't need to creat a class, and since the interface has only one method, we don't even need to name it!
three parts to lambda expression
- the method argument
- the arrow separator ->
- the method implementation
Function interface
public interface Function { R apply(T t); } takes an input object, returns an output object
why don’t we need to name the method we call in a lambda expression?
the lamda “operator” -> automatically calls the ONE method in the interface, that one method is defined on the right of the -> operator
Supplier interface
public interface Supplier { T get(); } no input, returns an output
Supplier examples
List participants = {"ace", "queen", "diamond"} Random random = new Random(); Supplier randomIndex = () -> random.nextInt(participants.size()); ------ public void callOnParticipant(Supplier index) { String person = participants.get(indexChooser.get()); System.out.println("Hello " + person); }
Supplier interface notes
- Supplier does not take input directly, but may access any variable within its scope
- called with empty parenthesis () -> the get method implementation
- it doesn’t “appear” to have a return but does!
note on keyword ‘return’ in lambda expressions
if you drop the curly braces from your one-line method implementation, and the functional interface that your lambda expression is implementing has a return value, you drop the keyword return. (most common usage of a lambda)