Lambda in Java
When Can Lambda Be Used
Lambda’s allow programmers to implement an interface that contains one abstract method, without having to write the full class. They consist of:
- a parameter list,
- the arrow token, and
- a return value based on the body of the code statements.
So, here are the key points:
- We are writing the body of a method
- The method we are writing is the abstract method of an interface
- The compiler knows which method, since the interface has only ONE abstract method
- The number and type of parameters is defined by the abstract method in the interface
Implementing Interface without Lambda
Consider the following Computations interface:
Every time we wanted to provide unique functionality for the operation method in Computations, we would need to:
- create a class that implements
Computations - create an instance of this class
- call the method
operation
We could create classes AdditionComputation and MultiplyComputation as well as several others, such as:
and similarly for MultiplyComputation:
To use these classes, we might write the following:
Try it in the Java Playground
Implementing Interface with Lambda
With lambdas, we can create an instance of Computations that can be used to call the operation method and provide the functionality as part of declaring and creating that instance.
- The type of the instance is the type of interface.
- The reference will be assigned the implementation. Use parenthesis and parameters. The number of parameters in the parenthesis, is the number of parameters the abstract method in the interface takes.
- Then use the arrow operator -> followed by the implementation of the method.
The general form is:
Consider implementing an operation method to subtract two values. We can create an instance, for example called subt, and use lambda to provide the implementation of the operation method.
- The type of the instance is
Computations. - The reference should describe the implementation so
subtor something that implies subtraction. - The
operationmethod takes two parameters, sofandsin this case. - And the implementation is to subtract these parameters,
f - s.
Try it in the Java Playground
- Predict the output of the code then test your prediction by running it in the Java Playground.
- Edit the code to change the values that are being passed as parameters to
operation. How does this change the output? - Add implementations for multiply and add. Provide sample calls with output to test your code.
Positive Difference
Let's try an example where the body of the method contains more than one statement, such as an if statement.
Let's implement operation to compute the positive difference.
- The type of the instance is
Computations. - The reference should describe the implementation so
posDifferenceor something that implies subtraction. - The
operationmethod takes two parameters, sofandsin this case. - And the implementation is to subtract the smaller value from the larger value.
Try it in the Java Playground
- Predict the output of the code then test your prediction by running it in the Java Playground.
- Edit the code to change the values that are being passed as parameters to
operation. How does this change the output?
You can also use an if statement instead of the conditional operator.