28/05/2018 Lambda Expressions in Java 8 - GeeksforGeeks
Lambda Expressions in Java 8
Lambda expressions basically express instances of functional interfaces(An interface with single abstract
method is called functional interface. An example is [Link]). lambda expressions implement
the only abstract function and therefore implement functional interfaces
lambda expressions are added in Java 8 and provide below functionalities.
Enable to treat functionality as a method argument, or code as data.
A function that can be created without belonging to any class.
A lambda expression can be passed around as if it was an object and executed on demand.
// Java program to demonstrate lambda expressions
// to implement a user defined functional interface.
// A sample functional interface (An interface with
// single abstract method
interface FuncInterface
{
// An abstract function
void abstractFun(int x);
// A nonabstract (or default) function
default void normalFun()
{
[Link]("Hello");
}
}
class Test
{
public static void main(String args[])
{
// lambda expression to implement above
// functional interface. This interface
// by default implements abstractFun()
FuncInterface fobj = (int x)>[Link](2*x);
// This calls above lambda expression and prints 10.
[Link](5);
}
}
Run on IDE
Output:
10
Syntax:
[Link] 1/4