No comments yet

How to use lambda expressions with Functional Interface?

[Social9_Share type="horizontal"]

Using Lambda Expressions with Functional Interfaces in java

In this blog, lets learn how to use lambda expression with functional interface in Java 8.
Lets walk through

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.

For example, a method declaration like this in java

can be written as

Syntax and Structure

Let us now see the basic syntax and structure for Lambda expression. 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

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

Few examples of lambda expression

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

Example-1 : Use Lambda with functional interface

Let’s create a functional interface with the name BonusCalculator, having one method public double calcBonus(int amount)

Next, let’s create a public class EmpDetails with a method public void getBonus(BonusCalculator bc, int amount) that takes BonusCalculator as parameter.

Next, let’s create a class LambdaDemo with main method. Instead of using a class that implement BonusCalculator or using anonymous inner class, let’s use Lambda expression as below.

where amount is the argument.The complete code using anonymous inner class and Lambda is given below for comparison

Example-2 : Use Lambda with functional interface

One more example that shows the different ways in which Lambda can be used. Let’s create three functional interfaces and try to implement and call the methods using Lambda.

This is about lambda expressions.

Thats all.
Happy Coding.

Post a comment