Lambda expressions are considered to be the biggest feature in Java 8. In this article, I will be given an introduction to lambda expressions.
What is a lambda expression?
A Lambda expression is basically a function, so given an input value, it produces some output. Java 8 lambda expressions specify anonymous methods, that is methods without a method name and without being associated with any particular object. Lambda expressions are generally used to implement a functional interface.
Functional Interfaces
Functional interfaces were also added by Java 8 so as to support lambda expressions. Since Java is an object oriented language, lambda expressions cannot be used on their own. A functional interface is simply an interface that has only one abstract method. Lambda expressions are used to implement the method in a functional interface. Prior to Java 8, Java already had interfaces like Runnable, Comparator, ActionListener, etc. which had a single abstract method. Starting with Java 8, these were officially designated as functional interfaces using the @FunctionalInterface annotation. In addition, Java 8 also provides some more functional interfaces in the java.util.Function package. Some of the interfaces in the Function package are Predicate, Consumer, Function, Supplier, etc.
Syntax of Lambda expression
Lambda expression have the following syntax:
(parameters) -> {body}
Parameters
Parameters are the values passed in to the lambda expression. This must match the parameters in the functional interface method that the lambda expression implements.
Lambda operator
-> is known as the lambda operator.
Body of the lambda expression
Any code placed in curly brackets after the lambda operator constitutes the body of the lambda expression.
Examples of Lambda Expressions
The following are some examples of lambda expressions:
Single parameter, no return value |
num -> System.out.println(“In method, num is “+num); |
Single integer parameter with datatype specified, no return value |
(Integer num) -> System.out.println(“In method, num is “+num); |
Multiple parameters, no return value |
(num1,num2) -> System.out.println(“In method”); |
Multiple parameters, with return value |
(num1,num2) -> num1+num2; |
Multiple parameters, With multiple statements in the body |
(num1,num2) -> { int num3 = num1+num2; System.out.println(“Sum is “+num3); } |
Scoping Rules for lambda expressions
The scoping rules for a lambda expression are same as that for a nested code block. Some of the rules are as follows:
Method References and their Types
Method references are a step further from lambda expressions, they further reduce the code required to be written in a lambda expression. Sometimes, your class may already have a method that has the same code that you would like to specify in your lambda expression. In such scenarios, the method reference operator can be used to refer to this existing method instead of re-writing it using a lambda expression. It is represented by two double colons (::). There are 4 types of method references as follows:
Type |
Syntax |
Explanation |
Static method reference |
className::methodName |
Used to refer to a static method of a class |
Instance method reference |
objectName::methodName |
Used to refer to an instance method of a class |
Arbitrary method reference |
className::methodName |
Used to refer to an arbitrary method of a class |
Constructor reference |
Classname::new |
Used to refer to a constructor of a class |
Benefits of Lambda expressions
As seen earlier, lambda expressions help to keep the code clean and concise. In addition, they also offer the following benefits:
Conclusion
Lambda expressions are a ground-breaking feature added by Java 8. They help to keep the code clean and concise.
Leave a Reply