Lambda expression is a new and important feature of Java which was included in Java SE 8. It provides a clear and concise way to represent one method interface using an expression. It is very useful in collection library. It helps to iterate, filter and extract data from collection. Before lambda expression, anonymous inner class was the only option to implement the method.
In other words, we can say it is a replacement of java inner anonymous class. Java lambda expression is treated as a function, so compiler does not create .class file.
Functional Interface
Lambda expression provides implementation of functional interface. An interface which has only one abstract method is called functional interface. Java provides an anotation @FunctionalInterface, which is used to declare an interface as functional interface.
Why use Lambda Expression
To provide the implementation of Functional interface.
Less coding.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
interface Drawable{
public void draw();
}
interface Sayable{
public String say();
}
interface Sayable2{
public String say(String name);
}
interface Operation{
int operate(int a,int b);
}
public class LambdaExpressionExample {
public static void main(String[] args) {
int width=10;
//
//Drawable d=new Drawable() {
// @Override
// public void draw() {
// System.out.println(“.draw() “+ width);
// }
// };
//
//with lambda
Drawable d=()-> {
System.out.println(“.draw() “+ width);
};
d.draw();
//——————Whith out prammters——————————-
//Sayable s= new Sayable() {
// @Override
// public String say() {
// return “I have nothing to say.”;
// }
// };
Sayable s= ()->{
return “I have nothing to say.”;
};
System.out.println(s.say());
System.out.println(“==========================================”);
//————– Single Parameter——————————–
Sayable2 s2=(String saySomething)->{
return saySomething;
};
System.out.println(s2.say(“Welcome back”));
System.out.println(“==========================================”);
//————– Multiple Parameters————————
Operation addation=(a, b) ->{return a+b;};
Operation substraction=(a,b)->{return a-b;};
Operation diviation=(a,b)->{return a/b;};
Operation Multiplication=(a,b)->(a*b); //without return keyword-
System.out.println(addation.operate(10,20));
System.out.println(substraction.operate(10,20));
System.out.println(diviation.operate(10,20));
System.out.println(Multiplication.operate(10,20));
System.out.println(“==========================================”);
//————Foreach Loop——————-
List list=new ArrayList();
list.add(“ankit”);
list.add(“mayank”);
list.add(“irfan”);
list.add(“jai”);
list.forEach((x)->System.out.println(x));
list.forEach((n)->System.out.println(n));
System.out.println(“==========================================”);
list.forEach((item)->System.out.println(item));
System.out.println(“==========================================”);
list.forEach(item->{
if(“C”.equals(item)){
System.out.println(item);
}});
System.out.println(“==========================================”);
list.forEach(item->{
if(“C”.equals(item)){
System.out.println(item);
}});
System.out.println(“==========================================”);
list.forEach(System.out::println); //method reference
System.out.println(“==========================================”);
//Stream and filter
list.stream()
.filter(k->k.contains(“f”))
.forEach(System.out::println);
System.out.println(“==========================================”);
Map<String, Integer> items = new HashMap<>();
items.put(“A”, 10);
items.put(“B”, 20);
items.put(“C”, 30);
items.put(“D”, 40);
items.put(“E”, 50);
items.put(“F”, 60);
for (Map.Entry<String, Integer> entry : items.entrySet()) {
System.out.println( entry.getKey() + ” = ” + entry.getValue());
}
System.out.println(“==========================================”);
//with foreach and lambada
items.forEach((k,v)->{
if(k.contains(“A”))
System.out.println(k+” = “+v);
}
);
System.out.println(“==========================================”);
//passing lambda as argument
LambdaExpressionExample l =new LambdaExpressionExample();
l.x((a,b)-> { return a*b;});
}
public void x(Operation operation){
operation.operate(0, 0);
}
}