Lambda expression is a Java programming language feature introduced in Java 8 that provides functional programming constructs to the Java programming language, which simplifies the Java code in certain cases such as with Java anonymous inner classes. Lambda expressions blends functional programming features with object oriented programming features of Java resulting in simplified and more powerful concurrency features.
Lambda expressions are blocks of Java code that can be defined and passed as data which can be executed at a later time.
Functional interfaces are Java interfaces which have only one declared (abstract) method. Functional interfaces can have other implemented methods such as default methods and static methods. Some of the common functional interfaces in Java are Runnable, Comparable, ActionListner etc.
Functional interfaces are generally implemented as inner classes and anonymous inner classes, which results in bulky code with many lines. This is sometimes referred to as 'Vertical' problem.
Lambda expressions are used to implement functional interfaces which define a single method. Lambda expressions avoid the vertical problem by simplifying the code of the traditional inner classes and anonymous inner classes.
Lambda expression simplifies the inner class and anonymous inner class code, which usually suffer from the 'vertical' problem (Too many lines of code required to implement a basic logic). Lambda expressions avoid the 'vertical' problem by simplifying and reducing the number of lines required to implement the inner class functionality.
Lambda expressions can be used implement interfaces having default and static methods only if there is a single abstract method in the interface. This called a functional interface.
From Java 8 onwards, an interface can contain default methods and static methods whose implementation is defined directly in the interface declaration.
A lambda expression consists of three parts
First - A parenthesized set of parameters, Second - An arrow pointing to right, Third - A body, which can be a block of Java code or a single expression.
//Passes an integer argument i, and returns 10+i
(int i) -> 10+i;
//Passes no arguments and returns 50
() -> 50;
//Passes string argument s and returns nothing
(String s) -> {system.out.println(s);}
A lambda expression can have zero, one or multiple parameters.
//Zero parameter lambda expression
() -> System.out.println('No parameters');
//One parameter lambda expression
(i) -> i*10;
//Multiple parameter lambda expression
(i1, i1) -> System.out.println(i1+i2);
This is a good exercise to clearly understand the concepts and advantages of lambda expressions. In below example we declare a functional interface 'EventChangeListener' that has one abstract method 'onChange()'. We then implement the interface to print the text 'Event Changed' when the onChange() method is invoked. For comparison, we implement the code using non-lambda code as well as with lambda expression...
*** See complete answer and code snippet in the Java Interview Guide.
Following three criteria must be met to match a lambda expression to an interface.
1. The interface must have one (and only one) abstract method...
*** See complete answer and code snippet in the Java Interview Guide.
Anonymous interface implementations can have state (member variables), whereas lambda expressions are stateless...
*** See complete answer and code snippet in the Java Interview Guide.
In most cases you do not need to specify the type for parameters in a lambda expression. The java compiler infers the type for the parameters by matching them with the parameter types of the abstract method of the functional interface.
For example, in below lambda expression ...
*** See complete answer and code snippet in the Java Interview Guide.
Yes, lambda function body can have multiple lines of code within curly braces {}...
*** See complete answer and code snippet in the Java Interview Guide.