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 References as Lambdas

Java lambda implementation provides a shorter way to express the method call.  

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

Above example passes println() method reference with the help of double colon (::). It tells the Java compile that method will be applied to the passed parameter of the 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 -,


Popular posts from this blog

Java Streams API - Process set of elements

Java 8 Vs Java 9

Artificial Intelligence Vs Machine Learning Vs Deep Learning