Over

120,000

Worldwide

Saturday - Sunday CLOSED

Mon - Fri 8.00 - 18.00

Call us

 

Introduction to Lambda Expressions

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:

 You cannot declare a parameter or a local variable within a lambda expression that has the same name as a variable in the enclosing method
 If there is a class level variable, and if the lambda defines a parameter or local variable with the same name, the parameter or local variable shadows the class level variable
 You cannot have multiple local variables within a lambda expression that have the same name
 When the this or super keyword is used in a lambda expression, it refers to the object within which the lambda expression is present, and not the functional interface
 Lambda expressions can access variables defined in the enclosing method

.

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:

 They help to provide different implementations for the same method on the fly
 They help to pass around code as method parameters
 They allow internally iterating over a collection via the forEach support
 They allow bulk operations on collections via the Stream API

Conclusion

Lambda expressions are a ground-breaking feature added by Java 8. They help to keep the code clean and concise.

.

Leave a Reply

Your email address will not be published. Required fields are marked *

Working Hours

  • Monday 9am - 6pm
  • Tuesday 9am - 6pm
  • Wednesday 9am - 6pm
  • Thursday 9am - 6pm
  • Friday 9am - 6pm
  • Saturday Closed
  • Sunday Closed