1

Lambda Expressions in java

[Social9_Share type="horizontal"]

Lambda Expressions in java

In this blog let us see about Lambda expressions which is a feature of Java8. Lets walk through

  • What is a Lambda Expression?
  • Syntax and Structure
  • Why Lambda?
  • When to use Lambda expression
  • Points to be noted
  • A simple example to use Lambda

 

What is a Lambda Expression?

A Lambda Expression can be defined as a method without name, access specifier and also return type. They are useful while working with anonymous inner classes in Java and provides a convenient way to create anonymous functions.
Lambda expressions are built upon functional interfaces. Functional interfaces are interfaces that have a single abstract method.
Lambdas can be applied in many different contexts, ranging from simple anonymous functions to sorting and filtering Collections.
Java SE 8 added two packages, the java.util.function package that talks about functional interfaces, and the java.util.stream for heavy data operations in Collection classes.
Java 8 also added a new concept default methods in interfaces, which helps to add new functionality without affecting the existing contract. click here to know more on default methods.

For example, a method declaration like this in java

This can be written as

Syntax

The basic syntax for Lambda expression is

Lambda expression is a short way that allows to write a method in the same place where it will be used.

Few examples of lambda expression

Structure
Lambda expressions follow a basic structure to be written. They have

  • Argument List
  • Arrow (->) token
  • Body Part – Body can be a single expression or a statement block

 

Why Lambda ?

Lambda expressions help to reduce the syntactical problems that arise in anonymous classes. They also make the code writing simple, short and readable.
You write a method in the same place where you are going to use it, as they are anonymous functions.

When to use Lambda Expressions?
Whenever there is a functional interface, instead of using anonymous inner classes, you can use Lambdas.

Points to be noted

  • Lambda expression can have zero, one or more parameter.
  • It can be inferred from the context. i.e. (int a) is same as (a)
  • Parameters are enclosed in parentheses and separated by commas.eg. (a, b) or (int a, int b)
  • Empty parentheses specify empty set of parameters. eg. () -> 100
  • If there is a single parameter, then its type is inferred. eg. a -> return a*a;
  • You cannot use inferred and declared types together.eg. (int arg1, arg2) // invalid declaration
  • The body of lambda expression can contain zero, one or more statements.

 

A simple example to use Lambda

Now, let’s do a simple example.

I have used eclipse Mars, which supports Java 8 for this example.

I have created a functional interface with the name IncCalculator.java, having one method calculate(int salary). The code is given below.

Next, I have created a public class EmpDetails.java which has a method getBonus(IncCalculator inc, int salary) that takes IncCalculator object and int salary as parameter. This method is used to get the bonus of the employee. The code is given below.

Finally I am using a Test class having main method. While calling getBonus method of the EmpDetails class, instead of using anonymous inner class, I can use Lambda expression.
In my example, I have shown how anonymous class is used and also how Lambda replaces anonymous class easily. The code is given below.

The anonymous class which is written as

is now replaced with lambdas

where salary is the argument of the method calculate(). So, instead of creating anonymous class that implements IncCalculator.java, you can use Lambda expressions with great ease.

One more example that depicts the different ways in which Lambda can be used. I have created three functional interfaces and trying to implement and call the methods using Lambdas.

Thats all.

Happy Coding.

Comment(1)

  1. Reply
    Surekha says

    I also need an example in java 8 Streaming API

Post a comment