Lambda Expressions in Java 8


Java 8 has introduced a new feature called Lambda expressions.It is considered to be a major change in java. As this change will bring functional programming into java. Other languages such as Scala already have this feature so this is not new to programming world, it is new to java
Before understanding Lambda expressions, Lets first understand Functional Interface.

What is Functional Interface?

Functional interfaces are those interfaces which have only one abstract method in it. It can have more than one default or static method and can override method from java.lang.object. Lets create a functional interface:
Java can itself identify Functional Interface but you can also denote interface as Functional Interface by annotating it with @FunctionalInterface.
Some popular Functional Interfaces are:
  • java.lang.Runnable
  • java.util.concurrent.Callable
  • java.awt.event.ActionListener
  • java.util.Comparator

Why Lambda Expressions?

Lets understand with help of  Anonymous Comparator:
The problem with Anonymous Comparator is of syntax. Each time you want to sort list using comparator, you have to remember bulky syntax.
So generally main problem with Anonymous classes are syntax.For very simple operation, we need to write complex code. To solve this problem, JDK has introducenew feature called Lambda Expressions.I will take this example after explaining lambda expression to understand , how lambda expression will reduce this complex code.

What is Lambda Expressions:

lambda expression represents an anonymous function. It comprises of a set of parameters, a lambda operator (->) and a function body .You can call it function without name,
Connection between Lambda Expression and Functional Interface:
You might be thinking I have introduced functional Interface above but how it is connected to Lambda.So Lambda expression can be applied for abstract method of functional Interface which is being implemented or being instantiated anonymously.

Structure of Lambda Expressions

So we can divide structure of Lambda expression to three parts:
    1. Argument list or parameters
      • Lambda expression can have zero or more arguments.
      • You can choose to not declare type of arguments as it can be inferred from context.

      • you can not declare one argument’s type and do not declare type for other argument.

    • When there is a single parameter, if its type is inferred, it is not mandatory to use parentheses
  1. Array token (->)
  2. Body
    • Body can have expression or statements.
    • If there is only one statement in body,curly brace is not needed and return type of the anonymous function is same as of  body expression
    • If there are more than one statements, then it should be in curly braces and return type of anonymous function is same as value return from code block, void if nothing is returned.
Lets take some examples ,  you want to initialize thread and now you will realize, how can lambda expression can make your coding easy :
Lets take another example of Comparator which we have seen in Anonymous Comparator in java.
Create a class called
Create a main class called
As you can see here, we have used lambda expression for using Comparator. SO inspite of wrting Anonymous comparator, our expression became very easy.
So we have passed 2 arguments o1 and o2, we didn’t pass type because it can inferred from context.
We have only one statement here, so no need to put it in curly braces.

Lets create HelloWorld Lambda Expression Example:

Create an Interface Called HelloWorld:
Create a class called HelloWorldMain
Run above program you will get following output:
Hope you understand Lambda expression now, in next post, we will see more examples of it.

You may also like:


Add Comment