Lambda Expression
What is Lambda Expression ?
- Lambda expressions are introduced in Java 8 and are touted to be the biggest feature of Java 8. Lambda expression facilitates functional programming, and simplifies the development a lot.
- Lambda Expressions, a new language feature, has been introduced in this release. They enable you to treat functionality as a method argument, or code as data. Lambda expressions let you express instances of single-method interfaces (referred to as functional interfaces) more compactly.
- Lambda expression is an anonymous function (not 100% true for Java but lets assume it for time being). Simply put, it’s a method without a declaration, i.e., access modifier, return value declaration, and name.It’s a shorthand that allows you to write a method in the same place you are going to use it. Especially useful in places where a method is being used only once, and the method definition is short. It saves you the effort of declaring and writing a separate method to the containing class.
Lets see some example and how the code is different from Java Older Version
Using Java 7 :
List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
for(Integer n: list) {
System.out.println(n);
}
Using Java 8 :
list.forEach(n -> System.out.println(n));
or
list.forEach(System.out::println);
//sum of all number using Java 7
int total = 0;
for(Integer element : values){
total += element * 2;
}
System.out.println(total);
o/p - 42
//sum of all number using Java 8
System.out.println(
values.stream()
.map(e -> e * 2)
.reduce(0, (c,e) -> c+e));
);
o/p - 42
// sum of all values using Java 7
public int totalValues(List numbers){
int total = 0;
for(Integer element : numbers){
total += element;
}
return total;
}
//sum of all the even values
public int totalEvenValues(List numbers){
int total = 0;
for(Integer element : numbers){
if(e % 2 == 0) total += element;
}
return total;
}
//sum of all the odd values
public int totalOddValues(List numbers){
int total = 0;
for(Integer element : numbers){
if(e % 2 != 0) total += element;
}
return total;
}
totalValues(values);
totalEvenValues(values);
totalOddValues(values);
// Solution by using strategy pattern using Java 8
import java.util.function.Predicate;
public int totalValues(List numbers, Predicate selector){
int total = 0;
for(Integer element : numbers){
if(selector.test(e))
total += element;
}
return total;
}
//0 stands for intial value where wanna starts
public int totalValues(List numbers, Predicate selector){
return numbers
.stream()
.filter(selector)
.reduce(0 , (c, e) -> c + e);
}
totalValues(values, e -> true); // sum of all
totalValues(values, e -> e % 2 == 0); // sum of even
totalValues(values, e -> e % 2 != 0); // sum of odd
//double of first even number in the list which is greater
//then 3 using Java 7
int result = 0;
for(int e : values){
if(e > 3 && e % 2 == 0){
result = e * 2;
break;
}
}
System.out.println(result);
o/p - 8
//Solution using Java 8
System.out.println(values.stream()
.filter(e -> e > 3)
.filter(e -> e % 2 == 0)
.map(e -> e * 2)
.findFirst());
o/p - 8 [optional 8]
or
//remove null pointer exception use orElse(0)
System.out.println(values.stream()
.filter(e -> e > 3)
.filter(e -> e % 2 == 0)
.map(e -> e * 2)
.findFirst()
.orElse(0));
o/p - 8
or
//works lazily or efficient
System.out.println(values.stream()
.filter(Sample::isGT3)
.filter(Sample::isEven)
.map(Sample::doubleIt)
.findFirst()
.orElse(0));
o/p - 8
public static boolean isEven(int number){
return number % 2 ==0;
}
public static boolean isGT3(int number){
return number > 3;
}
public static int doubleIt(int number){
return number * 2;
}
For more detail : you can watch below video
Please Please subscribe our channel for more updates.
Comments
Post a Comment