Lambda Expression in java
It is a short way of writing the implementation of functional interface. Functional interface is an interface, a class with only one abstract method. It can also have multiple default and static methods.
It is introduced in java 8. Lambda requires functional interface.
It make code shorter, cleaner and easier to read, especially when working with collections and functional interface.
Why do we need Lambda Expression?
- Less code — We don’t need to write any complex code.
- Better Readability — We can able to easily understand the logic.
- Pass behaviour as a parameter — We can pass a piece of code to another method.
Lambda Expression and Lambda Block
Syntax for lambda expression:
(a,b)->a+b;Syntax for lambda block:
(a,b)->{
int sum = Math.max(a, b);
return sum;
}Lambda with no parameters
@FunctionalInterface
interface Message{
void show();
}
public class Demo {
public static void main(String[] args) {
Message m=()-> System.out.println("Hello world!");
m.show();
}
}
output:
Hello world!Lambda with return values
@FunctionalInterface
interface Calculator{
int add(int a,int b);
}
public class Demo {
public static void main(String[] args) {
Calculator c=(a,b)->a+b;
System.out.println(c.add(10,20));;
}
}
output:
30Using lambda with finding odd/even:
@FunctionalInterface
interface NumberCheck{
boolean checkEven(int a);
}
public class Demo {
public static void main(String[] args) {
NumberCheck c=(n)->n%2==0;
System.out.println(c.checkEven(10));
}
}
output:
trueWorking with collections:
Get Vignesh’s stories in your inbox
Join Medium for free to get updates from this writer.
Using lambda functions, we can easily work with collections and perfrom many operations such as forEach(), map() and removeIf().
They help us to write collections opreation in shorter and more readable way.
Using forEach() Method:
public class Demo {
public static void main(String[] args) {
List<String> names= Arrays.asList("Vignesh","Linkesh","Eswar","Vadivel");
names.forEach((name)-> System.out.println(name.toUpperCase()));
//Another way of printing elements
names.forEach(System.out::println);
}
}Equivalent Lamda:
names.forEach((name)-> System.out.println(name));Method Reference:
names.forEach(System.out::println);The above one is called method reference, shorter way of writing lambda expression.
For each element, call the println() method.
Using removeIf() method:
public class Demo {
public static void main(String[] args) {
List<Integer> nums=new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
nums.removeIf((num)->num%2==0);
System.out.println(nums);
}
}What is Stream() in Lambda Epression?
Stream() is not a part of Lamda expression.
Stream() is a part of Java Stream API, and Lambda expreassion are commonly used with stream in java.
Why do we need Stream()?
Streams allows us to process the collection of elements, without creating any complex loops.
public class Demo {
public static void main(String[] args) {
List<Integer> nums=new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
nums.stream()
.filter((num)->num%2==0)
.map((num)->num*num)
.forEach(System.out::println);
}
} // This is also called as stream chaining.Explanation:
Filter() method helps to filter the elements based on the condition, In the above example even numbers are passed to the next phase of stream. Then we again processing the List element, using map() method we can transform elements and it returns the new Stream.
Finally we used forEach() method through iterate and print the elements in the stream.







