Lambda Expression in java
Lambda Expression used to loop Iteration , method declaration and etc. If you want to define the abstract method without creating the any other class , you can use Lambda Expression. Thats a main reason for using Lambda Expression in method.
Example-1
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> num = new ArrayList<Integer>(); num.add(10); num.add(20); num.add(30); num.forEach(i -> System.out.println(i)); } }
Output
10 20 30
Example-2
interface A { void show(); } public class Main { public static void main(String[] args) { A obj; obj = () -> { System.out.print("show"); }; obj.show(); } }
Output
show