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

What is Functional Interface?

Functional interfaces are those interfaces that have only one abstract method in it. It can have more than one default or static method and can override the method from java.lang.object. Let’s create a functional interface:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

@FunctionalInterface

publicinterfaceDecorable{

// one abstract method

voiddecorateWithCurtains();

// default method

defaultvoiddecorateWithPaints()

{

System.out.println("Decorating using paints");

}

// Overriding method of java.lang.Object

@Override

publicinthashCode();

}

Java can itself identify Functional Interface but you can also denote interface as Functional Interface by annotating it with @FunctionalInterface.

The problem with Anonymous Comparator is of syntax. Each time you want to sort the list using a comparator, you have to remember the bulky syntax.

So generally the main problem with
Anonymous classes is syntax. For very simple operation, we need to write complex code. To solve this problem, JDK has introduced a new 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
functionwithout name,

The connection between Lambda Expression and Functional Interface:

You might be thinking I have introduced the functional Interface above but how it is connected to Lambda. So Lambda expression can be applied for the abstract method of functional Interface which is being implemented or being instantiated anonymously.

Structure of Lambda Expressions

(Argument List) ->{expression;} or
(Argument List) ->{statements;}

So we can divide structure of Lambda expression to three parts:

Argument list or parameters

Lambda expression can have zero or more arguments.

()->{System.out.println(“Hello”)}; //Without argument, will print hello
(int a)->{System.out.println(a)}; // One argument, will print value of a
(int a,int b)-> {a+b};//two argument, will return sum of these two integers

You can choose to not declare the type of arguments as it can be inferred from context.

(a,b)->{a+b}; // two argument, will return sum of these two numbers

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

(int a,b)->{a+b}; // Compilation error

When there is a single parameter, if its type is inferred, it is not mandatory to use parentheses

a->{System.out.println(a)}; // Will print value of number a

Array token (->)

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.

Let’s take some examples , you want to initialize thread and now you will realize, how can lambda expression make your coding easy :

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

publicclassThreadSample{

publicstaticvoidmain(String[]args){

// old way

newThread(newRunnable(){

@Override

publicvoidrun(){

System.out.println("Thread is started");

}

}).start();

// using lambda Expression

newThread(()->System.out.println("Thread is started")).start();

}

}

Let’s take another example of Comparator which we have seen in Anonymous Comparator in java. We will create list of movies and sort it by movie name using trivial way and lambda expressions.