Lambda Expressions

Lambda expressions provide the simplest and shortest way to define anonymous functions.  Java Swing frequently uses anonymous functions for event handling.

Lambda expressions define the body of Functional Interfaces.

An interface that has only one abstract method, is called Functional Interface. It can be annotated by @FunctionalInterface annotation.

Let's define HelloInt functional interface that has an abstract method say() :
interface HelloInt {
public void say();

Now define interface definition using old anonymous function approach: 

HelloInt aFun = new HelloInt() {
public void say() {
System.out.println("Hello Anonymous Function");


Let's define the anonymous function using Lambda expression:  

HelloInt lambFun = () -> System.out.println("Hello Lambda");

The above example, clearly shown the shortest code of an anonymous function definition. The only condition is interface must have a single abstract method.  In other words, the interface must be a Functional Interface.

Few  most frequently used functional interfaces are  java.lang.Runnable and  java.lang.Comparable

Lambda Expression Syntax:

A lambda expression is composed of three parts.

 Argument List Arrow Token Body Remark
 () -> System.out.println("Hello Lambda") No parameters 
 x -> System.out.println(x) Single Parameters 
 (x, y) -> return x+y     Muti parameters 
 (x, y) ->{
     x +=2;
     return x*y;
 Body with multiple statements 

Lambda expressions may have zero or more arguments. The body can be either a single expression or a statement block. 

Let's define MathInt functional interface with multiple parameters.  It will have multiple lambda definitions to calculate add, subtract, and multiply of two numbers:

interface MathInt {
public int calculate(int a, int b);

MathInt add = (a, b) -> {
return a + b;
MathInt multiply = (a, b) -> {
return a * b;
MathInt subtract = (a, b) -> {
return a - b;

int a = 10;
int b = 5;
System.out.println("Add " + add.calculate(a, b));
System.out.println("Multiply " + multiply.calculate(a, b));
System.out.println("Suntract " + subtract.calculate(a, b));

Method Reference as Lambda

Java lambda implementation provides a shorter way to express a method call using the (::) operator that separates the method name from the name of an object or class in a method reference.

Here is an example of a method reference.
interface GreetingInt {
public void hello(String name);
GreetingInt g = System.out::println;

The above example passes a reference of method println() with the help of a double colon (::). It tells the Java compiler that the method will be applied to the passed parameter of the lambda method.
GreetingInt g = System.out::println;
is the replacement of 
GreetingInt g = (n) -> System.out.println(n);

You can reference the following types of methods:
  1. Static method
  2. Instance method on parameter objects
  3. Instance method
  4. Constructor
Example Source Code

More ...

Also, Watch:

► Deploy Angular build on Tomcat or WAMP server -  
► SpringBoot Hibernate Integration -  
► Spring Boot – Create RESTful Web Service -  
► Angular app using REST API (Part 3) -  
► Test Public REST API -  
► Angular First Project (Part-2) -  
► Angular First Project (Part-2) -  
► First Python Program -  



►URL -,


  1. Are you looking for a reliable Hibernate Training in Delhi? Look no further than APTRON Delhi! With our comprehensive Hibernate training, you can master this popular Java framework and build enterprise-level applications with ease.


Post a Comment

Popular posts from this blog

Java Streams API - Process set of elements

Java 8 Vs Java 9

Concurrent Collection in Java