This article provides an introduction to
lambda expression introduced in JDK 8 and explains executing them using functional
interfaces.
What is Lambda expression in Java?
A Lambda expression represents an Anonymous
method.
Anonymous method concept is similar to that
of an Anonymous class… the difference being it implements a functional
interface.
Functional interface is a new interface
concept in Java 8. A functional interface can only declare one abstract method.
Lambda expressions allow the programmer to
pass code in a concise manner making the code clearer and flexible.
Lambda
expression syntax
A lambda expression contains:
- A
parameter list
- An
arrow symbol (->)
- Body
of the lambda containing statements
The syntax for Lambda expressions is:
(parameters) -> {statements;}
How
to write simple methods as lambda expression
Here is a simple method that displays a
message “Hello World”.
public void hello(){
System.out.println("Hello
World");
}
To convert this method to a lambda
expression, remove the access specifier “public”, return type “void” and method
name “hello” and write it as:
() -> {System.out.println("Hello
World");}
Here is another method example that adds two
numbers and returns their sum:
int
add(int x, int y){
return
x+y;
}
This
method can be converted to lambda expression as:
(int
x, int y) -> {return x+y;}
Important points about
lambda expressions
Here are some notable points regarding lambda
expressions:
1) A lambda expression can have zero or more
parameters.
Here is an example of lambda
expression with zero parameters:
()
-> {System.out.println("Hello World");}
Here is
an example of lambda expression with two parameters:
(int
x, int y) -> {return x+y;}
2)
If the type of the parameters can be decided by the compiler, then we can
ignore adding them in the lambda expression.
(int
x, int y) -> {return x+y;} // type mentioned
(x,y)
-> {return x+y;}; // type ignored
3)
If there is only one parameter. the parenthesis of parameter can be omitted,
x
-> {return x+10;}
String
s -> s.toUpperCase()
String
s -> System.out.println(s)
4)
If the body has just one expression, the return keyword and curly braces can be
omitted as show below:
(int
x, int y) -> x + y
Calling a lambda expression
Once
a lambda expression is written, it can be called and executed like a method.
For
calling a lambda expression, we should create a functional interface.
Here
is an example that uses functional interface to execute lambda expression:
public
class FuntionalInterfaceDemo {
interface
MyInter{
void
hello();
}
public
static void main(String[] args) {
//
TODO Auto-generated method stub
MyInter
infVar = () -> {System.out.println("Hello World");};
infVar.hello();
}
}
Java
8 added lambda expressions and Stream api.
The
Stream api java.util.Stream provides a forEach that can be used to loop over
collection as shown below :
public
class ForLoopExample {
public
static void main(String[] args) {
List
categories = Arrays.asList("Java","Dot Net","Oracle","Excel");
//
For Each loop
for(String
category: categories){ System.out.println(category); }
//
Java 8 Lambda For loop
categories.stream().forEach(category->
System.out.println(category));
}
}
No comments:
Post a Comment